github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/action/defined.go (about) 1 // Copyright 2014 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 ) 14 15 // DefinedCommand lists actions defined by the charm of a given service. 16 type DefinedCommand struct { 17 ActionCommandBase 18 serviceTag names.ServiceTag 19 fullSchema bool 20 out cmd.Output 21 } 22 23 const definedDoc = ` 24 Show the actions available to run on the target service, with a short 25 description. To show the full schema for the actions, use --schema. 26 27 For more information, see also the 'do' subcommand, which executes actions. 28 ` 29 30 // Set up the YAML output. 31 func (c *DefinedCommand) SetFlags(f *gnuflag.FlagSet) { 32 // TODO(binary132) add json output? 33 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 34 f.BoolVar(&c.fullSchema, "schema", false, "display the full action schema") 35 } 36 37 func (c *DefinedCommand) Info() *cmd.Info { 38 return &cmd.Info{ 39 Name: "defined", 40 Args: "<service name>", 41 Purpose: "WIP: show actions defined for a service", 42 Doc: definedDoc, 43 } 44 } 45 46 // Init validates the service name and any other options. 47 func (c *DefinedCommand) Init(args []string) error { 48 switch len(args) { 49 case 0: 50 return errors.New("no service name specified") 51 case 1: 52 svcName := args[0] 53 if !names.IsValidService(svcName) { 54 return errors.Errorf("invalid service name %q", svcName) 55 } 56 c.serviceTag = names.NewServiceTag(svcName) 57 return nil 58 default: 59 return cmd.CheckEmpty(args[1:]) 60 } 61 } 62 63 // Run grabs the Actions spec from the api. It then sets up a sensible 64 // output format for the map. 65 func (c *DefinedCommand) Run(ctx *cmd.Context) error { 66 api, err := c.NewActionAPIClient() 67 if err != nil { 68 return err 69 } 70 defer api.Close() 71 72 actions, err := api.ServiceCharmActions(params.Entity{c.serviceTag.String()}) 73 if err != nil { 74 return err 75 } 76 77 output := actions.ActionSpecs 78 if len(output) == 0 { 79 return c.out.Write(ctx, "No actions defined for "+c.serviceTag.Id()) 80 } 81 82 if c.fullSchema { 83 return c.out.Write(ctx, output) 84 } 85 86 shortOutput := make(map[string]string) 87 for name, action := range actions.ActionSpecs { 88 shortOutput[name] = action.Description 89 if shortOutput[name] == "" { 90 shortOutput[name] = "No description" 91 } 92 } 93 return c.out.Write(ctx, shortOutput) 94 }