-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_helpers.go
123 lines (115 loc) · 2.77 KB
/
app_helpers.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
package cline
import (
"fmt"
"strings"
)
// It checks if a list of commands and initialize them if they are valid.
func validateCommands(commands []Cmd) (cmds []Cmd, err error) {
for _, c := range commands {
name := strings.TrimSpace(c.Name)
if name == "" {
err = fmt.Errorf("one command name has empty value")
return
}
flags, errf := validateFlagsAndInit(c.Flags)
if errf != nil {
err = errf
return
}
c.Flags = flags
cmds = append(cmds, c)
}
return
}
// It checks a list of flags and initialize them if they are valid.
func validateFlagsAndInit(flags []Flag) (vflags []Flag, err error) {
for _, v := range flags {
switch f := v.(type) {
case FlagBool:
if name := strings.ToLower(strings.TrimSpace(f.Name)); name == "" {
err = fmt.Errorf("bool flag name has an empty value")
return
}
f.initialize()
vflags = append(vflags, f)
case FlagInt:
if name := strings.ToLower(strings.TrimSpace(f.Name)); name == "" {
err = fmt.Errorf("int flag name has an empty value")
return
}
f.initialize()
vflags = append(vflags, f)
case FlagString:
if name := strings.ToLower(strings.TrimSpace(f.Name)); name == "" {
err = fmt.Errorf("string flag name has an empty value")
return
}
f.initialize()
vflags = append(vflags, f)
case FlagStringSlice:
if name := strings.ToLower(strings.TrimSpace(f.Name)); name == "" {
err = fmt.Errorf("string slice flag name has an empty value")
return
}
f.initialize()
vflags = append(vflags, f)
default:
err = fmt.Errorf("one flag has invalid data type value. Use a bool, int, string, []string or nil value")
return
}
}
return
}
// It finds a flag item with its index in a given flags array by key
// then checks if every flag is a short flag or not.
func findFlagByKey(key string, flags []Flag) (int, Flag, bool) {
for i, v := range flags {
switch f := v.(type) {
case FlagBool:
// Check for long named flags
if f.Name == key {
return i, f, false
}
// Check for short named flags
for _, s := range f.Aliases {
if s == key {
return i, f, true
}
}
case FlagInt:
// Check for long named flags
if f.Name == key {
return i, f, false
}
// Check for short named flags
for _, s := range f.Aliases {
if s == key {
return i, f, true
}
}
case FlagString:
// Check for long named flags
if f.Name == key {
return i, f, false
}
// Check for short named flags
for _, s := range f.Aliases {
if s == key {
return i, f, true
}
}
case FlagStringSlice:
// Check for long named flags
if f.Name == key {
return i, f, false
}
// Check for short named flags
for _, s := range f.Aliases {
if s == key {
return i, f, true
}
}
}
}
return -1, nil, false
}