-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
369 lines (327 loc) · 10.3 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package command
import (
"context"
"errors"
"fmt"
"io"
"reflect"
"slices"
"strings"
)
var (
ErrInvalidCommand = errors.New("invalid command")
ErrCommandAlreadyHasParent = errors.New("command already has a parent")
)
// HelpConfig is a configuration added to every executed command, for automatic help screen generation.
type HelpConfig struct {
Help bool `inherited:"true" desc:"Show this help screen and exit."`
}
type Action interface {
Run(context.Context) error
}
type ActionFunc func(context.Context) error
func (i ActionFunc) Run(ctx context.Context) error {
if i != nil {
return i(ctx)
} else {
return nil
}
}
type PreRunHook interface {
PreRun(context.Context) error
}
type PreRunHookFunc func(context.Context) error
func (i PreRunHookFunc) PreRun(ctx context.Context) error {
if i != nil {
return i(ctx)
} else {
return nil
}
}
type PostRunHook interface {
PostRun(context.Context, error, ExitCode) error
}
type PostRunHookFunc func(context.Context, error, ExitCode) error
func (i PostRunHookFunc) PostRun(ctx context.Context, err error, exitCode ExitCode) error {
if i != nil {
return i(ctx, err, exitCode)
} else {
return nil
}
}
// Command is a command instance, created by [New] and can be composed with more Command instances to form a CLI command
// hierarchy.
type Command struct {
name string
shortDescription string
longDescription string
preRunHooks []PreRunHook
postRunHooks []PostRunHook
action Action
flags *flagSet
parent *Command
subCommands []*Command
HelpConfig *HelpConfig
}
// MustNew creates a new command using [New], but will panic if it returns an error.
//
//goland:noinspection GoUnusedExportedFunction
func MustNew(name, shortDescription, longDescription string, action Action, hooks []any, subCommands ...*Command) *Command {
cmd, err := New(name, shortDescription, longDescription, action, hooks, subCommands...)
if err != nil {
panic(err)
}
return cmd
}
// New creates a new command with the given name, short & long descriptions, and the given executor. The executor object
// is also scanned for configuration structs via reflection.
func New(name, shortDescription, longDescription string, action Action, hooks []any, subCommands ...*Command) (*Command, error) {
if name == "" {
return nil, fmt.Errorf("%w: empty name", ErrInvalidCommand)
} else if shortDescription == "" {
return nil, fmt.Errorf("%w: empty short description", ErrInvalidCommand)
}
// Translate the any-based hooks list into pre-run and post-run hooks
// Fail on any hook that doesn't implement at least one of them
var preRunHooks []PreRunHook
var postRunHooks []PostRunHook
for i, hook := range hooks {
var pre, post bool
if preRunHook, ok := hook.(PreRunHook); ok {
preRunHooks = append(preRunHooks, preRunHook)
pre = true
}
if postRunHook, ok := hook.(PostRunHook); ok {
postRunHooks = append(postRunHooks, postRunHook)
post = true
}
if !pre && !post {
return nil, fmt.Errorf("%w: hook %d (%T) is neither a PreRunHook nor a PostRunHook", ErrInvalidCommand, i, hook)
}
}
// Create the command instance
cmd := &Command{
name: name,
shortDescription: shortDescription,
longDescription: longDescription,
action: action,
preRunHooks: preRunHooks,
postRunHooks: postRunHooks,
HelpConfig: &HelpConfig{},
}
// Set nil parent
if err := cmd.setParent(nil); err != nil {
return nil, fmt.Errorf("failed creating command '%s': %w", name, err)
}
// Add sub-commands
for _, subCmd := range subCommands {
if err := cmd.AddSubCommand(subCmd); err != nil {
return nil, fmt.Errorf("%w: failed adding sub-command '%s' to '%s': %w", ErrInvalidCommand, subCmd.name, name, err)
}
}
return cmd, nil
}
// setParent updates the parent command of this command.
func (c *Command) setParent(parent *Command) error {
// Determine the parent flagSet, if any
var parentFlags *flagSet
if parent != nil {
parentFlags = parent.flags
} else if parentFlagSet, err := newFlagSet(nil, reflect.ValueOf(c).Elem().FieldByName("HelpConfig")); err != nil {
return fmt.Errorf("failed creating Help flag set: %w", err)
} else {
parentFlags = parentFlagSet
}
// Create the flag-set
var configObjects []reflect.Value
if c.action != nil {
configObjects = append(configObjects, reflect.ValueOf(c.action))
}
for _, hook := range c.preRunHooks {
configObjects = append(configObjects, reflect.ValueOf(hook))
}
for _, hook := range c.postRunHooks {
hv := reflect.ValueOf(hook)
if !slices.ContainsFunc(configObjects, func(v reflect.Value) bool { return v.Interface() == hv.Interface() }) {
configObjects = append(configObjects, hv)
}
}
if fs, err := newFlagSet(parentFlags, configObjects...); err != nil {
return fmt.Errorf("failed creating flag-set for command '%s': %w", c.name, err)
} else {
c.parent = parent
c.flags = fs
}
return nil
}
// AddSubCommand will add the given command as a sub-command of this command. An error is returned if the given command
// already has another parent.
func (c *Command) AddSubCommand(cmd *Command) error {
if cmd.parent != nil {
return fmt.Errorf("%w: %s", ErrCommandAlreadyHasParent, cmd.parent.name)
}
c.subCommands = append(c.subCommands, cmd)
if err := cmd.setParent(c); err != nil {
return fmt.Errorf("failed setting parent for command '%s': %w", cmd.name, err)
}
return nil
}
// inferCommandAndArgs takes the given CLI arguments, and splits them into flags, positional arguments, but most
// importantly, understands which command the user is trying to invoke. This is done by comparing given positional
// arguments to the current command hierarchy, and removing positional arguments that denote sub-commands.
//
// For example, assuming the following command line is given:
//
// cmd1 -flag1 sub1 something -flag2=1 sub2 -- sub3 -flag3 a b c
//
// And the command hierarchy is: cmd1 -> sub1 -> sub2 -> sub3
//
// The returned values would be:
// - flags: [-flag1, -flag2=1]: no "-flag3" because it's after the "--" separator
// - positionals: [something, sub3, a, b, c]: no "cmd1", "sub1" and "sub2" as they are commands in the hierarchy
// - command: sub2 (since it's the last valid command before the "--" which signals positional args only)
func (c *Command) inferCommandAndArgs(args []string) (flags, positionals []string, current *Command) {
current = c
onlyPositionalArgs := false
for _, arg := range args {
if onlyPositionalArgs {
positionals = append(positionals, arg)
} else if arg == "--" {
onlyPositionalArgs = true
} else if strings.HasPrefix(arg, "-") {
flags = append(flags, arg)
} else {
found := false
for _, subCmd := range current.subCommands {
if subCmd.name == arg {
current = subCmd
found = true
break
}
}
if !found {
positionals = append(positionals, arg)
}
}
}
return
}
// getFullName returns the names of all commands in this command's hierarchy, starting from the root, all the way to
// this command.
//
// For example, assuming the following command hierarchy:
//
// cmd1 -> sub1 -> sub2 -> sub3
//
// This function would return "cmd1 sub1" for the "sub1" command.
func (c *Command) getFullName() string {
var fullName string
for cmd := c; cmd != nil; cmd = cmd.parent {
if fullName != "" {
fullName = " " + fullName
}
fullName = cmd.name + fullName
}
return fullName
}
// getChain returns the chain of commands for this command, starting from the root, all the way to this command.
func (c *Command) getChain() []*Command {
var chain []*Command
for cmd := c; cmd != nil; cmd = cmd.parent {
chain = append([]*Command{cmd}, chain...)
}
return chain
}
func (c *Command) PrintHelp(w io.Writer, width int) error {
ww, err := NewWrappingWriter(width)
if err != nil {
return err
}
prefix4 := strings.Repeat(" ", 4)
prefix8 := strings.Repeat(" ", 8)
fullName := c.getFullName()
// Command name & short description
if c.shortDescription != "" {
_, _ = fmt.Fprint(ww, fullName)
_, _ = fmt.Fprint(ww, ": ")
_ = ww.SetLinePrefix(prefix4)
_, _ = fmt.Fprintln(ww, c.shortDescription)
_ = ww.SetLinePrefix("")
} else {
_, _ = fmt.Fprintln(ww, fullName)
}
_, _ = fmt.Fprintln(ww)
// Long description if we have one
if c.longDescription != "" {
_, _ = fmt.Fprint(ww, "Description: ")
_ = ww.SetLinePrefix(prefix4)
_, _ = fmt.Fprintln(ww, c.longDescription)
_ = ww.SetLinePrefix("")
_, _ = fmt.Fprintln(ww)
}
// Usage line
_, _ = fmt.Fprintln(ww, "Usage:")
_ = ww.SetLinePrefix(prefix4)
_, _ = fmt.Fprint(ww, fullName+" ")
_ = ww.SetLinePrefix(prefix8)
if err := c.flags.printFlagsSingleLine(ww); err != nil {
return err
}
_ = ww.SetLinePrefix("")
_, _ = fmt.Fprintln(ww)
_, _ = fmt.Fprintln(ww)
// Flags
if c.flags.hasFlags() {
_, _ = fmt.Fprintln(ww, "Flags:")
_ = ww.SetLinePrefix(prefix4)
if err := c.flags.printFlagsMultiLine(ww, prefix4); err != nil {
return err
}
_ = ww.SetLinePrefix("")
_, _ = fmt.Fprintln(ww)
}
// Sub-commands
if len(c.subCommands) > 0 {
_, _ = fmt.Fprintln(ww, "Available sub-commands:")
lenOfLongestSubCommand := 0
for _, subCmd := range c.subCommands {
if len(subCmd.name) > lenOfLongestSubCommand {
lenOfLongestSubCommand = len(subCmd.name)
}
}
subCommandNameDescSpacing := 10 - lenOfLongestSubCommand%10
subCommandDescriptionCol := lenOfLongestSubCommand + subCommandNameDescSpacing
for _, subCmd := range c.subCommands {
_ = ww.SetLinePrefix(prefix4)
_, _ = fmt.Fprint(ww, subCmd.name)
_, _ = fmt.Fprint(ww, strings.Repeat(" ", subCommandDescriptionCol-len(subCmd.name)))
_ = ww.SetLinePrefix(strings.Repeat(" ", len(prefix4)+subCommandDescriptionCol))
_, _ = fmt.Fprintln(ww, subCmd.shortDescription)
}
_, _ = fmt.Fprintln(ww)
}
if _, err = w.Write([]byte(ww.String())); err != nil {
return err
}
return nil
}
func (c *Command) PrintUsageLine(w io.Writer, width int) error {
ww, err := NewWrappingWriter(width)
if err != nil {
return err
}
prefix4 := strings.Repeat(" ", 4)
fullName := c.getFullName()
_, _ = fmt.Fprint(ww, "Usage: ")
_ = ww.SetLinePrefix(prefix4)
_, _ = fmt.Fprint(ww, fullName+" ")
if err := c.flags.printFlagsSingleLine(ww); err != nil {
return err
}
_ = ww.SetLinePrefix("")
_, _ = fmt.Fprintln(ww)
if _, err = w.Write([]byte(ww.String())); err != nil {
return err
}
return nil
}