~themue/pyjuju/go-state-remove-relation

« back to all changes in this revision

Viewing changes to cmd/jujuc/server/config-get.go

  • Committer: Frank Mueller
  • Date: 2012-05-04 15:31:48 UTC
  • mfrom: (146.1.6 go)
  • Revision ID: themue@gmail.com-20120504153148-y71r3htfmku0y2ls
Commit after merge.

Show diffs side-by-side

added added

removed removed

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