github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/storage/show.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package storage 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/gnuflag" 10 "gopkg.in/juju/names.v2" 11 12 "github.com/juju/juju/apiserver/params" 13 jujucmd "github.com/juju/juju/cmd" 14 "github.com/juju/juju/cmd/modelcmd" 15 "github.com/juju/juju/cmd/output" 16 ) 17 18 // NewShowCommand returns a command that shows storage details 19 // on the specified machine 20 func NewShowCommand() cmd.Command { 21 cmd := &showCommand{} 22 cmd.newAPIFunc = func() (StorageShowAPI, error) { 23 return cmd.NewStorageAPI() 24 } 25 return modelcmd.Wrap(cmd) 26 } 27 28 const showCommandDoc = ` 29 Show extended information about storage instances. 30 Storage instances to display are specified by storage ids. 31 Storage ids are positional arguments to the command and do not need to be comma 32 separated when more than one id is desired. 33 34 ` 35 36 // showCommand attempts to release storage instance. 37 type showCommand struct { 38 StorageCommandBase 39 ids []string 40 out cmd.Output 41 newAPIFunc func() (StorageShowAPI, error) 42 } 43 44 // Init implements Command.Init. 45 func (c *showCommand) Init(args []string) (err error) { 46 if len(args) < 1 { 47 return errors.New("must specify storage id(s)") 48 } 49 c.ids = args 50 return nil 51 } 52 53 // Info implements Command.Info. 54 func (c *showCommand) Info() *cmd.Info { 55 return jujucmd.Info(&cmd.Info{ 56 Name: "show-storage", 57 Args: "<storage ID> [...]", 58 Purpose: "Shows storage instance information.", 59 Doc: showCommandDoc, 60 }) 61 } 62 63 // SetFlags implements Command.SetFlags. 64 func (c *showCommand) SetFlags(f *gnuflag.FlagSet) { 65 c.StorageCommandBase.SetFlags(f) 66 c.out.AddFlags(f, "yaml", output.DefaultFormatters) 67 } 68 69 // Run implements Command.Run. 70 func (c *showCommand) Run(ctx *cmd.Context) (err error) { 71 api, err := c.newAPIFunc() 72 if err != nil { 73 return err 74 } 75 defer api.Close() 76 77 tags, err := c.getStorageTags() 78 if err != nil { 79 return err 80 } 81 82 results, err := api.StorageDetails(tags) 83 if err != nil { 84 return err 85 } 86 87 var errs params.ErrorResults 88 var valid []params.StorageDetails 89 for _, result := range results { 90 if result.Error != nil { 91 errs.Results = append(errs.Results, params.ErrorResult{result.Error}) 92 continue 93 } 94 valid = append(valid, *result.Result) 95 } 96 if len(errs.Results) > 0 { 97 return errs.Combine() 98 } 99 100 output, err := formatStorageDetails(valid) 101 if err != nil { 102 return err 103 } 104 return c.out.Write(ctx, output) 105 } 106 107 func (c *showCommand) getStorageTags() ([]names.StorageTag, error) { 108 tags := make([]names.StorageTag, len(c.ids)) 109 for i, id := range c.ids { 110 if !names.IsValidStorage(id) { 111 return nil, errors.Errorf("invalid storage id %v", id) 112 } 113 tags[i] = names.NewStorageTag(id) 114 } 115 return tags, nil 116 } 117 118 // StorageAPI defines the API methods that the storage commands use. 119 type StorageShowAPI interface { 120 Close() error 121 StorageDetails(tags []names.StorageTag) ([]params.StorageDetailsResult, error) 122 }