1
// Copyright 2014, 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
13
errors "github.com/juju/errors"
14
"github.com/juju/utils"
15
"gopkg.in/juju/names.v2"
16
"launchpad.net/gnuflag"
18
"github.com/juju/juju/apiserver/params"
19
"github.com/juju/juju/cmd/modelcmd"
22
func NewListCommand() cmd.Command {
23
return modelcmd.Wrap(&listCommand{})
26
// listCommand lists actions defined by the charm of a given service.
27
type listCommand struct {
29
applicationTag names.ApplicationTag
35
List the actions available to run on the target application, with a short
36
description. To show the full schema for the actions, use --schema.
38
For more information, see also the 'run-action' command, which executes actions.
42
func (c *listCommand) SetFlags(f *gnuflag.FlagSet) {
43
c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
44
f.BoolVar(&c.fullSchema, "schema", false, "Display the full action schema")
47
func (c *listCommand) Info() *cmd.Info {
50
Args: "<application name>",
51
Purpose: "List actions defined for a service.",
53
Aliases: []string{"list-actions"},
57
// Init validates the service name and any other options.
58
func (c *listCommand) Init(args []string) error {
61
return errors.New("no application name specified")
64
if !names.IsValidApplication(svcName) {
65
return errors.Errorf("invalid application name %q", svcName)
67
c.applicationTag = names.NewApplicationTag(svcName)
70
return cmd.CheckEmpty(args[1:])
74
// Run grabs the Actions spec from the api. It then sets up a sensible
75
// output format for the map.
76
func (c *listCommand) Run(ctx *cmd.Context) error {
77
api, err := c.NewActionAPIClient()
83
actions, err := api.ApplicationCharmActions(params.Entity{c.applicationTag.String()})
88
if len(actions) == 0 {
89
return c.out.Write(ctx, "No actions defined for "+c.applicationTag.Id())
93
verboseSpecs := make(map[string]interface{})
94
for k, v := range actions {
95
verboseSpecs[k] = v.Params
98
return c.out.Write(ctx, verboseSpecs)
101
shortOutput := make(map[string]string)
102
var sortedNames []string
103
for name, action := range actions {
104
shortOutput[name] = action.Description
105
if shortOutput[name] == "" {
106
shortOutput[name] = "No description"
108
sortedNames = append(sortedNames, name)
110
utils.SortStringsNaturally(sortedNames)
111
return c.printTabular(ctx, shortOutput, sortedNames)
114
// printTabular prints the list of actions in tabular format
115
func (c *listCommand) printTabular(ctx *cmd.Context, actions map[string]string, sortedNames []string) error {
118
// To format things into columns.
125
tw := tabwriter.NewWriter(&out, minwidth, tabwidth, padding, padchar, flags)
126
fmt.Fprintf(tw, "%s\t%s\n", "ACTION", "DESCRIPTION")
127
for _, name := range sortedNames {
128
fmt.Fprintf(tw, "%s\t%s\n", name, strings.TrimSpace(actions[name]))
131
return c.out.Write(ctx, string(out.Bytes()))