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