-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.go
More file actions
executable file
·345 lines (311 loc) · 7.8 KB
/
cli.go
File metadata and controls
executable file
·345 lines (311 loc) · 7.8 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
340
341
342
343
344
345
package g
import (
"sort"
"strings"
"github.com/integrii/flaggy"
"github.com/jedib0t/go-pretty/table"
"github.com/spf13/cast"
)
// CliArr is the array of CliSC
var CliArr = []*CliSC{}
var AllScs = []*CliSC{}
var CliObj = &CliSC{}
// CliSC represents a CLI subcommand
type CliSC struct {
Name string
Description string
AdditionalHelpPrepend string
AdditionalHelpAppend string
Singular string
Sc *flaggy.Subcommand
ScUsed string // override sc used
Vals map[string]interface{}
PosFlags []Flag
Flags []Flag
CrudFlags []Flag
ExecProcess func(c *CliSC) (bool, error)
SubComs []*CliSC
CrudOps []string
CrudPK []string
InclAccID bool
ExecuteWithoutFlags bool
parentSc *CliSC
}
// Flag represents a CLI Flag
type Flag struct {
Type string
ShortName string
Name string
Description string
Required bool
}
// Add adds to the main CLI array
func (c *CliSC) Add() *CliSC {
CliArr = append(CliArr, c)
sort.Slice(CliArr, func(i, j int) bool { return CliArr[i].Name < CliArr[j].Name })
return c
}
// Make makes the subcommand properties
func (c *CliSC) Make() *CliSC {
c.Sc = flaggy.NewSubcommand(c.Name)
c.Sc.Description = c.Description
c.Sc.AdditionalHelpPrepend = c.AdditionalHelpPrepend
c.Sc.AdditionalHelpAppend = c.AdditionalHelpAppend
AllScs = append(AllScs, c)
c.Vals = map[string]interface{}{}
for _, f := range c.Flags {
switch f.Type {
case "bool":
val := false
c.Sc.Bool(&val, f.ShortName, f.Name, f.Description)
c.Vals[f.Name] = &val
case "slice":
val := []string{}
c.Sc.StringSlice(&val, f.ShortName, f.Name, f.Description)
c.Vals[f.Name] = &val
default:
val := ""
c.Sc.String(&val, f.ShortName, f.Name, f.Description)
c.Vals[f.Name] = &val
}
}
for i, f := range c.PosFlags {
val := ""
c.Sc.AddPositionalValue(&val, f.Name, i+1, f.Required, f.Description)
c.Vals[f.Name] = &val
}
for _, op := range c.CrudOps {
switch op {
case "add":
addSC := flaggy.NewSubcommand("add")
addSC.Description = F("add a new %s", c.Singular)
for _, f := range c.CrudFlags {
val := new(string)
addSC.String(val, "", f.Name, f.Description)
c.Vals["add="+f.Name] = val
}
c.Sc.AttachSubcommand(addSC, 1)
case "update":
updateSC := flaggy.NewSubcommand("update")
updateSC.Description = F("update existing %s", c.Singular)
id := ""
updateSC.String(&id, "", "id", F("update the %s with the provided id", c.Singular))
c.Vals["update=id"] = &id
if c.InclAccID {
accID := ""
updateSC.String(&accID, "", "account_id", F("update the %s with the provided account id", c.Singular))
c.Vals["update=account_id"] = &accID
}
for _, f := range c.CrudFlags {
if _, ok := c.Vals["update="+f.Name]; ok {
continue
}
switch f.Type {
case "bool":
val := false
updateSC.Bool(&val, f.ShortName, f.Name, f.Description)
c.Vals[f.Name] = &val
case "slice":
val := []string{}
updateSC.StringSlice(&val, f.ShortName, f.Name, f.Description)
c.Vals[f.Name] = &val
default:
val := ""
updateSC.String(&val, f.ShortName, f.Name, f.Description)
c.Vals[f.Name] = &val
}
}
c.Sc.AttachSubcommand(updateSC, 1)
case "list":
listSC := flaggy.NewSubcommand("list")
listSC.Description = F("list %ss", c.Singular)
limit := ""
listSC.String(&limit, "l", "limit", "limit records. (max: 500)")
c.Vals["list=limit"] = &limit
last := ""
listSC.String(&last, "", "last", "show the N most recent records. (max: 100)")
c.Vals["list=last"] = &last
for _, f := range c.CrudFlags {
val := ""
listSC.String(&val, "", f.Name, F("filter results by %s", f.Name))
c.Vals["list="+f.Name] = &val
}
c.Sc.AttachSubcommand(listSC, 1)
case "remove":
id := ""
removeSC := flaggy.NewSubcommand("remove")
removeSC.Description = F("remove a %s", c.Singular)
removeSC.String(&id, "", "id", F("removes the %s with the provided id", c.Singular))
c.Vals["remove=id"] = &id
if c.InclAccID {
accID := ""
removeSC.String(&accID, "", "account_id", F("removes the %s with the provided account id", c.Singular))
c.Vals["remove=account_id"] = &accID
}
c.Sc.AttachSubcommand(removeSC, 1)
default:
id := ""
newSC := flaggy.NewSubcommand(op)
newSC.Description = F("%s existing %s", op, c.Singular)
newSC.String(&id, "", "id", F("%s the %s with the provided id", op, c.Singular))
c.Vals[op+"=id"] = &id
if c.InclAccID {
accID := ""
newSC.String(&accID, "", "account_id", F("%s the %s with the provided account id", op, c.Singular))
c.Vals[op+"=account_id"] = &accID
}
c.Sc.AttachSubcommand(newSC, 1)
}
}
for _, s := range c.SubComs {
s.parentSc = c
s.Make()
c.Sc.AttachSubcommand(s.Sc, 1)
}
return c
}
// CliProcess processes the cli objects
func CliProcess() (bool, error) {
allBlanks := func(m map[string]interface{}) bool {
if len(m) == 0 { // no flags
return false
}
blankCnt := 0
for k, v := range m {
switch v.(type) {
case *bool:
b := *v.(*bool)
if !b {
blankCnt++
}
m[k] = b
case *int:
i := *v.(*int)
if i == 0 {
blankCnt++
}
m[k] = i
case *[]string:
ss := *v.(*[]string)
if len(ss) == 0 {
blankCnt++
}
m[k] = ss
default:
s := *v.(*string)
if s == "" {
blankCnt++
}
m[k] = s
}
}
return blankCnt == len(m)
}
for _, cObj := range AllScs {
if cObj.Sc.Used && cObj.ExecProcess != nil {
// Debug("used -> %s", cObj.Name)
for _, sc2 := range cObj.Sc.Subcommands {
if sc2.Used {
for _, scCli := range cObj.SubComs {
if scCli.Name == sc2.Name {
for k, v := range scCli.Vals {
cObj.Vals[k] = v
}
}
}
_, ok := cObj.Vals["list=limit"]
if sc2.Name == "list" && ok {
defLimit := "20"
cObj.Vals["list=limit"] = &defLimit
}
}
}
requiredFlag := 0
for _, f := range cObj.Flags {
if f.Required {
requiredFlag++
}
}
if !cObj.ExecuteWithoutFlags {
if requiredFlag > 0 && allBlanks(cObj.Vals) {
return false, nil
} else if len(cObj.Flags) > 0 && allBlanks(cObj.Vals) {
return false, nil
}
}
// delete blanks, prepare values
for k, v := range cObj.Vals {
switch v.(type) {
case []string:
default:
val := cast.ToString(v)
if val == "" {
delete(cObj.Vals, k)
continue
}
}
keyArr := strings.Split(k, "=")
if len(keyArr) == 2 {
cObj.Vals[keyArr[1]] = v
delete(cObj.Vals, k)
k = keyArr[1]
}
// try int
valInt, err := cast.ToIntE(v)
if err == nil {
cObj.Vals[k] = valInt
}
}
CliObj = cObj
ok, err := cObj.ExecProcess(cObj)
if err != nil {
err = Error(err)
}
return ok, err
}
}
return false, nil
}
// ListWhere get the list where fields/values
func (c *CliSC) ListWhere() map[string]interface{} {
where := map[string]interface{}{}
for _, flag := range c.CrudFlags {
if v, ok := c.Vals[flag.Name]; ok {
where[flag.Name] = v
}
}
return where
}
// UsedSC returns the name of the used subcommand
func (c *CliSC) UsedSC() string {
if c == nil || c.Sc == nil {
return ""
}
if c.ScUsed != "" {
return c.ScUsed
}
for _, sc2 := range c.Sc.Subcommands {
if !sc2.Used {
continue
}
return sc2.Name
}
return ""
}
func PrettyTable(header []string, rows [][]any) (output string) {
T := table.NewWriter()
headerValues := make(table.Row, len(header))
for i, name := range header {
headerValues[i] = name
}
T.AppendHeader(headerValues)
for _, row := range rows {
for i, val := range row {
if val == nil {
row[i] = ""
}
}
T.AppendRow(row)
}
return T.Render()
}