launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/uniter/jujuc/config-get.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package jujuc 5 6 import ( 7 "launchpad.net/errgo/errors" 8 "launchpad.net/gnuflag" 9 "launchpad.net/juju-core/cmd" 10 ) 11 12 // ConfigGetCommand implements the config-get command. 13 type ConfigGetCommand struct { 14 cmd.CommandBase 15 ctx Context 16 Key string // The key to show. If empty, show all. 17 All bool 18 out cmd.Output 19 } 20 21 func NewConfigGetCommand(ctx Context) cmd.Command { 22 return &ConfigGetCommand{ctx: ctx} 23 } 24 25 func (c *ConfigGetCommand) Info() *cmd.Info { 26 doc := ` 27 When no <key> is supplied, all keys with values or defaults are printed. If 28 --all is set, all known keys are printed; those without defaults or values are 29 reported as null. <key> and --all are mutually exclusive. 30 ` 31 return &cmd.Info{ 32 Name: "config-get", 33 Args: "[<key>]", 34 Purpose: "print service configuration", 35 Doc: doc, 36 } 37 } 38 39 func (c *ConfigGetCommand) SetFlags(f *gnuflag.FlagSet) { 40 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 41 f.BoolVar(&c.All, "a", false, "print all keys") 42 f.BoolVar(&c.All, "all", false, "") 43 } 44 45 func (c *ConfigGetCommand) Init(args []string) error { 46 if args == nil { 47 return nil 48 } 49 c.Key = args[0] 50 if c.Key != "" && c.All { 51 return errors.Newf("cannot use argument --all together with key %q", c.Key) 52 } 53 54 return cmd.CheckEmpty(args[1:]) 55 } 56 57 func (c *ConfigGetCommand) Run(ctx *cmd.Context) error { 58 settings, err := c.ctx.ConfigSettings() 59 if err != nil { 60 return mask(err) 61 } 62 var value interface{} 63 if c.Key == "" { 64 if !c.All { 65 for k, v := range settings { 66 if v == nil { 67 delete(settings, k) 68 } 69 } 70 } 71 value = settings 72 } else { 73 value, _ = settings[c.Key] 74 } 75 return c.out.Write(ctx, value) 76 }