-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
exec.go
171 lines (141 loc) · 4.72 KB
/
exec.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
package worker
import (
"context"
"sync/atomic"
errorsv3 "gopkg.in/hedzr/errors.v3"
logz "github.com/hedzr/logg/slog"
"github.com/hedzr/cmdr/v2/cli"
)
func (w *workerS) exec(ctx *parseCtx) (err error) {
lastCmd := ctx.LastCmd()
w.parsingCtx = ctx // save ctx for later, OnAction might need it.
if w.Store().Has("app.force-default-action") {
ctx.forceDefaultAction = w.Store().MustBool("app.force-default-action", false)
}
var deferActions func(errInvoked error)
if deferActions, err = w.beforeExec(ctx, lastCmd); err != nil {
return
}
defer func() {
deferActions(err) // err must be delayed caught here
if err = w.afterExec(ctx, lastCmd); err == nil {
c := context.Background()
err = w.finalActions(c, ctx, lastCmd)
}
}()
if !ctx.forceDefaultAction && lastCmd.HasOnAction() {
logz.Verbose("invoke action of cmd, with args", "cmd", lastCmd, "args", ctx.positionalArgs)
err = lastCmd.Invoke(ctx.positionalArgs)
if !w.errIsSignalFallback(err) {
return
}
ctx.forceDefaultAction, err = true, nil
}
handled, err1 := w.handleActions(ctx)
// for k, action := range w.actions {
// if k&w.actionsMatched != 0 {
// logz.Verbose("Invoking worker.actionsMatched", "hit-action", k, "actions", w.Actions())
// err, handled = action(ctx, lastCmd), true
// break
// }
// }
if handled || !w.errIsSignalOrNil(err1) {
return
}
// if ctx.helpScreen {
// err = w.onPrintHelpScreen(ctx, lastCmd)
// return
// }
if ctx.forceDefaultAction {
err = w.onDefaultAction(ctx, lastCmd)
return
}
logz.Verbose("no onAction associate to cmd", "cmd", lastCmd)
err = w.onPrintHelpScreen(ctx, lastCmd)
return
}
func (w *workerS) handleActions(ctx *parseCtx) (handled bool, err error) {
lastCmd := ctx.LastCmd()
for k, action := range w.actions {
if k&w.actionsMatched != 0 {
logz.Verbose("Invoking worker.actionsMatched", "hit-action", k, "actions", w.Actions())
err, handled = action(ctx, lastCmd), true
break
}
}
return
}
type onAction func(ctx *parseCtx, lastCmd *cli.Command) (err error)
func (w *workerS) beforeExec(ctx *parseCtx, lastCmd *cli.Command) (deferActions func(errInvoked error), err error) {
err = w.checkRequiredFlags(ctx, lastCmd)
deferActions = func(error) {}
if err != nil {
return
}
if lastCmd != w.root.Command {
deferActions, err = w.root.RunPreActions(lastCmd, ctx.positionalArgs)
}
return
}
func (w *workerS) checkRequiredFlags(ctx *parseCtx, lastCmd *cli.Command) (err error) { //nolint:revive
lastCmd.WalkBackwards(func(cc *cli.Command, ff *cli.Flag, index, count, level int) {
if ff != nil {
if ff.Required() && ff.GetTriggeredTimes() < 0 {
err = ErrRequiredFlag.FormatWith(ff, lastCmd)
_, _, _, _, _ = ctx, cc, index, count, level
return
}
}
})
return
}
func (w *workerS) afterExec(ctx *parseCtx, lastCmd *cli.Command) (err error) { //nolint:revive
_, _ = ctx, lastCmd
return
}
func (w *workerS) finalActions(ctx context.Context, pc *parseCtx, lastCmd *cli.Command) (err error) { //nolint:revive,unparam
_, _ = pc, lastCmd
err = w.writeBackToLoaders(ctx)
return
}
func (w *workerS) onPrintHelpScreen(ctx *parseCtx, lastCmd *cli.Command) (err error) { //nolint:unparam
(&helpPrinter{w: w}).Print(ctx, lastCmd)
return
}
func (w *workerS) onDefaultAction(ctx *parseCtx, lastCmd *cli.Command) (err error) { //nolint:unparam
(&helpPrinter{w: w, debugMatches: true}).Print(ctx, lastCmd)
return
}
func (w *workerS) onPassThruCharMatched(ctx *parseCtx) (err error) { //nolint:unparam
if atomic.CompareAndSwapInt32(&ctx.passThruMatched, 0, 1) {
atomic.StoreInt32(&ctx.passThruMatched, int32(ctx.i))
}
return
}
func (w *workerS) onSingleHyphenMatched(ctx *parseCtx) (err error) { //nolint:unparam
if atomic.CompareAndSwapInt32(&ctx.singleHyphenMatched, 0, 1) {
atomic.StoreInt32(&ctx.singleHyphenMatched, int32(ctx.i))
}
return
}
func (w *workerS) onUnknownCommandMatched(ctx *parseCtx) (err error) {
logz.Warn("UNKNOWN <mark>Command</mark> FOUND", "arg", ctx.arg)
err = ErrUnmatchedCommand.FormatWith(ctx.arg, ctx.LastCmd())
return
}
func (w *workerS) onUnknownFlagMatched(ctx *parseCtx) (err error) {
logz.Warn("UNKNOWN <mark>Flag</mark> FOUND", "arg", ctx.arg)
err = ErrUnmatchedFlag.FormatWith(ctx.arg, ctx.LastCmd())
return
}
//
//
//
var (
// ErrUnmatchedCommand means Unmatched command found. It's just a state, not a real error, see [cli.Config.UnmatchedAsError]
ErrUnmatchedCommand = errorsv3.New("UNKNOWN Command FOUND: %q | cmd=%v")
// ErrUnmatchedFlag means Unmatched flag found. It's just a state, not a real error, see [cli.Config.UnmatchedAsError]
ErrUnmatchedFlag = errorsv3.New("UNKNOWN Flag FOUND: %q | cmd=%v")
// ErrRequiredFlag means required flag must be set explicitly
ErrRequiredFlag = errorsv3.New("Flag %q is REQUIRED | cmd=%v")
)