-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
333 lines (302 loc) · 8.54 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) Christoph Berger. All rights reserved.
// Use of this source code is governed by the BSD (3-Clause)
// License that can be found in the LICENSE.txt file.
//
// This source code may import third-party source code whose
// licenses are provided in the respective license files.
package start
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
flag "github.com/spf13/pflag"
)
// Add adds a command to either the global Commands map, or, if the command has a parent value, to its parent command as a subcommand.
func Add(cmd *Command) error {
return Commands.Add(cmd)
}
// Add for CommandMap adds a command to a list of commands.
func (c *CommandMap) Add(cmd *Command) error {
if cmd == nil {
return errors.New("Add: Parameter cmd must not be nil.")
}
cmd.init()
if cmd.Parent == "" {
// Add a top-level command.
if _, alreadyExists := (*c)[cmd.Name]; alreadyExists {
return errors.New("Add: command " + cmd.Name + " already exists.")
}
(*c)[cmd.Name] = cmd
return nil
}
// Add a child command.
if _, ok := Commands[cmd.Parent]; !ok {
return errors.New("Add: Parent command not found for subcommand " +
cmd.Name + ".")
}
return Commands[cmd.Parent].Add(cmd)
}
// Add for Command adds a subcommand to a command.
func (cmd *Command) Add(subcmd *Command) error {
cmd.init()
subcmd.init()
if _, alreadyExists := (*cmd).children[subcmd.Name]; alreadyExists {
return errors.New("Add: subcommand " + subcmd.Name +
" already exists for command " + cmd.Name + ".")
}
(*cmd).children[subcmd.Name] = subcmd
return nil
}
// Helper functions for External() and Usage():
// errPrintln & errPrintf -> print to stderr
func errPrintln(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
}
func errPrintf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
}
// External defines an external command to execute via os/exec. The external command's name follows Git subcommmand naming convention: "mycmd do" invokes the external command "mycmd-do".
func External() func(cmd *Command) error {
return func(cmd *Command) error {
cmdName := appName() + "-" + cmd.Name
path := filepath.Join(cmd.Path, cmdName)
c := exec.Command(path, rawCmdArgs)
out, err := c.Output()
fmt.Println(string(out))
if err != nil {
exitErr, ok := err.(*exec.ExitError)
if ok {
errPrintln(string(exitErr.Stderr))
} else {
errPrintln(err)
}
}
return err
}
}
// Usage prints a description of the application and the short help string
// of every command, when called with a nil argument.
// When called with a command as parameter, Usage prints this command's
// long help string as well as the short help strings of the available
// subcommands.
// Parse() or Up() must be called before invoking Usage().
func Usage(cmd *Command) error {
if cmd == nil {
applicationUsage()
} else {
err := commandUsage(cmd)
if err != nil {
errPrintln(err)
}
}
errPrintln()
return nil
}
func applicationUsage() {
errPrintln()
errPrintln(filepath.Base(os.Args[0]))
errPrintln()
if len(description) > 0 {
errPrintln(description)
errPrintln()
}
if len(Commands) > 0 {
width := maxCmdNameLen()
errPrintln("Available commands:")
errPrintln()
for _, c := range Commands {
errPrintf("%-*s %s\n", width, c.Name, c.Short)
}
}
globalFlags := checkFlags(nil)
if len(globalFlags) > 0 {
errPrintln("Available global flags:")
flagUsage(nil)
}
errPrintln()
errPrintln("Type help <command> to get help for a specific command.")
errPrintln()
}
func commandUsage(cmd *Command) error {
errPrintln()
if cmd.Parent != "" {
errPrintf("%v ", cmd.Parent)
}
errPrintf("%v\n\n%v\n", cmd.Name, cmd.Long)
if len(cmd.Flags) > 0 {
if err := Parse(); err != nil {
return err
}
errPrintln()
errPrintln("Command-specific flags:")
errPrintln()
flagUsage(cmd.Flags)
}
return nil
}
func flagUsage(flagNames []string) {
flagUsageList := [][]string{}
var flagNamesAndDefault string
var width int
for _, flagName := range flagNames {
flg := flag.Lookup(flagName)
if flg == nil {
panic("Flag '" + flagName + "' does not exist.")
}
flagNamesAndDefault = fmt.Sprintf("-%s, --%s=%s", flg.Shorthand, flagName, flg.Value) // TODO -> pflag specific "Shorthand"
if width < len(flagNamesAndDefault) {
width = len(flagNamesAndDefault)
}
flagUsageList = append(flagUsageList, []string{flagNamesAndDefault, flg.Usage})
}
for _, flg := range flagUsageList {
errPrintf("%-*s %s\n", width, flg[0], flg[1])
}
}
func help(cmd *Command) error {
if len(cmd.Args) == 0 {
applicationUsage()
return nil
}
command := Commands[cmd.Args[0]]
if command == nil {
return errors.New("Unknown command: " + cmd.Args[0])
}
return commandUsage(command)
}
func showVersion(cmd *Command) error {
errPrintln(filepath.Base(os.Args[0]) + " version " + version)
return nil
}
// maxCmdNameLen returns the length of the longest command name.
func maxCmdNameLen() int {
maxLength := 0
for _, cmd := range Commands {
length := len(cmd.Name)
if length > maxLength {
maxLength = length
}
}
return maxLength
}
// init initializes the children map.
// Calling init more than once for the same cmd should be safe.
func (cmd *Command) init() *Command {
if len(cmd.children) == 0 {
cmd.children = make(CommandMap)
}
cmd.registerPrivateFlags()
return cmd
}
// registerPrivateFlags adds the command's flags to the global PrivateFlags map.
func (cmd *Command) registerPrivateFlags() {
for _, f := range cmd.Flags {
privateFlags[f] = true
}
}
// If c is nil, then checkFlags returns all *global* flags.
// If c exists, then checkFlags returns a list of *private* flags that
// c has rejected as not being its own flags.
func checkFlags(c *Command) map[string]bool {
notMyFlags := make(map[string]bool)
// visit all flags that were passed in via command line:
flag.Visit(func(f *flag.Flag) {
isNotMyFlag := true
if c != nil {
for _, myFlag := range c.Flags {
if f.Name == myFlag {
isNotMyFlag = false // yes, f is among my flags
}
}
}
if isNotMyFlag {
for pf := range privateFlags {
if f.Name == pf {
notMyFlags[pf] = true
}
}
}
})
return notMyFlags
}
// readCommand extracts the command (and any subcommand, if applicable) from the
// list of arguments.
// Parameter args is the list of arguments *after* being parsed by flag.Parse().
// The first item of args must be a command name. If that command has
// subcommands defined, the second item must contain the name of a subcommand.
// If any error occurs, readCommand returns an error and a Command calling the
// pre-defined Usage function
func readCommand(args []string) (*Command, error) {
var cmd, subcmd *Command
var ok bool
if len(args) == 0 {
// No command passed in: Print usage.
return &Command{
Cmd: func(cmd *Command) error { return Usage(nil) },
}, nil
}
var name = args[0]
cmd, ok = Commands[name]
if !ok {
// Command not found: Print usage.
return &Command{
Cmd: func(cmd *Command) error { return Usage(nil) },
}, nil
}
// command found. Remove it from the argument list.
args = args[1:]
if len(cmd.children) == 0 {
return cmdWithFlagsChecked(cmd, args)
}
// len (cmd.children > 0)
if len(args) == 0 {
// Subcommands exist but none was not found in args.
// If no main cmd is defined, return an error.
if cmd.Cmd == nil {
return wrongOrMissingSubcommand(cmd)
}
}
// len (cmd.children > 0) && len(args) > 0
var subname = args[0]
subcmd, ok = cmd.children[subname]
if ok {
// subcommand found.
args = args[1:]
cmd = subcmd
} else {
// no subcommand passed in, so cmd should have a Cmd to execute
return wrongOrMissingSubcommand(cmd)
}
return cmdWithFlagsChecked(cmd, args)
}
// Take a *Command and check if any flags were passed in that
// do not belong to the Command. Return either the Command, or
// a Usage command in case unknown flags are found.
func cmdWithFlagsChecked(cmd *Command, args []string) (*Command, error) {
// No subcommands defined. Check the flags and return the command.
cmd.Args = args
notMyFlags := checkFlags(cmd)
s := ""
if len(notMyFlags) > 0 {
if len(notMyFlags) > 1 {
s = "s"
}
errmsg := fmt.Sprintf("Unknown flag%s: %v", s, notMyFlags)
return &Command{
Cmd: Usage,
}, errors.New(errmsg)
}
return cmd, nil
}
// Create a "subcommands required" error and a Usage command.
func wrongOrMissingSubcommand(cmd *Command) (*Command, error) {
errmsg := "Command " + cmd.Name + " requires one of these subcommands:\n"
for _, n := range cmd.children {
errmsg += n.Name + "\n"
}
return &Command{
Cmd: func(cmd *Command) error { return Usage(cmd) },
}, errors.New(errmsg)
}