/
config.go
68 lines (59 loc) · 1.65 KB
/
config.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
package main
import (
"errors"
"fmt"
)
// ConfigCommand specifies options for the config subcommand.
type ConfigCommand struct {
System bool `short:"s" long:"system" description:"Modify system level configuration."`
Unset bool `short:"u" long:"unset" description:"Unset key."`
List bool `short:"l" long:"list" description:"List current config values."`
Args struct {
Key string `description:"Configuration key." positional-arg-name:"key"`
Value string `description:"Configuration value. (optional)" positional-arg-name:"value"`
} `positional-args:"yes"`
}
var configCommand ConfigCommand
// Execute handles setting, getting, and listing configuration values.
func (x *ConfigCommand) Execute(args []string) error {
conf, err := NewDefaultConfigClient(&DefaultSystem{})
if err != nil {
return err
}
if configCommand.List {
all, err := conf.GetAll()
if err != nil {
return err
}
for k, v := range all {
fmt.Printf("%s = %s\n", k, v)
}
} else if configCommand.Unset {
err := conf.Unset(configCommand.System, configCommand.Args.Key)
if err != nil {
return err
}
} else {
if len(configCommand.Args.Value) > 0 {
return conf.Set(configCommand.System, configCommand.Args.Key, configCommand.Args.Value)
} else if len(configCommand.Args.Key) > 0 {
val, err := conf.Get(configCommand.Args.Key)
if err != nil {
return err
}
fmt.Println(val)
} else {
return errors.New("Specify a key and optional value to set or pass --help for more information")
}
}
return nil
}
func init() {
_, err := parser.AddCommand("config",
"Set and get configuration.",
"",
&configCommand)
if err != nil {
fmt.Println(err)
}
}