github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/storage-get.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 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 jujucmd "github.com/juju/juju/cmd" 13 ) 14 15 // StorageGetCommand implements the storage-get command. 16 type StorageGetCommand struct { 17 cmd.CommandBase 18 ctx Context 19 storageTag names.StorageTag 20 storageTagProxy gnuflag.Value 21 key string 22 out cmd.Output 23 } 24 25 func NewStorageGetCommand(ctx Context) (cmd.Command, error) { 26 c := &StorageGetCommand{ctx: ctx} 27 sV, err := newStorageIdValue(ctx, &c.storageTag) 28 if err != nil { 29 return nil, errors.Trace(err) 30 } 31 c.storageTagProxy = sV 32 return c, nil 33 } 34 35 func (c *StorageGetCommand) Info() *cmd.Info { 36 doc := ` 37 When no <key> is supplied, all keys values are printed. 38 ` 39 return jujucmd.Info(&cmd.Info{ 40 Name: "storage-get", 41 Args: "[<key>]", 42 Purpose: "print information for storage instance with specified id", 43 Doc: doc, 44 }) 45 } 46 47 func (c *StorageGetCommand) SetFlags(f *gnuflag.FlagSet) { 48 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 49 f.Var(c.storageTagProxy, "s", "specify a storage instance by id") 50 } 51 52 func (c *StorageGetCommand) Init(args []string) error { 53 if c.storageTag == (names.StorageTag{}) { 54 return errors.New("no storage instance specified") 55 } 56 key, err := cmd.ZeroOrOneArgs(args) 57 if err != nil { 58 return err 59 } 60 c.key = key 61 return nil 62 } 63 64 func (c *StorageGetCommand) Run(ctx *cmd.Context) error { 65 storage, err := c.ctx.Storage(c.storageTag) 66 if err != nil { 67 return errors.Trace(err) 68 } 69 values := map[string]interface{}{ 70 "kind": storage.Kind().String(), 71 "location": storage.Location(), 72 } 73 if c.key == "" { 74 return c.out.Write(ctx, values) 75 } 76 if value, ok := values[c.key]; ok { 77 return c.out.Write(ctx, value) 78 } 79 return errors.Errorf("invalid storage attribute %q", c.key) 80 }