-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcmd.go
760 lines (626 loc) · 16.9 KB
/
cmd.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
package gcli
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/gookit/color"
"github.com/gookit/gcli/v3/events"
"github.com/gookit/gcli/v3/gflag"
"github.com/gookit/gcli/v3/helper"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/strutil"
)
// Runner /Executor interface
type Runner interface {
// Run the command
//
// TIP:
// args is the remain arguments after parse flags(options and arguments).
Run(c *Command, args []string) error
}
// RunnerFunc definition
//
// TIP:
//
// args is the remain arguments after parse flags(options and arguments).
type RunnerFunc func(c *Command, args []string) error
// Run implement the Runner interface
func (f RunnerFunc) Run(c *Command, args []string) error {
return f(c, args)
}
const maxFunc = 64
// HandlersChain middleware handlers chain definition
type HandlersChain []RunnerFunc
// Last returns the last handler in the chain. tip: the last handler is the main own.
func (c HandlersChain) Last() RunnerFunc {
length := len(c)
if length > 0 {
return c[length-1]
}
return nil
}
// Command a CLI command structure
type Command struct {
// internal use
base
// Flags cli (options+arguments) parse and manage for the command
gflag.Flags
// Name is the command name.
Name string
// Desc is the command description message.
// Can use string-var in contents, eg: {$cmd}
Desc string
// Aliases is the command name's alias names
Aliases arrutil.Strings
// Category for the command
Category string
// Config func, will call on `initialize`.
//
// - you can config options and other init works
Config func(c *Command)
// Hidden the command on render help
Hidden bool
// --- for middleware ---
// run error
runErr error
// middleware index number
middleIdx int8
// middleware functions
middles HandlersChain
// errorHandler // loop find parent.errorHandler
// path names of the command. 'parent current'
pathNames []string
// command is inject to the App
app *App
root bool // is root command
// Parent parent command
parent *Command
// Subs sub commands of the Command
// NOTICE: if command has been initialized, adding through this field is invalid
Subs []*Command
// module is the name for grouped commands
// subName is the name for grouped commands
// eg: "sys:info" -> module: "sys", subName: "info"
// module, subName string
// Examples some usage example display
Examples string
// Func is the command handler func. Func Runner
//
// TIP:
// func `args` is the remain arguments after parse flags(options and arguments).
Func RunnerFunc
// Help is the long help message text
// Can use string-var in contents, eg: {$cmd}
Help string
// HelpRender custom render cmd help message
HelpRender func(c *Command)
// mark is disabled. if true will skip register to app.
disabled bool
// command is standalone running.
standalone bool
// global option binding on standalone. deny error on repeat run.
gOptBounded bool
}
// NewCommand create a new command instance.
//
// Usage:
//
// cmd := NewCommand("my-cmd", "description")
// // OR with an config func
// cmd := NewCommand("my-cmd", "description", func(c *Command) { ... })
// app.Add(cmd) // OR cmd.AttachTo(app)
func NewCommand(name, desc string, setFn ...func(c *Command)) *Command {
c := &Command{
Name: name,
Desc: desc,
}
// init set name
c.Flags.SetName(name)
// has config func
if len(setFn) > 0 {
c.Config = setFn[0]
}
return c
}
// Init command. only use for tests
func (c *Command) Init() {
c.initialize()
}
// SetFunc Settings command handler func
func (c *Command) SetFunc(fn RunnerFunc) {
c.Func = fn
}
// WithFunc Settings command handler func
func (c *Command) WithFunc(fn RunnerFunc) *Command {
c.Func = fn
return c
}
// WithHidden Settings command is hidden
func (c *Command) WithHidden() *Command {
c.Hidden = true
return c
}
// AttachTo attach the command to CLI application
func (c *Command) AttachTo(app *App) {
app.AddCommand(c)
}
// Disable set cmd is disabled
func (c *Command) Disable() {
c.disabled = true
}
// Visible return cmd is visible
func (c *Command) Visible() bool {
return c.Hidden == false
}
// IsDisabled get cmd is disabled
func (c *Command) IsDisabled() bool {
return c.disabled
}
// IsRunnable reports whether the command can be run; otherwise
// it is a documentation pseudo-command such as import path.
func (c *Command) IsRunnable() bool {
return c.Func != nil
}
// Add one or multi sub-command(s). alias of the AddSubs
func (c *Command) Add(sub *Command, more ...*Command) {
c.AddSubs(sub, more...)
}
// AddSubs add one or multi sub-command(s)
func (c *Command) AddSubs(sub *Command, more ...*Command) {
c.AddCommand(sub)
if len(more) > 0 {
for _, cmd := range more {
c.AddCommand(cmd)
}
}
}
// AddCommand add a sub command
func (c *Command) AddCommand(sub *Command) {
// init command
sub.app = c.app
sub.parent = c
// inherit something from parent command
sub.Ctx = c.Ctx
sub.standalone = c.standalone
// initialize command
c.initialize()
// extend path names from parent
sub.pathNames = c.pathNames[0:]
// do add and init sub command
c.base.addCommand(c.Name, sub)
// update some parser config
sub.Flags.WithConfigFn(gflag.WithIndentLongOpt(c.ParserCfg().IndentLongOpt))
}
// Match sub command by input names
func (c *Command) Match(names []string) *Command {
// ensure is initialized
c.initialize()
if len(names) == 0 { // return self.
return c
}
return c.base.Match(names)
}
// MatchByPath command by path. eg: "top:sub"
func (c *Command) MatchByPath(path string) *Command {
return c.Match(splitPath2names(path))
}
// initialize works for the command
//
// - ctx
// - sub-cmd
func (c *Command) initialize() {
if c.initialized {
return
}
// check command name
cName := c.goodName()
Debugf("initialize the command '%s': init flags, run config func", cName)
c.initialized = true
c.pathNames = append(c.pathNames, cName)
// init base
c.initCommandBase(cName)
c.Fire(events.OnCmdInitBefore, nil)
// init for cmd flags parser
c.Flags.Init(cName)
// load common sub commands
if len(c.Subs) > 0 {
for _, sub := range c.Subs {
c.AddCommand(sub)
}
}
// format description
if len(c.Desc) > 0 {
c.Desc = strutil.UpperFirst(c.Desc)
if strings.Contains(c.Desc, "{$cmd}") {
c.Desc = strings.Replace(c.Desc, "{$cmd}", c.Name, -1)
}
}
// call config func
if c.Config != nil {
c.Config(c)
}
c.Fire(events.OnCmdInitAfter, nil)
}
// init base, ctx
func (c *Command) initCommandBase(cName string) {
Logf(VerbCrazy, "init command c.base for the command: %s", cName)
if c.Hooks == nil {
c.Hooks = &Hooks{}
}
if c.Ctx == nil {
Logf(VerbDebug, "cmd: %s - use the gCtx as command context", cName)
c.Ctx = gCtx
}
binWithPath := c.Ctx.binName + " " + c.Path()
c.initHelpReplacer()
c.AddReplaces(map[string]string{
"cmd": cName,
// binName with command name
"binWithCmd": binWithPath,
// binName with command path
"binWithPath": binWithPath,
// binFile with command
"fullCmd": binWithPath,
})
c.base.cmdNames = make(map[string]int)
c.base.commands = make(map[string]*Command)
// set an default value.
c.base.nameMaxWidth = 12
// c.base.cmdAliases = make(maputil.Aliases)
c.base.cmdAliases = structs.NewAliases(aliasNameCheck)
}
// Next TODO processing, run all middleware handlers
func (c *Command) Next() {
c.middleIdx++
s := int8(len(c.middles))
for ; c.middleIdx < s; c.middleIdx++ {
err := c.middles[c.middleIdx](c, c.RawArgs())
// will abort on error
if err != nil {
c.runErr = err
return
}
}
}
/*************************************************************
* standalone running
*************************************************************/
// MustRun Alone the current command, will panic on error
//
// Usage:
//
// // run with os.Args
// cmd.MustRun(nil)
// cmd.MustRun(os.Args[1:])
// // custom args
// cmd.MustRun([]string{"-a", ...})
func (c *Command) MustRun(args []string) {
if err := c.Run(args); err != nil {
color.Errorln("ERROR:", err.Error())
}
}
// Run standalone running the command
//
// Usage:
//
// // run with os.Args
// cmd.Run(nil)
// cmd.Run(os.Args[1:])
// // custom args
// cmd.Run([]string{"-a", ...})
func (c *Command) Run(args []string) (err error) {
if c.app != nil || c.parent != nil {
return c.innerDispatch(args)
}
// mark is standalone
c.standalone = true
// if not set input args
if args == nil {
args = os.Args[1:]
}
// init the command
c.initialize()
// add default error handler.
if !c.HasHook(events.OnCmdRunError) {
c.On(events.OnCmdRunError, defaultErrHandler)
}
// binding global options
if !c.gOptBounded {
Debugf("cmd: %s - binding global options on standalone mode", c.Name)
gOpts.bindingOpts(&c.Flags)
c.gOptBounded = true
}
// dispatch and parse flags and execute command
return c.innerDispatch(args)
}
/*************************************************************
* command run
*************************************************************/
// dispatch execute the command
func (c *Command) innerDispatch(args []string) (err error) {
// parse command flags
args, err = c.parseOptions(args)
if err != nil {
if err == flag.ErrHelp {
Debugf("cmd: %s - parse opts return flag.ErrHelp, render command help", c.Name)
return c.ShowHelp()
}
Debugf("cmd: %s - command options parse error", c.Name)
color.Error.Tips("option error - %s", err.Error())
return nil
}
// remaining args
if c.standalone {
if gOpts.ShowHelp {
Debugf("cmd: %s - gOpts.ShowHelp is True, render command help", c.Name)
return c.ShowHelp()
}
c.Fire(events.OnGlobalOptsParsed, map[string]any{"args": args})
}
c.Fire(events.OnCmdOptParsed, map[string]any{"args": args})
Debugf("cmd: %s - remaining args on options parsed: %v", c.Name, args)
// find sub command
if len(args) > 0 {
name := args[0]
// ensure is not an option
if name != "" && name[0] != '-' {
name = c.ResolveAlias(name)
// is valid sub command
if sub, has := c.Command(name); has {
// TIP: loop find sub...command and run it.
return sub.innerDispatch(args[1:])
}
// is not a sub command and has no arguments -> error
if !c.HasArguments() {
// fire events
hookData := map[string]any{"name": name, "args": args[1:]}
if c.Fire(events.OnCmdSubNotFound, hookData) {
return
}
if c.Fire(events.OnCmdNotFound, hookData) {
return
}
color.Error.Tips("%s - subcommand '%s' is not found", c.Name, name)
}
}
}
// not set command func and has sub commands.
if c.Func == nil && len(c.commands) > 0 {
Logf(VerbWarn, "cmd: %s - c.Func is empty, but has subcommands, render help", c.Name)
return c.ShowHelp()
}
// do execute current command
return c.doExecute(args)
}
// execute the current command
func (c *Command) innerExecute(args []string, igrErrHelp bool) (err error) {
// parse flags
args, err = c.parseOptions(args)
if err != nil {
// whether ignore flag.ErrHelp error
if igrErrHelp && err == flag.ErrHelp {
err = nil
}
return
}
// do execute command
return c.doExecute(args)
}
// do parse option flags, remaining is cmd args
func (c *Command) parseOptions(args []string) (ss []string, err error) {
// strict format options
if gOpts.strictMode && len(args) > 0 {
args = strictFormatArgs(args)
}
// fix and compatible
// args = moveArgumentsToEnd(args)
// Debugf("cmd: %s - option flags on after format: %v", c.Name, args)
Debugf("cmd: %s - will parse options from args: %v", c.Name, args)
// parse options, don't contains command name.
if err = c.Parse(args); err != nil {
Logf(VerbCrazy, "cmd: %s - parse options, err: <red>%s</>", c.Name, err.Error())
return
}
// remaining args, next use for parse arguments
return c.RawArgs(), nil
}
// prepare: before execute the command
func (c *Command) prepare(_ []string) (status int, err error) {
return
}
type panicErr struct {
val any
}
// Error string
func (p panicErr) Error() string {
return fmt.Sprint(p.val)
}
// do execute the command
func (c *Command) doExecute(args []string) (err error) {
// collect and binding named argument
Debugf("cmd: %s - collect and binding named arguments", c.Name)
if err := c.ParseArgs(args); err != nil {
c.Fire(events.OnCmdRunError, map[string]any{"err": err})
Logf(VerbError, "binding command '%s' arguments err: <red>%s</>", c.Name, err.Error())
return err
}
fnArgs := c.ExtraArgs()
c.Fire(events.OnCmdRunBefore, map[string]any{"args": fnArgs})
// do call command handler func
if c.Func == nil {
Logf(VerbWarn, "the command '%s' no handler func to running", c.Name)
c.Fire(events.OnCmdRunAfter, nil)
return
}
Debugf("cmd: %s - run command func with extra-args %v", c.Name, fnArgs)
// recover panics
if re := recover(); re != nil {
var ok bool
err, ok = re.(error)
if !ok {
err = panicErr{val: re}
}
c.fireAfterExec(err)
return
}
// do run func
err = c.Func(c, fnArgs)
c.fireAfterExec(err)
return
}
func (c *Command) fireAfterExec(err error) {
if err != nil {
c.Fire(events.OnCmdRunError, map[string]any{"err": err})
} else {
c.Fire(events.OnCmdRunAfter, nil)
}
}
/*************************************************************
* parent and subs
*************************************************************/
// Root get root command
func (c *Command) Root() *Command {
if c.parent != nil {
return c.parent.Root()
}
return c
}
// IsRoot command
func (c *Command) IsRoot() bool {
return c.parent == nil
}
// Parent get parent
func (c *Command) Parent() *Command {
return c.parent
}
// SetParent set parent
func (c *Command) SetParent(parent *Command) {
c.parent = parent
}
// ParentName name of the parent command
func (c *Command) ParentName() string {
if c.parent != nil {
return c.parent.Name
}
return ""
}
// Sub get sub command by name. eg "sub"
func (c *Command) Sub(name string) *Command {
return c.GetCommand(name)
}
// SubCommand get sub command by name. eg "sub"
func (c *Command) SubCommand(name string) *Command {
return c.GetCommand(name)
}
// IsSubCommand name check. alias of the HasCommand()
func (c *Command) IsSubCommand(name string) bool {
return c.IsCommand(name)
}
// find sub command by name
// func (c *Command) findSub(name string) *Command {
// if index, ok := c.subName2index[name]; ok {
// return c.Subs[index]
// }
//
// return nil
// }
/*************************************************************
* helper methods
*************************************************************/
// IsStandalone running
func (c *Command) IsStandalone() bool {
return c.standalone
}
// NotStandalone running
func (c *Command) NotStandalone() bool {
return !c.standalone
}
// ID get command ID name.
func (c *Command) goodName() string {
name := strings.Trim(strings.TrimSpace(c.Name), ": ")
if name == "" {
panicf("the command name can not be empty")
}
if !helper.IsGoodCmdName(name) {
panicf("the command name '%s' is invalid, must match: %s", name, helper.RegGoodCmdName)
}
// update name
c.Name = name
return name
}
// Fire event handler by name
func (c *Command) Fire(event string, data map[string]any) (stop bool) {
hookCtx := newHookCtx(event, c, data)
Debugf("cmd: %s - trigger the event: <mga>%s</>", c.Name, event)
// notify all parent commands
p := c.parent
for p != nil {
if p.Hooks.Fire(event, hookCtx) {
return true
}
p = p.parent
}
// notify to app
if c.app != nil && c.app.Hooks.Fire(event, hookCtx) {
return
}
return c.Hooks.Fire(event, hookCtx)
}
// On add hook handler for a hook event
func (c *Command) On(name string, handler HookFunc) {
Debugf("cmd: %s - register hook: <cyan>%s</>", c.Name, name)
if c.Hooks == nil {
c.Hooks = &Hooks{}
}
c.Hooks.On(name, handler)
}
// Copy a new command for current
func (c *Command) Copy() *Command {
nc := *c
// reset some fields
nc.Func = nil
nc.Hooks.ResetHooks() // TODO bug, will clear c.Hooks
return &nc
}
// App returns the CLI application
func (c *Command) App() *App {
return c.app
}
// ID get command ID string. return like "git:branch:create"
func (c *Command) ID() string {
return strings.Join(c.pathNames, CommandSep)
}
// Path get command full path
func (c *Command) Path() string {
return strings.Join(c.pathNames, " ")
}
// PathNames get command path names
func (c *Command) PathNames() []string {
return c.pathNames
}
// NewErr format message and add error to the command
func (c *Command) NewErr(msg string) error { return errors.New(msg) }
// NewErrf format message and add error to the command
func (c *Command) NewErrf(format string, v ...any) error {
return fmt.Errorf(format, v...)
}
// HelpDesc format desc string for render help
func (c *Command) HelpDesc() (desc string) {
if len(c.Desc) == 0 {
return
}
// dump.P(desc)
desc = strutil.UpperFirst(c.Desc)
// contains help var "{$cmd}". replace on here is for 'app help'
if strings.Contains(desc, "{$cmd}") {
desc = strings.Replace(desc, "{$cmd}", color.WrapTag(c.Name, "mga"), -1)
}
return wrapColor2string(desc)
}
// Logf print log message
// func (c *Command) Logf(level uint, format string, v ...any) {
// Logf(level, format, v...)
// }