-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathargs.go
More file actions
411 lines (337 loc) · 7.74 KB
/
args.go
File metadata and controls
411 lines (337 loc) · 7.74 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
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
package extcap
import (
"fmt"
"reflect"
"strings"
)
type Arg interface {
IsSet() bool
setID(int)
marshal(w *strings.Builder)
addFlag(*CaptureArgs)
}
type id int
func (v *id) setID(i int) {
*v = id(i)
}
/*type value[T int | uint | float64 | string | bool] struct {
val T
isSet bool
}
func (v *value[T]) set(val T) {
v.val = val
v.isSet = true
}
func (v *value[T]) getValue() (T, bool) {
return v.val, v.isSet
}*/
// common type for all numeric types: integer, unsigned integer, float
type numericArg[T int | uint | float64 /*, F cli.IntFlag*/] struct {
id
Name string
Display string
Tooltip string
Required bool
Group string
Default T
Range Range[T]
val *T
ca *CaptureArgs
}
func (o *numericArg[T]) IsSet() bool {
return o.ca.Exist(o.Name)
}
func (o *numericArg[T /*, F*/]) marshal(w *strings.Builder) {
if o.Default != 0 {
f := "{default=%d}"
if _, ok := any(o).(*FloatArg); ok {
f = "{default=%f}"
}
fmt.Fprintf(w, f, o.Default)
}
if o.Range.Min != o.Range.Max {
fmt.Fprintf(w, "{range=%v,%v}", o.Range.Min, o.Range.Max)
}
}
func (o *numericArg[T]) addFlag(ca *CaptureArgs) {
var val any
o.ca = ca
switch f := any(o).(type) {
case *IntArg:
val = ca.fs.Int(f.Name, f.Default, f.Display)
o.val = val.(*T)
case *UintArg:
val = ca.fs.Uint(f.Name, f.Default, f.Display)
o.val = val.(*T)
case *FloatArg:
val = ca.fs.Float64(f.Name, f.Default, f.Display)
o.val = val.(*T)
}
}
// Value returns value of argument
func (o *numericArg[T]) Value() T {
if o.val == nil {
return 0
}
return *(o.val)
}
type Range[T int | uint | float64] struct {
Min T
Max T
}
// Integer argument
type IntArg = numericArg[int /*, cli.IntFlag*/]
type UintArg = numericArg[uint]
type FloatArg = numericArg[float64]
// String
type StringArg struct {
id
Name string
Display string
Tooltip string
Required bool
Group string
Default string
Validator string
Placeholder string
val string
ca *CaptureArgs
}
func (o *StringArg) IsSet() bool {
return o.ca.Exist(o.Name)
}
func (o *StringArg) marshal(w *strings.Builder) {
if o.Default != "" {
fmt.Fprintf(w, "{default=%s}", o.Default)
}
if o.Placeholder != "" {
fmt.Fprintf(w, "{placeholder=%s}", o.Placeholder)
}
if o.Validator != "" {
fmt.Fprintf(w, "{validation=%s}", o.Validator)
}
}
func (o *StringArg) addFlag(ca *CaptureArgs) {
o.ca = ca
ca.fs.StringVar(&o.val, o.Name, o.Default, o.Display)
}
// Value returns value of argument
func (o *StringArg) Value() string {
return o.val
}
// Password
type PasswordArg struct {
id
Name string
Display string
Tooltip string
Required bool
Group string
Default string
Placeholder string
val string
ca *CaptureArgs
}
func (o *PasswordArg) IsSet() bool {
return o.ca.Exist(o.Name)
}
func (o *PasswordArg) marshal(w *strings.Builder) {
if o.Default != "" {
fmt.Fprintf(w, "{default=%s}", o.Default)
}
if o.Placeholder != "" {
fmt.Fprintf(w, "{placeholder=%s}", o.Placeholder)
}
}
func (o *PasswordArg) addFlag(ca *CaptureArgs) {
o.ca = ca
ca.fs.StringVar(&o.val, o.Name, o.Default, o.Display)
}
// Value returns value of argument
func (o *PasswordArg) Value() string {
return o.val
}
// Bool
type BoolArg struct {
id
Name string
Display string
Tooltip string
Required bool
Group string
Default bool
val bool
ca *CaptureArgs
}
func (o *BoolArg) IsSet() bool {
return o.ca.Exist(o.Name)
}
func (o *BoolArg) marshal(w *strings.Builder) {
fmt.Fprintf(w, "{default=%t}", o.Default)
}
func (o *BoolArg) addFlag(ca *CaptureArgs) {
o.ca = ca
ca.fs.BoolVar(&o.val, o.Name, o.Default, o.Display)
}
// Value returns value of argument
func (o *BoolArg) Value() bool {
return o.val
}
// FileSelector
type FileSelectArg struct {
id
Name string
Display string
Tooltip string
Required bool
Group string
Default string
Exist bool
//FileExt string
val string
ca *CaptureArgs
}
func (o *FileSelectArg) IsSet() bool {
return o.ca.Exist(o.Name)
}
func (o *FileSelectArg) marshal(w *strings.Builder) {
if o.Default != "" {
fmt.Fprintf(w, "{default=%s}", o.Default)
}
fmt.Fprintf(w, "{mustexist=%t}", o.Exist)
// if o.FileExt != "" {
// fmt.Fprintf(w, "{fileext=%s}", o.FileExt)
// }
}
func (o *FileSelectArg) addFlag(ca *CaptureArgs) {
o.ca = ca
ca.fs.StringVar(&o.val, o.Name, o.Default, o.Display)
}
// Value returns value of argument
func (o *FileSelectArg) Value() string {
return o.val
}
type (
selectorType struct{}
editSelectorType struct{}
radioSelectorType struct{}
multiCheckType struct{}
)
// common type for all type where the user may choose from one or more options
type selectionOptType[T selectorType | editSelectorType | radioSelectorType | multiCheckType] struct {
id
Name string
Display string
Tooltip string
Required bool
Group string
Default string
Values []Value
val string
ca *CaptureArgs
}
func (o *selectionOptType[_]) IsSet() bool {
return o.ca.Exist(o.Name)
}
/*func (o *selectionOptType[T]) flag() cli.Flag {
var expectedVals []string
for _, v := range o.Values {
expectedVals = append(expectedVals, v.Name)
}
o.f = &cli.StringFlag{
Name: o.Name,
Usage: o.Display,
Action: func(ctx *cli.Context, actual string) error {
if !slices.Contains(expectedVals, actual) {
return fmt.Errorf("Invalid value. Expect [%s] but %s", strings.Join(expectedVals, "|"), actual)
}
return nil
},
}
return o.f
}*/
func (o *selectionOptType[_]) marshal(w *strings.Builder) {
fmt.Fprintln(w)
// "value {arg=3}{value=if1}{display=Remote1}{default=true}"
for _, v := range o.Values {
fmt.Fprintf(w, "value {arg=%d}{value=%s}{display=%s}{default=%t}\n", o.id, v.Name, v.Display, v.Name == o.Default)
}
}
func (o *selectionOptType[T]) addFlag(ca *CaptureArgs) {
o.ca = ca
switch f := any(o).(type) {
case *SelectorArg:
ca.fs.StringVar(&f.val, f.Name, f.Default, f.Display)
case *EditSelectorArg:
ca.fs.StringVar(&f.val, f.Name, f.Default, f.Display)
case *RadioArg:
ca.fs.StringVar(&f.val, f.Name, f.Default, f.Display)
case *MultiCheckArg:
ca.fs.StringVar(&f.val, f.Name, f.Default, f.Display)
}
}
// Value returns value of argument
func (o *selectionOptType[T]) Value() string {
return o.val
}
type Value struct {
Name string
Display string
}
// Selector
type SelectorArg = selectionOptType[selectorType]
type EditSelectorArg = selectionOptType[editSelectorType]
type RadioArg = selectionOptType[radioSelectorType]
type MultiCheckArg = selectionOptType[multiCheckType]
// argString convert arg into extcap output format
func argString(arg Arg) string {
w := new(strings.Builder)
w.WriteString("arg ")
o := reflect.Indirect(reflect.ValueOf(arg))
id := o.FieldByName("id").Int()
fmt.Fprintf(w, "{number=%d}", id)
name := o.FieldByName("Name").String()
fmt.Fprintf(w, "{call=--%s}", name)
display := o.FieldByName("Display").String()
fmt.Fprintf(w, "{display=%s}", display)
var tt string
switch arg.(type) {
case *IntArg:
tt = "integer"
case *UintArg:
tt = "unsigned"
case *FloatArg:
tt = "double"
case *StringArg:
tt = "string"
case *PasswordArg:
tt = "password"
case *BoolArg:
tt = "boolean"
case *FileSelectArg:
tt = "fileselect"
case *SelectorArg:
tt = "selector"
case *EditSelectorArg:
tt = "editselector"
case *RadioArg:
tt = "radio"
case *MultiCheckArg:
tt = "multicheck"
default:
err := fmt.Errorf("Unknown type: %T", arg)
panic(err)
}
fmt.Fprintf(w, "{type=%s}", tt)
if tooltip := o.FieldByName("Tooltip").String(); tooltip != "" {
fmt.Fprintf(w, "{tooltip=%s}", tooltip)
}
if required := o.FieldByName("Required").Bool(); required {
fmt.Fprint(w, "{required=true}")
}
if group := o.FieldByName("Group").String(); group != "" {
fmt.Fprintf(w, "{group=%s}", group)
}
arg.marshal(w)
return w.String()
}