github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/action/status.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 "launchpad.net/gnuflag" 10 11 "github.com/juju/juju/apiserver/params" 12 ) 13 14 // StatusCommand shows the status of an Action by ID. 15 type StatusCommand struct { 16 ActionCommandBase 17 out cmd.Output 18 requestedId string 19 } 20 21 const statusDoc = ` 22 Show the status of an Action by its identifier. 23 ` 24 25 // Set up the YAML output. 26 func (c *StatusCommand) SetFlags(f *gnuflag.FlagSet) { 27 c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{ 28 "yaml": cmd.FormatYaml, 29 }) 30 } 31 32 func (c *StatusCommand) Info() *cmd.Info { 33 return &cmd.Info{ 34 Name: "status", 35 Args: "<action identifier>", 36 Purpose: "WIP: show results of an action by identifier", 37 Doc: statusDoc, 38 } 39 } 40 41 func (c *StatusCommand) Init(args []string) error { 42 switch len(args) { 43 case 0: 44 return errors.New("no action identifier specified") 45 case 1: 46 c.requestedId = args[0] 47 return nil 48 default: 49 return cmd.CheckEmpty(args[1:]) 50 } 51 } 52 53 func (c *StatusCommand) Run(ctx *cmd.Context) error { 54 api, err := c.NewActionAPIClient() 55 if err != nil { 56 return err 57 } 58 defer api.Close() 59 60 actionTag, err := getActionTagFromPrefix(api, c.requestedId) 61 if err != nil { 62 return err 63 } 64 65 actions, err := api.Actions(params.Entities{ 66 Entities: []params.Entity{{actionTag.String()}}, 67 }) 68 if err != nil { 69 return err 70 } 71 actionResults := actions.Results 72 numActionResults := len(actionResults) 73 if numActionResults == 0 { 74 return errors.Errorf("identifier %q matched action %q, but found no results", c.requestedId, actionTag.Id()) 75 } 76 if numActionResults != 1 { 77 return errors.Errorf("too many results for action %s", actionTag.Id()) 78 } 79 80 result := actionResults[0] 81 if result.Error != nil { 82 return result.Error 83 } 84 return c.out.Write(ctx, struct { 85 Id string 86 Status string 87 }{ 88 Id: actionTag.Id(), 89 Status: result.Status, 90 }) 91 }