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