Repository navigation
Expand file tree
/
Copy pathext.go
More file actions
339 lines (278 loc) · 7.76 KB
/
Copy pathext.go
File metadata and controls
339 lines (278 loc) · 7.76 KB
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
package gcli
import (
"context"
"errors"
"fmt"
"strings"
"github.com/gookit/gcli/v3/gevent"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/maputil"
)
// constants for hooks event, there are default allowed event names.
//
// These are aliases for the gevent.* event names. So user code can reference
// them directly from gcli without importing the gevent package.
const (
// EvtAppInitBefore on app init before
EvtAppInitBefore = gevent.OnAppInitBefore
// EvtAppInit on app init after
EvtAppInit = gevent.OnAppInitAfter
// EvtAppExit on app exit
EvtAppExit = gevent.OnAppExit
// EvtAppBindOptsBefore before bind app options
EvtAppBindOptsBefore = gevent.OnAppBindOptsBefore
// EvtAppBindOptsAfter after bind app options
EvtAppBindOptsAfter = gevent.OnAppBindOptsAfter
// EvtAppCmdAdd on app cmd add before
EvtAppCmdAdd = gevent.OnAppCmdAdd
// EvtAppCmdAdded on app cmd added
EvtAppCmdAdded = gevent.OnAppCmdAdded
// EvtAppOptsParsed on app options parsed
EvtAppOptsParsed = gevent.OnAppOptsParsed
// EvtAppHelpBefore before render app help
EvtAppHelpBefore = gevent.OnAppHelpBefore
// EvtAppHelpAfter after render app help
EvtAppHelpAfter = gevent.OnAppHelpAfter
// EvtAppPrepareAfter prepare for run, after the EvtAppOptsParsed
EvtAppPrepareAfter = gevent.OnAppPrepared
EvtAppRunBefore = gevent.OnAppRunBefore
EvtAppRunAfter = gevent.OnAppRunAfter
EvtAppRunError = gevent.OnAppRunError
// EvtCmdInitBefore on cmd init before
EvtCmdInitBefore = gevent.OnCmdInitBefore
// EvtCmdInit on cmd init after
EvtCmdInit = gevent.OnCmdInitAfter
// EvtCmdNotFound app or sub command not found
EvtCmdNotFound = gevent.OnCmdNotFound
// EvtAppCmdNotFound app command not found
EvtAppCmdNotFound = gevent.OnAppCmdNotFound
// EvtCmdSubNotFound sub command not found
EvtCmdSubNotFound = gevent.OnCmdSubNotFound
EvtCmdOptParsed = gevent.OnCmdOptParsed
// EvtCmdRunBefore cmd run
EvtCmdRunBefore = gevent.OnCmdRunBefore
EvtCmdRunAfter = gevent.OnCmdRunAfter
EvtCmdRunError = gevent.OnCmdRunError
// EvtCmdExecBefore cmd exec
EvtCmdExecBefore = gevent.OnCmdExecBefore
EvtCmdExecAfter = gevent.OnCmdExecAfter
EvtCmdExecError = gevent.OnCmdExecError
EvtGOptionsParsed = gevent.OnGlobalOptsParsed
)
// runErr struct
type runErr struct {
code int
err error
}
// newRunErr instance
func newRunErr(code int, err error) errorx.ErrorCoder {
return &runErr{code: code, err: err}
}
// Code for error
func (e *runErr) Code() int { return e.code }
// Error string
func (e *runErr) Error() string {
return fmt.Sprintf("%v (exit code %d)", e.err, e.code)
}
// exitCodeError carries a process exit code chosen by the command itself.
type exitCodeError struct {
code int
err error
}
// NewExitError wraps err so App.Run exits with code instead of the default
// ERR code. The wrapped error text is printed unchanged.
//
// return gcli.NewExitError(130, errors.New("interrupted"))
func NewExitError(code int, err error) error {
if err == nil {
err = errors.New("command failed")
}
return &exitCodeError{code: code, err: err}
}
// Code for error
func (e *exitCodeError) Code() int { return e.code }
// Error string, without decorating the wrapped message
func (e *exitCodeError) Error() string { return e.err.Error() }
// Unwrap allows errors.Is/As on the wrapped error
func (e *exitCodeError) Unwrap() error { return e.err }
// HookFunc definition.
//
// Returns:
// - True for stop continue run.
// - False continue handle next logic.
type HookFunc func(ctx *HookCtx) (stop bool)
/*************************************************************
* simple events manage
*************************************************************/
// Hooks struct. hookManager
type Hooks struct {
// pfxHooks can set prefix match hooks func on running.
// eg: app.run.* => app.run.init, app.run.after
pfxHooks map[string]HookFunc
// Hooks can set some hooks func on running.
hooks map[string]HookFunc
}
// On register event hook by name
func (h *Hooks) On(name string, handler HookFunc) {
if handler == nil {
panicf("event %q handler is nil", name)
}
if strings.HasSuffix(name, ".*") {
if h.pfxHooks == nil {
h.pfxHooks = make(map[string]HookFunc)
}
h.pfxHooks[name[:len(name)-2]] = handler
return
}
if h.hooks == nil {
h.hooks = make(map[string]HookFunc)
}
h.hooks[name] = handler
}
// AddHook register on not exists hook.
func (h *Hooks) AddHook(name string, handler HookFunc) {
if _, ok := h.hooks[name]; !ok {
h.On(name, handler)
}
}
// Fire event by name, allow with event data.
// returns True for stop continue run.
func (h *Hooks) Fire(event string, ctx *HookCtx) (stop bool) {
if fn, ok := h.hooks[event]; ok {
if fn(ctx) {
return true
}
}
// check prefix match hooks
for name, fn := range h.pfxHooks {
if strings.HasPrefix(event, name) {
if fn(ctx) {
return true
}
}
}
// check * hook
if fn, ok := h.hooks["*"]; ok {
if fn(ctx) {
return true
}
}
return false
}
// HasHook registered check.
func (h *Hooks) HasHook(event string) bool {
_, ok := h.hooks[event]
return ok
}
// ResetHooks clear all hooks
func (h *Hooks) ResetHooks() {
h.hooks = nil
h.pfxHooks = nil
}
/*************************************************************
* events context
*************************************************************/
// HookCtx struct
type HookCtx struct {
context.Context
maputil.Data
App *App
Cmd *Command
stop bool // stop to continue handle.
err error // set handle error. TODO
name string
}
func newHookCtx(name string, c *Command, data map[string]any) *HookCtx {
if data == nil {
data = make(maputil.Data)
}
hc := &HookCtx{
name: name,
Cmd: c,
Data: data,
// with empty context
Context: context.Background(),
}
if c != nil {
hc.App = c.app
}
return hc
}
// Err of event
func (hc *HookCtx) Err() error {
return hc.err
}
// Name of event
func (hc *HookCtx) Name() string { return hc.name }
// Stopped value
func (hc *HookCtx) Stopped() bool { return hc.stop }
// SetStop value
func (hc *HookCtx) SetStop(stop bool) bool {
hc.stop = stop
return hc.stop
}
// WithErr value
func (hc *HookCtx) WithErr(err error) *HookCtx {
hc.err = err
return hc
}
// WithData to ctx
func (hc *HookCtx) WithData(data map[string]any) *HookCtx {
if data != nil {
hc.Data = data
}
return hc
}
// WithApp to ctx
func (hc *HookCtx) WithApp(a *App) *HookCtx {
hc.App = a
return hc
}
/*************************************************************
* app/cmd help string-var replacer
*************************************************************/
// HelpVarFormat allow string replace on render help info.
//
// Default support:
//
// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
const HelpVarFormat = "{$%s}"
// HelpReplacer provide string var replace for render help template.
type HelpReplacer struct {
VarOpen, VarClose string
// replaces you can add string-var map for render help info.
replaces map[string]string
}
// AddReplace get command name. AddReplace
func (hv *HelpReplacer) AddReplace(name, value string) {
if hv.replaces == nil {
hv.replaces = make(map[string]string)
}
hv.replaces[name] = value
}
// AddReplaces add multi tpl vars.
func (hv *HelpReplacer) AddReplaces(vars map[string]string) {
for n, v := range vars {
hv.AddReplace(n, v)
}
}
// GetReplace get a help var by name
func (hv *HelpReplacer) GetReplace(name string) string {
return hv.replaces[name]
}
// Replaces get all tpl vars.
func (hv *HelpReplacer) Replaces() map[string]string {
return hv.replaces
}
// ReplacePairs replace string vars in the input text.
func (hv *HelpReplacer) ReplacePairs(input string) string {
// if not use var
if !strings.Contains(input, "{$") {
return input
}
var ss []string
for n, v := range hv.replaces {
ss = append(ss, fmt.Sprintf(HelpVarFormat, n), v)
}
return strings.NewReplacer(ss...).Replace(input)
}