forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (70 loc) · 1.55 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cmd
import (
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/goto/salt/cmdx"
"github.com/spf13/cobra"
)
type Config struct {
Host string `mapstructure:"host"`
}
func LoadConfig() (*Config, error) {
var config Config
cfg := cmdx.SetConfig("shield")
err := cfg.Load(&config)
return &config, err
}
func configCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config <command>",
Short: "Manage client configurations",
Example: heredoc.Doc(`
$ shield config init
$ shield config list`),
}
cmd.AddCommand(configInitCommand())
cmd.AddCommand(configListCommand())
return cmd
}
func configInitCommand() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Initialize a new client configuration",
Example: heredoc.Doc(`
$ shield config init
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
cfg := cmdx.SetConfig("shield")
if err := cfg.Init(&Config{}); err != nil {
return err
}
fmt.Printf("config created: %v\n", cfg.File())
return nil
},
}
}
func configListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List client configuration settings",
Example: heredoc.Doc(`
$ shield config list
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
cfg := cmdx.SetConfig("shield")
data, err := cfg.Read()
if err != nil {
return ErrClientConfigNotFound
}
fmt.Println(data)
return nil
},
}
return cmd
}