-
Notifications
You must be signed in to change notification settings - Fork 328
/
kingpeon.go
592 lines (572 loc) · 14.5 KB
/
kingpeon.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package kingpeon
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"text/template"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
// type so we can mock out how scripts are executed for testing
// it defaults to syscall.Exec
type runner func(string, []string, []string) error
func runDynamicCommand(run runner, dynamiccommand *DynamicCommand, t *template.Template) error {
buf := bytes.NewBufferString("")
t, err := t.Parse(dynamiccommand.Script)
if err != nil {
return err
}
err = t.Execute(buf, nil)
if err != nil {
return err
}
bin, err := exec.LookPath("sh")
if err != nil {
return err
}
cmd := []string{"sh", "-c", buf.String()}
return run(bin, cmd, os.Environ())
}
// either kingpin.Application or kingpin.CmdClause fit this interface
type kingpinAppOrCommand interface {
Command(string, string) *kingpin.CmdClause
GetCommand(string) *kingpin.CmdClause
}
func lookupCommand(app *kingpin.Application, command *DynamicCommand) *kingpin.CmdClause {
commandWords := strings.Fields(command.Name)
var appOrCmd kingpinAppOrCommand = app
if len(commandWords) > 1 {
for _, name := range commandWords[0 : len(commandWords)-1] {
tmp := appOrCmd.GetCommand(name)
if tmp == nil {
tmp = appOrCmd.Command(name, "")
}
appOrCmd = tmp
}
}
return appOrCmd.Command(commandWords[len(commandWords)-1], command.Help)
}
func RegisterDynamicCommands(app *kingpin.Application, commands DynamicCommands, t *template.Template) error {
return RegisterDynamicCommandsWithRunner(syscall.Exec, app, commands, t)
}
func RegisterDynamicCommandsWithRunner(run runner, app *kingpin.Application, commands DynamicCommands, t *template.Template) error {
args := map[string]interface{}{}
opts := map[string]interface{}{}
t = t.Funcs(map[string]interface{}{
"args": func() map[string]interface{} {
return args
},
"options": func() map[string]interface{} {
return opts
},
})
for _, command := range commands {
cmd := lookupCommand(app, &command)
for _, alt := range command.Aliases {
cmd = cmd.Alias(alt)
}
if command.Default {
cmd = cmd.Default()
}
if command.Hidden {
cmd = cmd.Hidden()
}
for _, realOpt := range command.Options {
opt := realOpt // copy
cmdFlag := cmd.Flag(opt.Name, opt.Help)
if opt.Short != "" {
cmdFlag.Short(rune(opt.Short[0]))
}
if opt.Required {
cmdFlag.Required()
}
if opt.Default != "" {
opts[opt.Name] = opt.Default
}
if opt.Hidden {
cmdFlag.Hidden()
}
switch opt.Type {
case BOOL:
if opt.Repeat {
var val []bool
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).BoolListVar(&val)
} else {
var val bool
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).BoolVar(&val)
}
case COUNTER:
if opt.Repeat {
return fmt.Errorf("`type: COUNTER` and `repeat: true` not supported for %s", opt.Name)
} else {
var val int
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).CounterVar(&val)
}
case ENUM:
if opt.Repeat {
var val []string
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).EnumsVar(&val, opt.Enum...)
} else {
var val string
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).EnumVar(&val, opt.Enum...)
}
case FLOAT32:
if opt.Repeat {
var val []float32
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Float32ListVar(&val)
} else {
var val float32
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Float32Var(&val)
}
case FLOAT64:
if opt.Repeat {
var val []float64
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Float64ListVar(&val)
} else {
var val float64
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Float64Var(&val)
}
case INT8:
if opt.Repeat {
var val []int8
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int8ListVar(&val)
} else {
var val int8
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int8Var(&val)
}
case INT16:
if opt.Repeat {
var val []int16
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int16ListVar(&val)
} else {
var val int16
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int16Var(&val)
}
case INT32:
if opt.Repeat {
var val []int32
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int32ListVar(&val)
} else {
var val int32
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int32Var(&val)
}
case INT64:
if opt.Repeat {
var val []int64
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int64ListVar(&val)
} else {
var val int64
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Int64Var(&val)
}
case INT:
if opt.Repeat {
var val []int
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).IntsVar(&val)
} else {
var val int
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).IntVar(&val)
}
case DEFAULT, STRING:
if opt.Repeat {
var val []string
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).StringsVar(&val)
} else {
var val string
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).StringVar(&val)
}
case STRINGMAP:
if opt.Repeat {
return fmt.Errorf("`type: STRINGMAP` and `repeat: true` not supported for %s", opt.Name)
} else {
val := make(map[string]string)
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).StringMapVar(&val)
}
case UINT8:
if opt.Repeat {
var val []uint8
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint8ListVar(&val)
} else {
var val uint8
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint8Var(&val)
}
case UINT16:
if opt.Repeat {
var val []uint16
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint16ListVar(&val)
} else {
var val uint16
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint16Var(&val)
}
case UINT32:
if opt.Repeat {
var val []uint32
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint32ListVar(&val)
} else {
var val uint32
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint32Var(&val)
}
case UINT64:
if opt.Repeat {
var val []uint64
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint64ListVar(&val)
} else {
var val uint64
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).Uint64Var(&val)
}
case UINT:
if opt.Repeat {
var val []uint
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).UintsVar(&val)
} else {
var val uint
cmdFlag.PreAction(func(_ *kingpin.ParseContext) error {
opts[opt.Name] = val
return nil
}).UintVar(&val)
}
}
}
for _, realArg := range command.Args {
arg := realArg // copy
cmdArg := cmd.Arg(arg.Name, arg.Help)
if arg.Required {
cmdArg.Required()
}
if arg.Default != "" {
args[arg.Name] = arg.Default
}
switch arg.Type {
case BOOL:
if arg.Repeat {
var val []bool
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).BoolListVar(&val)
} else {
var val bool
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).BoolVar(&val)
}
case COUNTER:
if arg.Repeat {
return fmt.Errorf("`type: COUNTER` and `repeat: true` not supported for %s", arg.Name)
} else {
var val int
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).CounterVar(&val)
}
case ENUM:
if arg.Repeat {
var val []string
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).EnumsVar(&val, arg.Enum...)
} else {
var val string
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).EnumVar(&val, arg.Enum...)
}
case FLOAT32:
if arg.Repeat {
var val []float32
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Float32ListVar(&val)
} else {
var val float32
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Float32Var(&val)
}
case FLOAT64:
if arg.Repeat {
var val []float64
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Float64ListVar(&val)
} else {
var val float64
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Float64Var(&val)
}
case INT8:
if arg.Repeat {
var val []int8
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int8ListVar(&val)
} else {
var val int8
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int8Var(&val)
}
case INT16:
if arg.Repeat {
var val []int16
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int16ListVar(&val)
} else {
var val int16
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int16Var(&val)
}
case INT32:
if arg.Repeat {
var val []int32
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int32ListVar(&val)
} else {
var val int32
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int32Var(&val)
}
case INT64:
if arg.Repeat {
var val []int64
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int64ListVar(&val)
} else {
var val int64
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Int64Var(&val)
}
case INT:
if arg.Repeat {
var val []int
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).IntsVar(&val)
} else {
var val int
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).IntVar(&val)
}
case DEFAULT, STRING:
if arg.Repeat {
var val []string
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).StringsVar(&val)
} else {
var val string
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).StringVar(&val)
}
case STRINGMAP:
if arg.Repeat {
return fmt.Errorf("`type: STRINGMAP` and `repeat: true` not supported for %s", arg.Name)
} else {
val := make(map[string]string)
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).StringMapVar(&val)
}
case UINT8:
if arg.Repeat {
var val []uint8
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint8ListVar(&val)
} else {
var val uint8
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint8Var(&val)
}
case UINT16:
if arg.Repeat {
var val []uint16
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint16ListVar(&val)
} else {
var val uint16
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint16Var(&val)
}
case UINT32:
if arg.Repeat {
var val []uint32
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint32ListVar(&val)
} else {
var val uint32
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint32Var(&val)
}
case UINT64:
if arg.Repeat {
var val []uint64
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint64ListVar(&val)
} else {
var val uint64
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).Uint64Var(&val)
}
case UINT:
if arg.Repeat {
var val []uint
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).UintsVar(&val)
} else {
var val uint
cmdArg.PreAction(func(_ *kingpin.ParseContext) error {
args[arg.Name] = val
return nil
}).UintVar(&val)
}
}
}
copy := command
cmd.Action(func(_ *kingpin.ParseContext) error {
return runDynamicCommand(run, ©, t)
})
}
return nil
}