github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 "launchpad.net/gnuflag" 10 ) 11 12 // StorageGetCommand implements the storage-get command. 13 type StorageGetCommand struct { 14 cmd.CommandBase 15 ctx Context 16 storageInstanceId string 17 keys []string 18 out cmd.Output 19 } 20 21 func NewStorageGetCommand(ctx Context) cmd.Command { 22 return &StorageGetCommand{ctx: ctx} 23 } 24 25 func (c *StorageGetCommand) Info() *cmd.Info { 26 doc := ` 27 When no <key> is supplied, all keys values are printed. 28 ` 29 return &cmd.Info{ 30 Name: "storage-get", 31 Args: "<storageInstanceId> <key> [<key>]*", 32 Purpose: "print information for storage instance with specified id", 33 Doc: doc, 34 } 35 } 36 37 func (c *StorageGetCommand) SetFlags(f *gnuflag.FlagSet) { 38 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 39 } 40 41 func (c *StorageGetCommand) Init(args []string) error { 42 if len(args) < 1 { 43 return errors.New("no storage instance specified") 44 } 45 if len(args) < 2 { 46 return errors.New("no attribute keys specified") 47 } 48 c.storageInstanceId = args[0] 49 c.keys = args[1:] 50 return nil 51 } 52 53 func (c *StorageGetCommand) Run(ctx *cmd.Context) error { 54 storageInstance, ok := c.ctx.StorageInstance(c.storageInstanceId) 55 if !ok { 56 return nil 57 } 58 values := make(map[string]interface{}) 59 var singleValue interface{} 60 for _, key := range c.keys { 61 switch key { 62 case "kind": 63 values[key] = storageInstance.Kind 64 case "location": 65 values[key] = storageInstance.Location 66 default: 67 return errors.Errorf("invalid storage instance key %q", key) 68 } 69 singleValue = values[key] 70 } 71 // For single values with smart formatting, we want just the value printed, 72 // not "key: value". 73 if len(c.keys) == 1 && c.out.Name() == "smart" { 74 return c.out.Write(ctx, singleValue) 75 } 76 return c.out.Write(ctx, values) 77 }