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