-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
98 lines (92 loc) · 2.16 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
86
87
88
89
90
91
92
93
94
95
96
97
98
package internal
import (
"fmt"
"github.com/cheerego/uci/app/cli/internal/config"
"github.com/cheerego/uci/pkg/ini"
"github.com/cheerego/uci/pkg/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
"strings"
)
func (u *Uci) Config() *cobra.Command {
return &cobra.Command{
Use: "config",
Long: "manager the uci configuration files",
Example: `
uci config set <key>=<value> [<key>=<value> ...]
uci config get [<key> [<key> ...]]
uci config delete <key> [<key> ...]
uci config list [--json]
`,
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
}
func (u *Uci) ConfigSet() *cobra.Command {
return &cobra.Command{
Use: "set",
Short: "set uci config item",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
newIni := ini.NewIni(config.UciConfigPath())
for _, arg := range args {
splits := strings.SplitN(arg, "=", 2)
newIni.Write(splits[0], splits[1])
}
return nil
},
}
}
func (u *Uci) ConfigGet() *cobra.Command {
return &cobra.Command{
Use: "get [<key> [<key> ...]]",
Short: "get uci config item",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
m := config.Map()
if item, ok := m[args[0]]; ok {
value, err := item.Value()
if err != nil {
return err
}
fmt.Printf("%s=%s\n", args[0], value)
} else {
fmt.Printf("%s=%s\n", args[0], "")
}
return nil
},
}
}
func (u *Uci) ConfigList() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "list uci config items",
RunE: func(cmd *cobra.Command, args []string) error {
m := config.Map()
for name, item := range m {
value, err := item.Value()
if err != nil {
return err
}
fmt.Printf("%s=%s\n", name, value)
}
return nil
},
}
}
func (u *Uci) ConfigDelete() *cobra.Command {
return &cobra.Command{
Use: "delete",
Short: "delete uci config item",
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
log.L().Info("stop uci uci-uci-runner", zap.Any("args", args))
},
}
}