-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
236 lines (196 loc) · 5.85 KB
/
command.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package cli
import (
"errors"
"flag"
"fmt"
"strconv"
"github.com/fatih/color"
)
var bold = color.New(color.Bold)
var green = color.New(color.FgGreen, color.Bold)
var ErrParameterIsNotBool = errors.New("wanted parameter is not a boolean")
var ErrParameterIsNotInt = errors.New("wanted parameter is not an integer")
var ErrParameterIsNotString = errors.New("wanted parameter is not a string")
var ErrCommandHandlerNotFound = errors.New("command's handler function is not found")
var ErrCommandNotFound = errors.New("command is not found in configurator's commands")
var ErrParameterNotFound = errors.New("wanted parameter is not found")
type Command struct {
Name string
Use string
Description string
Parameters []Parameter
Commands Commands
Flagset *flag.FlagSet
handler func(cmd *Command)
}
type Commands []*Command
func NewCommand(name string, use string, description string) *Command {
command := &Command{
Name: name,
Use: use,
Description: description,
}
fs := flag.NewFlagSet(name, flag.ContinueOnError)
fs.Usage = command.Help().ShowHelp
command.Flagset = fs
return command
}
// Do sets the command's handler function
func (c *Command) Do(handler func(cmd *Command)) {
c.handler = handler
}
// AddCommand adds sub commands to the command
func (c *Command) AddCommand(command *Command) *Command {
c.Commands = append(c.Commands, command)
return command
}
// Parse parses the args returns the wanted command
func (c *Command) Parse(args []string) (*Command, error) {
cmd, position, err := c.getCurrentCommand(args)
if err != nil {
return nil, err
}
if len(args) > 0 && cmd != c {
args = args[position:]
}
err = cmd.Flagset.Parse(args)
if err != nil {
return nil, err
}
return cmd, nil
}
// getCurrentCommand returns the current command for example
// "app run concurrent -v"
// concurrent is the current command here
func (c *Command) getCurrentCommand(args []string) (*Command, int, error) {
currentCommand := c
var position int
for _, arg := range args {
if isFlagArg(arg) {
break
}
cmd, exists := currentCommand.FindCommand(arg)
if !exists {
return currentCommand, position, fmt.Errorf("%s %w", arg, ErrCommandNotFound)
}
currentCommand = cmd
position += 1
}
return currentCommand, position, nil
}
// ShowHelp prints the usage of the command
func (c *Command) Help() *Help {
var help Help
help.Usage = green.Sprintf("Usage: %s \n\n", c.Use)
if len(c.Commands) > 0 {
help.Commands += bold.Sprintf("Commands: \n")
for _, command := range c.Commands {
help.Commands += bold.Sprintf("%-20s %s \n", command.Name, command.Use)
}
}
if len(c.Parameters) > 0 {
help.Parameters += bold.Sprintf("Parameters: \n")
for _, parameter := range c.Parameters {
help.Parameters += bold.Sprintf("--%s, -%-10s %-30s %s \n", parameter.Name, parameter.Shortname, parameter.Use, parameter.Description)
}
}
return &help
}
// Run runs the command's handler function
func (c *Command) Run() error {
if c.handler == nil {
return fmt.Errorf("%s %w", c.Name, ErrCommandHandlerNotFound)
}
c.handler(c)
return nil
}
// FindHelp recognizes if any help command is received and returns a help
func (c *Command) FindHelp(args []string) (*Help, error) {
if len(args) == 0 {
return nil, nil
}
var help *Help
if len(args) == 0 && c.handler == nil {
help = c.Help()
}
if len(args) > 0 {
if args[0] == "help" {
if len(args) == 1 {
help = c.Help()
}
if len(args) > 1 {
cmd, _, err := c.getCurrentCommand(args[1:])
if err != nil {
return nil, err
}
help = cmd.Help()
}
}
}
return help, nil
}
// FindCommand searches the sub commands of the command
func (c *Command) FindCommand(name string) (*Command, bool) {
for _, command := range c.Commands {
if command.Name == name {
return command, true
}
}
return nil, false
}
func (c *Command) addParameter(parameter *Parameter) {
c.Parameters = append(c.Parameters, *parameter)
}
// AddBoolParameter sets a bool flag in the command's flagset
func (c *Command) AddBoolParameter(parameter *Parameter, value *bool, defaultValue bool) *Parameter {
c.addParameter(parameter)
for _, alias := range parameter.Aliases() {
c.Flagset.BoolVar(value, alias, defaultValue, parameter.Use)
}
return parameter
}
// AddIntParameter sets an int flag in the command's flagset
func (c *Command) AddIntParameter(parameter *Parameter, value *int, defaultValue int) *Parameter {
c.addParameter(parameter)
for _, alias := range parameter.Aliases() {
c.Flagset.IntVar(value, alias, defaultValue, parameter.Use)
}
return parameter
}
// AddStringParameter sets a string flag in the command's flagset
func (c *Command) AddStringParameter(parameter *Parameter, value *string, defaultValue string) *Parameter {
c.addParameter(parameter)
for _, alias := range parameter.Aliases() {
c.Flagset.StringVar(value, alias, defaultValue, parameter.Use)
}
return parameter
}
// GetBool gets a bool value with the given name from the command's flagset
func (c *Command) GetBool(name string) (bool, error) {
value := c.Flagset.Lookup(name).Value.String()
if value == "" {
return false, ErrParameterNotFound
}
return strconv.ParseBool(value)
}
// GetString gets a string value with the given name from the command's flagset
func (c *Command) GetString(name string) (string, error) {
value := c.Flagset.Lookup(name).Value.String()
if value == "" {
return "", ErrParameterNotFound
}
return value, nil
}
// GetInt gets an int value with the given name from the command's flagset
func (c *Command) GetInt(name string) (int, error) {
value := c.Flagset.Lookup(name).Value.String()
if value == "" {
return 0, ErrParameterNotFound
}
return strconv.Atoi(value)
}
// isFlagArg returns if the given arg is a flag or not
func isFlagArg(arg string) bool {
return ((len(arg) >= 3 && arg[1] == '-') ||
(len(arg) >= 2 && arg[0] == '-' && arg[1] != '-'))
}