github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/action/list.go (about) 1 // Copyright 2014, 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package action 5 6 import ( 7 "github.com/juju/cmd" 8 errors "github.com/juju/errors" 9 "github.com/juju/names" 10 "launchpad.net/gnuflag" 11 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/cmd/modelcmd" 14 ) 15 16 func NewListCommand() cmd.Command { 17 return modelcmd.Wrap(&listCommand{}) 18 } 19 20 // listCommand lists actions defined by the charm of a given service. 21 type listCommand struct { 22 ActionCommandBase 23 serviceTag names.ServiceTag 24 fullSchema bool 25 out cmd.Output 26 } 27 28 const listDoc = ` 29 List the actions available to run on the target service, with a short 30 description. To show the full schema for the actions, use --schema. 31 32 For more information, see also the 'run-ation' command, which executes actions. 33 ` 34 35 // Set up the output. 36 func (c *listCommand) SetFlags(f *gnuflag.FlagSet) { 37 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 38 f.BoolVar(&c.fullSchema, "schema", false, "display the full action schema") 39 } 40 41 func (c *listCommand) Info() *cmd.Info { 42 return &cmd.Info{ 43 Name: "list-actions", 44 Args: "<service name>", 45 Purpose: "list actions defined for a service", 46 Doc: listDoc, 47 Aliases: []string{"actions"}, 48 } 49 } 50 51 // Init validates the service name and any other options. 52 func (c *listCommand) Init(args []string) error { 53 switch len(args) { 54 case 0: 55 return errors.New("no service name specified") 56 case 1: 57 svcName := args[0] 58 if !names.IsValidService(svcName) { 59 return errors.Errorf("invalid service name %q", svcName) 60 } 61 c.serviceTag = names.NewServiceTag(svcName) 62 return nil 63 default: 64 return cmd.CheckEmpty(args[1:]) 65 } 66 } 67 68 // Run grabs the Actions spec from the api. It then sets up a sensible 69 // output format for the map. 70 func (c *listCommand) Run(ctx *cmd.Context) error { 71 api, err := c.NewActionAPIClient() 72 if err != nil { 73 return err 74 } 75 defer api.Close() 76 77 actions, err := api.ServiceCharmActions(params.Entity{c.serviceTag.String()}) 78 if err != nil { 79 return err 80 } 81 82 output := actions.ActionSpecs 83 if len(output) == 0 { 84 return c.out.Write(ctx, "No actions defined for "+c.serviceTag.Id()) 85 } 86 87 if c.fullSchema { 88 verboseSpecs := make(map[string]interface{}) 89 for k, v := range output { 90 verboseSpecs[k] = v.Params 91 } 92 93 return c.out.Write(ctx, verboseSpecs) 94 } 95 96 shortOutput := make(map[string]string) 97 for name, action := range actions.ActionSpecs { 98 shortOutput[name] = action.Description 99 if shortOutput[name] == "" { 100 shortOutput[name] = "No description" 101 } 102 } 103 return c.out.Write(ctx, shortOutput) 104 }