This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargify.go
231 lines (210 loc) · 5.18 KB
/
argify.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
package argify
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/dc0d/inflect"
"github.com/dc0d/inflect/inflectlab"
"github.com/urfave/cli"
)
// Argify adds arguments based on fields of a struct and binds them too
type Argify struct {
appName string
}
// NewArgify creates an instance of Argify
func NewArgify() *Argify {
return &Argify{}
}
func (b *Argify) extractSharedParts(
prefix, commandName string,
k1 string,
v1 inflectlab.Field) (name, usage, envVar string, hidden bool) {
name = strings.ToLower(k1)
if prefix != "" {
name = strings.ToLower(prefix) + "-" + name
}
if v, ok := v1.Tags["name"]; ok {
name = v
}
usage = ""
if v, ok := v1.Tags["usage"]; ok {
usage = v
}
envVar = ""
if v, ok := v1.Tags["envvar"]; ok {
envVar = v
}
if envVar == "" {
envName := name
envParts := strings.Split(envName, ",")
if len(envParts) > 1 {
envName = envParts[0]
if len(envParts[1]) > len(envName) {
envName = envParts[1]
}
}
if commandName == "" {
envVar = strings.Join([]string{b.appName, envName}, "_")
} else {
envVar = strings.Join([]string{b.appName, commandName, envName}, "_")
}
envVar = strings.Trim(envVar, "_")
envVar = strings.Replace(envVar, "-", "_", -1)
envVar = strings.ToUpper(envVar)
}
hidden = false
if v, ok := v1.Tags["hidden"]; ok {
if b, err := strconv.ParseBool(v); err == nil {
hidden = b
}
}
return
}
func (b *Argify) setSharedParts(f cli.Flag, name, usage, envVar string, hidden bool) {
if err := inflect.Set(f, "Name", name); err != nil {
// TODO:
}
if err := inflect.Set(f, "Usage", usage); err != nil {
// TODO:
}
if envVar != "-" {
if err := inflect.Set(f, "EnvVar", envVar); err != nil {
// TODO:
}
}
if err := inflect.Set(f, "Hidden", hidden); err != nil {
// TODO:
}
}
func (b *Argify) process(
prefix, commandName string,
fields map[string]inflectlab.Field,
commands *[]cli.Command,
flags *[]cli.Flag) {
var keys []string
for k1 := range fields {
keys = append(keys, k1)
}
sort.Strings(keys)
NEXT_FIELD:
for _, k1 := range keys {
v1 := fields[k1]
for kcmd := range *commands {
if strings.EqualFold((*commands)[kcmd].Name, k1) {
var subCommands []cli.Command = (*commands)[kcmd].Subcommands
var cmdFlags = (*commands)[kcmd].Flags
if cmdFlags == nil {
cmdFlags = []cli.Flag{}
}
b.process("", (*commands)[kcmd].Name, v1.Children, &subCommands, &cmdFlags)
(*commands)[kcmd].Subcommands = subCommands
(*commands)[kcmd].Flags = cmdFlags
continue NEXT_FIELD
}
}
var f cli.Flag
//
defaultValue := reflect.Zero(v1.Ptr.Type()).Interface()
var tagString string
if v, ok := v1.Tags["value"]; ok && v != fmt.Sprintf("%v", defaultValue) {
tagString = v
}
var tagValue interface{}
switch v1.Ptr.Type() {
case reflect.TypeOf(true):
f = &cli.BoolFlag{}
if tagString != "" {
if vp, err := strconv.ParseBool(tagString); err == nil {
tagValue = vp
}
}
case reflect.TypeOf(""):
f = &cli.StringFlag{}
if tagString != "" {
tagValue = tagString
}
case reflect.TypeOf(time.Second):
f = &cli.DurationFlag{}
if tagString != "" {
if vp, err := time.ParseDuration(tagString); err == nil {
tagValue = vp
}
}
case reflect.TypeOf(float64(1.0)):
f = &cli.Float64Flag{}
if tagString != "" {
if vp, err := strconv.ParseFloat(tagString, 64); err == nil {
tagValue = vp
}
}
case reflect.TypeOf(int64(1)):
f = &cli.Int64Flag{}
if tagString != "" {
if vp, err := strconv.ParseInt(tagString, 10, 64); err == nil {
tagValue = vp
}
}
case reflect.TypeOf(int(1)):
f = &cli.IntFlag{}
if tagString != "" {
if vp, err := strconv.ParseInt(tagString, 10, 64); err == nil {
tagValue = int(vp)
}
}
case reflect.TypeOf(uint(1)):
f = &cli.UintFlag{}
if tagString != "" {
if vp, err := strconv.ParseInt(tagString, 10, 64); err == nil {
tagValue = uint(vp)
}
}
case reflect.TypeOf(uint64(1)):
f = &cli.Uint64Flag{}
if tagString != "" {
if vp, err := strconv.ParseInt(tagString, 10, 64); err == nil {
tagValue = uint64(vp)
}
}
default:
var cmdFlags = *flags
if cmdFlags == nil {
cmdFlags = []cli.Flag{}
}
var subCommands []cli.Command
b.process(k1, "", v1.Children, &subCommands, &cmdFlags)
*flags = cmdFlags
continue
}
//
if v1.Ptr.Interface() != defaultValue {
if err := inflect.Set(f, "Value", v1.Ptr.Interface()); err != nil &&
err.Error() != "field does not exist" {
// TODO:
}
} else if tagString != "" {
if err := inflect.Set(f, "Value", tagValue); err != nil &&
err.Error() != "field does not exist" {
// TODO:
}
}
name, usage, envVar, hidden := b.extractSharedParts(prefix, commandName, k1, v1)
b.setSharedParts(f, name, usage, envVar, hidden)
if err := inflect.Set(f, "Destination", v1.Ptr.Addr().Interface()); err != nil {
// TODO:
}
*flags = append(*flags, f)
}
}
// Build builds the arguments/flags
func (b *Argify) Build(app *cli.App, confPtr interface{}) error {
fields, err := inflectlab.GetFields(confPtr)
if err != nil {
return err
}
b.appName = app.Name
b.process("", "", fields, &app.Commands, &app.Flags)
return nil
}