github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/names" 10 "launchpad.net/gnuflag" 11 12 "github.com/juju/juju/apiserver/params" 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 &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 storageKindString(k params.StorageKind) string { 65 switch k { 66 case params.StorageKindBlock: 67 return "block" 68 case params.StorageKindFilesystem: 69 return "filesystem" 70 } 71 return "unknown" 72 } 73 74 func (c *StorageGetCommand) Run(ctx *cmd.Context) error { 75 storage, err := c.ctx.Storage(c.storageTag) 76 if err != nil { 77 return errors.Trace(err) 78 } 79 values := map[string]interface{}{ 80 "kind": storage.Kind().String(), 81 "location": storage.Location(), 82 } 83 if c.key == "" { 84 return c.out.Write(ctx, values) 85 } 86 if value, ok := values[c.key]; ok { 87 return c.out.Write(ctx, value) 88 } 89 return errors.Errorf("invalid storage attribute %q", c.key) 90 }