Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package server | 1 package server |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/gnuflag" | 5 "launchpad.net/gnuflag" |
6 "launchpad.net/juju/go/cmd" | 6 "launchpad.net/juju/go/cmd" |
7 ) | 7 ) |
8 | 8 |
9 // ConfigGetCommand implements the config-get command. | 9 // ConfigGetCommand implements the config-get command. |
10 type ConfigGetCommand struct { | 10 type ConfigGetCommand struct { |
11 » ctx *Context | 11 » *ClientContext |
12 » Key string | 12 » Key string // The key to show. If empty, show all. |
13 } | 13 } |
14 | 14 |
15 func NewConfigGetCommand(ctx *Context) (cmd.Command, error) { | 15 func NewConfigGetCommand(ctx *ClientContext) (cmd.Command, error) { |
16 if ctx.State == nil { | 16 if ctx.State == nil { |
17 return nil, fmt.Errorf("context %s cannot access state", ctx.Id) | 17 return nil, fmt.Errorf("context %s cannot access state", ctx.Id) |
18 } | 18 } |
19 if ctx.LocalUnitName == "" { | 19 if ctx.LocalUnitName == "" { |
20 return nil, fmt.Errorf("context %s is not attached to a unit", c tx.Id) | 20 return nil, fmt.Errorf("context %s is not attached to a unit", c tx.Id) |
21 } | 21 } |
22 » return &ConfigGetCommand{ctx: ctx}, nil | 22 » return &ConfigGetCommand{ClientContext: ctx}, nil |
23 } | 23 } |
24 | 24 |
25 var purpose = "print service configuration" | |
rog
2012/05/03 09:39:49
sorry for not mentioning this before, but is there
fwereade
2012/05/03 10:14:30
Let's say there's no sane reason ;). Done.
| |
26 var doc = "If a key is given, only the value for that key will be printed" | |
27 | |
28 func (c *ConfigGetCommand) Info() *cmd.Info { | 25 func (c *ConfigGetCommand) Info() *cmd.Info { |
29 » return &cmd.Info{"config-get", "[<key>]", purpose, doc} | 26 » return &cmd.Info{ |
27 » » "config-get", "[<key>]", | |
28 » » "print service configuration", | |
29 » » "If a key is given, only the value for that key will be printed. ", | |
30 » } | |
30 } | 31 } |
31 | 32 |
32 func (c *ConfigGetCommand) Init(f *gnuflag.FlagSet, args []string) error { | 33 func (c *ConfigGetCommand) Init(f *gnuflag.FlagSet, args []string) error { |
33 if err := f.Parse(true, args); err != nil { | 34 if err := f.Parse(true, args); err != nil { |
34 return err | 35 return err |
35 } | 36 } |
36 args = f.Args() | 37 args = f.Args() |
37 if args == nil { | 38 if args == nil { |
38 return nil | 39 return nil |
39 } | 40 } |
40 c.Key = args[0] | 41 c.Key = args[0] |
41 return cmd.CheckEmpty(args[1:]) | 42 return cmd.CheckEmpty(args[1:]) |
42 } | 43 } |
43 | 44 |
44 func (c *ConfigGetCommand) Run(ctx *cmd.Context) error { | 45 func (c *ConfigGetCommand) Run(ctx *cmd.Context) error { |
45 » unit, err := c.ctx.State.Unit(c.ctx.LocalUnitName) | 46 » unit, err := c.State.Unit(c.LocalUnitName) |
46 if err != nil { | 47 if err != nil { |
47 return err | 48 return err |
48 } | 49 } |
49 » service, err := c.ctx.State.Service(unit.ServiceName()) | 50 » service, err := c.State.Service(unit.ServiceName()) |
50 if err != nil { | 51 if err != nil { |
51 return err | 52 return err |
52 } | 53 } |
53 conf, err := service.Config() | 54 conf, err := service.Config() |
54 if err != nil { | 55 if err != nil { |
55 return err | 56 return err |
56 } | 57 } |
57 var value interface{} | 58 var value interface{} |
58 if c.Key == "" { | 59 if c.Key == "" { |
59 value = conf.Map() | 60 value = conf.Map() |
60 } else { | 61 } else { |
61 value, _ = conf.Get(c.Key) | 62 value, _ = conf.Get(c.Key) |
62 } | 63 } |
63 // TODO --format ( = "smart") | 64 // TODO --format ( = "smart") |
64 fmt.Fprintln(ctx.Stdout, value) | 65 fmt.Fprintln(ctx.Stdout, value) |
65 return nil | 66 return nil |
66 } | 67 } |
LEFT | RIGHT |