-
Notifications
You must be signed in to change notification settings - Fork 18
/
flags.go
512 lines (417 loc) · 8.43 KB
/
flags.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
package cmd
import (
"fmt"
"net"
"reflect"
"strconv"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// nilValue is returned by a flag when the value is not set
const nilValue = "nil"
type uint8PtrValue struct {
*uint8
}
func (v *uint8PtrValue) Set(val string) error {
r, err := strconv.ParseUint(val, 10, 8)
if err != nil {
return err
}
res := uint8(r)
v.uint8 = &res
return nil
}
func (v *uint8PtrValue) Type() string {
return "uint8"
}
func (v *uint8PtrValue) String() string {
if v.uint8 == nil {
return nilValue
}
return strconv.FormatUint(uint64(*v.uint8), 10)
}
// XXX use reflect to factor those out.
// e.g. getCustomFlag(cmd *cobra.Command, name string, out interface{}) error {}
func getUint8CustomFlag(cmd *cobra.Command, name string) (*uint8PtrValue, error) {
it := cmd.Flags().Lookup(name)
if it != nil {
r := it.Value.(*uint8PtrValue)
if r != nil {
return r, nil
}
}
return nil, fmt.Errorf("unable to get flag %q", name)
}
type int64PtrValue struct {
*int64
}
func (v *int64PtrValue) Set(val string) error {
r, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
v.int64 = &r
return nil
}
func (v *int64PtrValue) Type() string {
return "int64"
}
func (v *int64PtrValue) String() string {
if v.int64 == nil {
return nilValue
}
return strconv.FormatInt(*v.int64, 10)
}
func getInt64CustomFlag(cmd *cobra.Command, name string) (int64PtrValue, error) {
it := cmd.Flags().Lookup(name)
if it != nil {
r := it.Value.(*int64PtrValue)
if r != nil {
return *r, nil
}
}
return int64PtrValue{}, fmt.Errorf("unable to get flag %q", name)
}
// EXO API flags
// uuid flag
type uuid struct {
UUID **egoscale.UUID
}
func (v *uuid) Set(val string) error {
r, err := egoscale.ParseUUID(val)
if err != nil {
return err
}
*(v.UUID) = r
return nil
}
func (v *uuid) Type() string {
return "UUID"
}
func (v *uuid) String() string {
if v.UUID == nil || *(v.UUID) == nil {
return nilValue
}
return (*(v.UUID)).String()
}
//ip flag
type ipValue struct {
IP *net.IP
}
func (v *ipValue) Set(val string) error {
if val == "" {
return nil
}
ip := net.ParseIP(val)
if ip == nil {
return fmt.Errorf("no a valid IP address: got %q", val)
}
v.IP = &ip
return nil
}
func (v *ipValue) Value() net.IP {
if v.IP == nil {
return net.IP{}
}
return *v.IP
}
func (v *ipValue) Type() string {
return "IP"
}
func (v *ipValue) String() string {
if v.IP == nil || *v.IP == nil {
return nilValue
}
return (*v.IP).String()
}
// getIPValue finds the value of a command by name
func getIPValue(cmd *cobra.Command, name string) (*ipValue, error) {
it := cmd.Flags().Lookup(name)
if it != nil {
r := it.Value.(*ipValue)
if r != nil {
return r, nil
}
}
return nil, fmt.Errorf("unable to get flag %q", name)
}
//cidr flag
type cidr struct {
CIDR **egoscale.CIDR
}
func (v *cidr) Set(val string) error {
r, err := egoscale.ParseCIDR(val)
if err != nil {
return err
}
*(v.CIDR) = r
return nil
}
func (v *cidr) Type() string {
return "CIDR"
}
func (v *cidr) String() string {
if v.CIDR == nil || *(v.CIDR) == nil {
return nilValue
}
return (*(v.CIDR)).String()
}
//bool flag
type boolFlag struct {
bool **bool
}
func (v *boolFlag) Set(val string) error {
r := true
if val == "true" {
*(v.bool) = &r
return nil
}
r = false
*(v.bool) = &r
return nil
}
func (v *boolFlag) Type() string {
return "bool"
}
func (v *boolFlag) String() string {
if v.bool == nil || *(v.bool) == nil {
return nilValue
}
if *(*(v.bool)) {
return "true"
}
return "false"
}
// uuid list flag
type uuidListGeneric struct {
value *[]egoscale.UUID
}
func (g *uuidListGeneric) Set(value string) error {
m := g.value
if *m == nil {
n := make([]egoscale.UUID, 0)
*m = n
}
values := strings.Split(value, ",")
for _, value := range values {
uuid, err := egoscale.ParseUUID(value)
if err != nil {
return err
}
*m = append(*m, *uuid)
}
return nil
}
func (g *uuidListGeneric) Type() string {
return "uuidListGeneric"
}
func (g *uuidListGeneric) String() string {
m := g.value
if m == nil || *m == nil {
return ""
}
vs := make([]string, 0, len(*m))
for _, v := range *m {
vs = append(vs, v.String())
}
return strings.Join(vs, ",")
}
//cidr list generic
type cidrListGeneric struct {
value *[]egoscale.CIDR
}
func (g *cidrListGeneric) Set(value string) error {
m := g.value
if *m == nil {
n := make([]egoscale.CIDR, 0)
*m = n
}
values := strings.Split(value, ",")
for _, value := range values {
cidr, err := egoscale.ParseCIDR(value)
if err != nil {
return err
}
*m = append(*m, *cidr)
}
return nil
}
func (g *cidrListGeneric) Type() string {
return "cidrListGeneric"
}
func (g *cidrListGeneric) String() string {
m := g.value
if m == nil || *m == nil {
return ""
}
vs := make([]string, 0, len(*m))
for _, v := range *m {
vs = append(vs, v.String())
}
return strings.Join(vs, ",")
}
// tag flag
type tagGeneric struct {
value *[]egoscale.ResourceTag
}
func (g *tagGeneric) Set(value string) error {
m := g.value
if *m == nil {
n := make([]egoscale.ResourceTag, 0)
*m = n
}
keypairs := strings.Split(value, ",")
for _, kv := range keypairs {
values := strings.SplitN(kv, "=", 2)
if len(values) != 2 {
return fmt.Errorf("not a valid tagname=value content, got %s", kv)
}
*m = append(*m, egoscale.ResourceTag{
Key: values[0],
Value: values[1],
})
}
return nil
}
func (g *tagGeneric) Type() string {
return "tag"
}
func (g *tagGeneric) String() string {
m := g.value
if m == nil || *m == nil {
return ""
}
vs := make([]string, 0, len(*m))
for _, v := range *m {
vs = append(vs, fmt.Sprintf("%s=%s", v.Key, v.Value))
}
return strings.Join(vs, ",")
}
// map flag
type mapGeneric struct {
value *map[string]string
}
func (g *mapGeneric) Set(value string) error {
m := g.value
if *m == nil {
n := make(map[string]string)
*m = n
}
values := strings.SplitN(value, "=", 2)
if len(values) != 2 {
return fmt.Errorf("not a valid key=value content, got %s", value)
}
(*m)[values[0]] = values[1]
return nil
}
func (g *mapGeneric) Type() string {
return "map"
}
func (g *mapGeneric) String() string {
m := g.value
if *m == nil {
return ""
}
vs := make([]string, 0, len(*m))
for k, v := range *m {
vs = append(vs, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(vs, ",")
}
// stringer generic flag
type stringerTypeGeneric struct {
addr interface{}
value string
typ reflect.Type
}
func (g *stringerTypeGeneric) Set(value string) error {
tv := reflect.ValueOf(g.addr)
fv := reflect.ValueOf(&value)
tv.Elem().Set(fv.Convert(reflect.PtrTo(g.typ)).Elem())
g.value = value
return nil
}
func (g *stringerTypeGeneric) Type() string {
return "string"
}
func (g *stringerTypeGeneric) String() string {
return g.value
}
// int16 generic flag
type intTypeGeneric struct {
addr interface{}
value int64
base int
bitSize int
typ reflect.Type
}
func (g *intTypeGeneric) Set(value string) error {
v, err := strconv.ParseInt(value, g.base, g.bitSize)
if err != nil {
return err
}
tv := reflect.ValueOf(g.addr)
var fv reflect.Value
switch g.bitSize {
case 8:
val := (int8)(v)
fv = reflect.ValueOf(&val)
case 16:
val := (int16)(v)
fv = reflect.ValueOf(&val)
case 32:
val := (int)(v)
fv = reflect.ValueOf(&val)
case 64:
fv = reflect.ValueOf(&v)
}
tv.Elem().Set(fv.Convert(reflect.PtrTo(g.typ)).Elem())
g.value = v
return nil
}
func (g *intTypeGeneric) Type() string {
return "int16"
}
func (g *intTypeGeneric) String() string {
if g.addr != nil {
return strconv.FormatInt(g.value, g.base)
}
return ""
}
type userSecurityGroupListGeneric struct {
value *[]egoscale.UserSecurityGroup
}
func (g *userSecurityGroupListGeneric) Set(value string) error {
m := g.value
if *m == nil {
n := make([]egoscale.UserSecurityGroup, 0)
*m = n
}
keypairs := strings.Split(value, ",")
for _, kv := range keypairs {
// Backward compatibility for account=group semantic
values := strings.SplitN(kv, "=", 2)
if len(values) == 2 {
values[0] = values[1]
}
*m = append(*m, egoscale.UserSecurityGroup{
Group: values[0],
})
}
return nil
}
func (g *userSecurityGroupListGeneric) Type() string {
return "userSecurityGroupList"
}
func (g *userSecurityGroupListGeneric) String() string {
m := g.value
if m == nil || *m == nil {
return ""
}
vs := make([]string, 0, len(*m))
for _, v := range *m {
vs = append(vs, v.Group)
}
return strings.Join(vs, ",")
}