-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlitecmd.go
234 lines (206 loc) · 7.62 KB
/
litecmd.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
package main
import (
"context"
"os"
"path"
"github.com/hedzr/cmdr/v2"
"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/dir"
"github.com/hedzr/is/exec"
"github.com/hedzr/is/term/color"
"github.com/hedzr/store"
)
// onEvalJumpSubCommands querys shell scripts in EXT directory
// (typically it is `/usr/local/lib/<app-name>/ext/`) and build
// as subcommands dynamically.
//
// In this demo app, we looks up `./ci/pkg/usr.local.lib.large-app/ext`
// with hard-code.
//
// EXT directory: see the [cmdr.UsrLibDir()] for its location.
func onEvalJumpSubCommands(ctx context.Context, c cli.Cmd) (it cli.EvalIterator, err error) {
files := make(map[string]*liteCmdS)
pos := 0
var keys []string
baseDir := cmdr.UsrLibDir()
if !dir.FileExists(baseDir) {
baseDir = path.Join("ci", "pkg", "usr.local.lib", c.App().Name(), "ext")
} else {
baseDir = path.Join(baseDir, "ext")
}
if !dir.FileExists(baseDir) {
return
}
err = dir.ForFile(baseDir, func(depth int, dirName string, fi os.DirEntry) (stop bool, err error) {
if fi.Name()[0] == '.' {
return
}
key := path.Join(dirName, fi.Name())
files[key] = &liteCmdS{dirName: dirName, fi: fi, depth: depth, owner: c}
keys = append(keys, key)
return
})
it = func(context.Context) (bo cli.Cmd, hasNext bool, err error) {
if pos < len(keys) {
key := keys[pos]
bo = files[key]
pos++
hasNext = pos < len(keys)
}
return
}
return
}
type liteCmdS struct {
dirName string
fi os.DirEntry
depth int
owner cli.Cmd
group string
hitTitle string
hitTimes int
}
var _ cli.Cmd = (*liteCmdS)(nil)
// var _ cli.CmdPriv = (*liteCmdS)(nil)
func (s *liteCmdS) name() string { return s.fi.Name() }
func (s *liteCmdS) String() string { return path.Join(s.dirName, s.name()) }
func (s *liteCmdS) GetDottedPath() string { return cli.DottedPath(s) }
func (s *liteCmdS) GetTitleName() string { return s.name() }
func (s *liteCmdS) GetTitleNamesArray() []string { return []string{s.name()} }
func (s *liteCmdS) GetTitleNames() string { return s.name() }
func (s *liteCmdS) App() cli.App { return nil }
func (s *liteCmdS) Set() store.Store { return s.Root().App().Store() }
func (s *liteCmdS) Store() store.Store { return cmdr.Store() }
func (s *liteCmdS) OwnerIsValid() bool {
if s.OwnerIsNotNil() {
if cx, ok := s.owner.(*liteCmdS); ok {
return cx != s
}
}
return false
}
func (s *liteCmdS) OwnerIsNil() bool { return s.owner == nil }
func (s *liteCmdS) OwnerIsNotNil() bool { return s.owner != nil }
func (s *liteCmdS) OwnerCmd() cli.Cmd { return s.owner }
func (s *liteCmdS) SetOwnerCmd(c cli.Cmd) { s.owner = c }
func (s *liteCmdS) Root() *cli.RootCommand { return s.owner.Root() }
func (s *liteCmdS) SetRoot(*cli.RootCommand) {}
func (s *liteCmdS) OwnerOrParent() cli.BacktraceableMin { return s.owner.(cli.Backtraceable) }
func (s *liteCmdS) Name() string { return s.name() }
func (s *liteCmdS) SetName(string) {}
func (s *liteCmdS) ShortName() string { return s.name() }
func (s *liteCmdS) ShortNames() []string { return []string{s.name()} }
func (s *liteCmdS) AliasNames() []string { return nil }
func (s *liteCmdS) Desc() string { return s.String() }
func (s *liteCmdS) DescLong() string { return "" }
func (s *liteCmdS) SetDesc(desc string) {}
func (s *liteCmdS) Examples() string { return "" }
func (s *liteCmdS) TailPlaceHolder() string { return "" }
func (s *liteCmdS) GetCommandTitles() string { return s.name() }
func (s *liteCmdS) GroupTitle() string { return cmdr.RemoveOrderedPrefix(s.SafeGroup()) }
func (s *liteCmdS) GroupHelpTitle() string { return s.GroupTitle() }
func (s *liteCmdS) SafeGroup() string {
if s.group == "" {
return cli.UnsortedGroup
}
return s.group
}
func (s *liteCmdS) AllGroupKeys(chooseFlag, sort bool) []string { return nil }
func (s *liteCmdS) Hidden() bool { return false }
func (s *liteCmdS) VendorHidden() bool { return false }
func (s *liteCmdS) Deprecated() string { return "" }
func (s *liteCmdS) DeprecatedHelpString(trans func(ss string, clr color.Color) string, clr, clrDefault color.Color) (hs, plain string) {
return
}
func (s *liteCmdS) CountOfCommands() int { return 0 }
func (s *liteCmdS) CommandsInGroup(groupTitle string) (list []cli.Cmd) { return nil }
func (s *liteCmdS) FlagsInGroup(groupTitle string) (list []*cli.Flag) { return nil }
func (s *liteCmdS) SubCommands() []*cli.CmdS { return nil }
func (s *liteCmdS) Flags() []*cli.Flag { return nil }
func (s *liteCmdS) HeadLikeFlag() *cli.Flag { return nil }
func (s *liteCmdS) SetHeadLikeFlag(*cli.Flag) {}
func (s *liteCmdS) SetHitTitle(title string) {
s.hitTitle = title
s.hitTimes++
}
func (s *liteCmdS) HitTitle() string { return s.hitTitle }
func (s *liteCmdS) HitTimes() int { return s.hitTimes }
func (s *liteCmdS) RedirectTo() (dottedPath string) { return }
func (s *liteCmdS) SetRedirectTo(dottedPath string) {}
func (s *liteCmdS) CanInvoke() bool {
return s.fi.Type().IsRegular()
}
func (s *liteCmdS) Invoke(ctx context.Context, args []string) (err error) {
fullPath := path.Join(s.dirName, s.name())
err = exec.Run("sh", "-c", fullPath)
return
}
func (s *liteCmdS) OnEvalSubcommands() cli.OnEvaluateSubCommands {
return nil
}
func (s *liteCmdS) OnEvalSubcommandsOnce() cli.OnEvaluateSubCommands {
return nil
}
func (s *liteCmdS) OnEvalSubcommandsOnceInvoked() bool {
return false
}
func (s *liteCmdS) OnEvalSubcommandsOnceCache() []cli.Cmd {
return nil
}
func (s *liteCmdS) OnEvalSubcommandsOnceSetCache(list []cli.Cmd) {
}
func (s *liteCmdS) OnEvalFlags() cli.OnEvaluateFlags {
return nil
}
func (s *liteCmdS) OnEvalFlagsOnce() cli.OnEvaluateFlags {
return nil
}
func (s *liteCmdS) OnEvalFlagsOnceInvoked() bool {
return false
}
func (s *liteCmdS) OnEvalFlagsOnceCache() []*cli.Flag {
return nil
}
func (s *liteCmdS) OnEvalFlagsOnceSetCache(list []*cli.Flag) {
}
func (s *liteCmdS) findSubCommandIn(ctx context.Context, cc cli.Cmd, children []cli.Cmd, longName string, wide bool) (res cli.Cmd) {
return
}
func (s *liteCmdS) findFlagIn(ctx context.Context, cc cli.Cmd, children []cli.Cmd, longName string, wide bool) (res *cli.Flag) {
return
}
func (s *liteCmdS) findFlagBackwardsIn(ctx context.Context, cc cli.Cmd, children []cli.Cmd, longName string) (res *cli.Flag) {
return
}
func (s *liteCmdS) partialMatchFlag(context.Context, string, bool, bool, map[string]*cli.Flag) (matched, remains string, ff *cli.Flag, err error) {
return
}
func (s *liteCmdS) Match(ctx context.Context, title string) (short bool, cc cli.Cmd) {
return
}
func (s *liteCmdS) TryOnMatched(position int, hitState *cli.MatchState) (handled bool, err error) {
return
}
func (s *liteCmdS) MatchFlag(ctx context.Context, vp *cli.FlagValuePkg) (ff *cli.Flag, err error) { //nolint:revive
return
}
func (s *liteCmdS) FindSubCommand(ctx context.Context, longName string, wide bool) (res cli.Cmd) {
return
}
func (s *liteCmdS) FindFlagBackwards(ctx context.Context, longName string) (res *cli.Flag) {
return
}
func (s *liteCmdS) ForeachFlags(context.Context, func(f *cli.Flag) (stop bool)) (stop bool) {
return
}
func (s *liteCmdS) Walk(ctx context.Context, cb cli.WalkCB) {
return
}
func (s *liteCmdS) WalkGrouped(ctx context.Context, cb cli.WalkGroupedCB) {
return
}
func (s *liteCmdS) WalkBackwardsCtx(ctx context.Context, cb cli.WalkBackwardsCB, pc *cli.WalkBackwardsCtx) {
return
}
func (s *liteCmdS) WalkEverything(ctx context.Context, cb cli.WalkEverythingCB) {
}