-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (173 loc) · 4.81 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"os"
"runtime"
"github.com/urfave/cli"
)
var configFile string
var environList string
var setType string
const (
/* Operation profile type */
YAMLTOOL string = "yaml"
JSONTOOL string = "json"
INITOOL string = "ini"
/*tool name*/
TOOLNAME string = "pscf"
/* commands */
CommandDel string = "del"
CommandGet string = "get"
CommandSet string = "set"
CommandGetKeys string = "getkeys"
CommandIgnore string = "tostr"
CommandFile string = "config"
CommandType string = "type"
CommandVerbose string = "verbose,v"
CommandComment string = "comment"
Version string = "1.0.0"
CurSystem string = runtime.GOOS + " (" + runtime.GOARCH + ")"
)
/* command line flags */
var commandBaseFlags []cli.Flag = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
Destination: &configFile,
},
cli.StringFlag{
Name: CommandDel,
Usage: "Delete a node in the configuration file.(--del node)",
Destination: &environList,
},
cli.StringFlag{
Name: CommandGet,
Usage: "Get the value of the node in the configuration file.(--get node)",
Destination: &environList,
},
cli.StringFlag{
Name: CommandSet,
Usage: "Set the value of the node and add a new node if the node does not exist.(--set node=value)",
Destination: &environList,
},
cli.BoolFlag{
Name: CommandVerbose,
Usage: "Debug mode.(--verbose, -v)",
},
}
var commandYamlFlags []cli.Flag = []cli.Flag{
cli.StringFlag{
Name: CommandIgnore,
Usage: "Force some tags into strings.(--tostr \"yes,no\")",
},
cli.StringFlag{
Name: CommandGetKeys,
Usage: "Get the keys value of the node in the configuration file.(--getkeys node)",
Destination: &environList,
},
cli.BoolFlag{
Name: CommandComment,
Usage: "Load comment enable.(--comment)",
},
}
var commandIniFlags []cli.Flag = []cli.Flag{
cli.StringFlag{
Name: CommandGetKeys,
Usage: "Get the keys value of the node in the configuration file.(--getkeys node)",
Destination: &environList,
},
}
var subCommands []cli.Command = []cli.Command{
{
Name: YAMLTOOL,
Usage: "Provides tools for set, delete, and get operations for a node in YAML format.",
UsageText: "pscf yaml [-c config.yaml] [--set node.node1.node2=value]\n" +
" pscf yaml [-c config.yaml] [--del node.node1.node2]\n" +
" pscf yaml [-c config.yaml] [--get node.node1.node2]\n" +
" pscf yaml [-c config.yaml] [--getkeys node.node1.node2]",
Flags: append(commandBaseFlags, commandYamlFlags...),
Action: func(c *cli.Context) error {
if len(os.Args) < 3 || c.NArg() != 0 {
cli.ShowCommandHelpAndExit(c, YAMLTOOL, -1)
}
err := run(c, YAMLTOOL, os.Args[2:])
if err != nil {
return err
}
return nil
},
},
{
Name: JSONTOOL,
Flags: commandBaseFlags,
Usage: "Provides tools for set, delete, and get operations for a node in JSON format.",
UsageText: "pscf json [-c config.yaml] [--set node.node1.node2=value]\n" +
" pscf json [-c config.yaml] [--del node.node1.node2]\n" +
" pscf json [-c config.yaml] [--get node.node1.node2]",
Action: func(c *cli.Context) error {
if len(os.Args) < 3 || c.NArg() != 0 {
cli.ShowCommandHelpAndExit(c, JSONTOOL, -1)
}
err := run(c, JSONTOOL, os.Args[2:])
if err != nil {
return err
}
return nil
},
},
{
Name: INITOOL,
Usage: "Provides tools for set, delete, and get operations for section or key in INI format.",
Flags: append(commandBaseFlags, commandIniFlags...),
UsageText: "pscf ini [-c config.yaml] [--set section.key=value]\n" +
" pscf ini [-c config.yaml] [--del section.key]\n" +
" pscf ini [-c config.yaml] [--get section.key]\n" +
" pscf ini [-c config.yaml] [--getkeys section]",
Action: func(c *cli.Context) error {
if len(os.Args) < 3 || c.NArg() != 0 {
cli.ShowCommandHelpAndExit(c, INITOOL, -1)
}
err := run(c, INITOOL, os.Args[2:])
if err != nil {
return err
}
return nil
},
},
}
func main() {
/* app settings */
app := cli.NewApp()
app.Name = TOOLNAME
app.Usage = "Processing standardized config file."
app.UsageText = app.Name + " [yaml|json|ini]" + " -h"
app.Copyright = "(c) 2018 LPD"
app.Authors = append(app.Authors, cli.Author{
Name: "liupeidong",
Email: "liupeidong0620@163.com",
})
app.Version = Version + " " + CurSystem + " Build Date " + app.Compiled.String()
app.Commands = subCommands
/* app exec func*/
app.Action = func(c *cli.Context) error {
if len(os.Args) < 2 || c.NArg() != 0 {
cli.ShowAppHelpAndExit(c, -1)
}
/*
switch c.Args().Get(1) {
case YAMLTOOL:
case JSONTOOL:
case INITOOL:
default:
cli.ShowAppHelpAndExit(c, -1)
}*/
return nil
}
/* app start */
err := app.Run(os.Args)
if err != nil {
fmt.Println("Err: ", err)
os.Exit(-1)
}
os.Exit(0)
}