-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy patharguments.go
395 lines (332 loc) · 9.31 KB
/
arguments.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
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"errors"
"os"
"reflect"
"strconv"
"strings"
"github.com/mgutz/ansi"
)
var (
// ErrDuplicateArgumentNames means the name for an argument was reused
ErrDuplicateArgumentNames = errors.New("duplicate argument name")
)
// argument types
const (
argTypeString = "String"
argTypeInt = "Int"
argTypeBool = "Bool"
argTypeFloat = "Float"
)
// a command argument has a name and a type and a value
type commandArg struct {
// argument label
name string
// argument type
argType reflect.Kind
// optionals are allowed, they can have default values
optional bool
defaultValue string
// value after parsing argument input from commandline
value string
}
// validate arguments string from CommandsFile
// and return the validatedArgs as map
func (c *CommandsFile) validateArgs(args []string) ([]*commandArg, error) {
// init map
var validatedArgs []*commandArg
// empty string - empty args
if len(args) == 0 {
return nil, nil
}
// parse arg string
for i, s := range args {
if len(s) == 0 {
return nil, errors.New("found empty argument at index: " + strconv.Itoa(i))
}
var (
k reflect.Kind
slice = strings.Split(s, ":")
opt bool
defaultValue string
)
if len(slice) > 1 {
// argument name may contain leading whitespace - trim it
var argumentName = strings.TrimSpace(slice[0])
// check for name conflicts with globals
g.Lock()
for name := range g.Vars {
if argumentName == name {
g.Unlock()
listGlobals()
return nil, errors.New("argument name " + argumentName + " conflicts with a global variable")
}
}
g.Unlock()
// check for duplicate argument names
if a, ok := containsArg(validatedArgs, argumentName); ok {
Log.Error("argument label ", a.name, " was used twice")
return nil, ErrDuplicateArgumentNames
}
// check if there's a default value set
defaultValSlice := strings.Split(slice[1], "=")
if len(defaultValSlice) > 1 {
if !strings.Contains(slice[1], "?") {
return nil, errors.New("default values for mandatory arguments are not allowed: " + s + ", at index: " + strconv.Itoa(i))
}
slice[1] = strings.TrimSpace(defaultValSlice[0])
defaultValue = defaultValSlice[1]
}
// check if its an optional arg
if strings.HasSuffix(slice[1], "?") {
slice[1] = strings.TrimSuffix(slice[1], "?")
opt = true
}
// check if its a valid argType and set reflect.Kind
switch slice[1] {
case argTypeBool:
k = reflect.Bool
case argTypeFloat:
k = reflect.Float64
case argTypeString:
k = reflect.String
case argTypeInt:
k = reflect.Int
default:
return nil, errors.New("invalid or missing argument type: " + s)
}
// add to validatedArgs
validatedArgs = append(validatedArgs, &commandArg{
name: argumentName,
argType: k,
optional: opt,
defaultValue: defaultValue,
})
} else {
return nil, errors.New("invalid argument declaration: " + s)
}
}
return validatedArgs, nil
}
// parse arguments array in the label=value format
// and return a code snippet that declares them in the language of the command
func (c *command) parseArguments(args []string) (string, map[string]string, error) {
var (
argBuf bytes.Buffer
occurrences = make(map[string]int, 0)
// In order to be able to use argument values in command output names,
// we need to return the current state of arguments.
// It's not possible to use the c.args map for that, as these are shared in between multiple invocations of the same command,
// and the argument values must be determined again for every invocation.
// For that reason, the argument names mapped to their current values will be returned as a separate map to the caller.
argValues = map[string]string{}
)
// parse args
for _, val := range args {
// handle argument labels
if strings.Contains(val, "=") {
var (
cmdArg *commandArg
ok bool
)
argSlice := strings.SplitN(val, "=", 2)
if len(argSlice) != 2 {
return "", argValues, errors.New("invalid argument: " + val)
}
cmdArg, ok = containsArg(c.args, argSlice[0])
if !ok {
return "", argValues, errors.New(ErrInvalidArgumentLabel.Error() + ": " + ansi.Red + argSlice[0] + cp.Reset)
}
if _, ok := occurrences[argSlice[0]]; ok {
occurrences[argSlice[0]]++
} else {
occurrences[argSlice[0]] = 1
}
if occurrences[argSlice[0]] > 1 {
return "", argValues, errors.New("argument label appeared more than once: " + cmdArg.name)
}
argValue := argSlice[1]
if err := validArgType(argValue, cmdArg.argType); err != nil {
return "", argValues, errors.New(ErrInvalidArgumentType.Error() + ": " + err.Error() + ", label=" + cmdArg.name + ", value=" + argValue)
}
// temporarily set value on arg
cmdArg.value = argValue
} else {
return "", argValues, errors.New("invalid argument: " + val)
}
}
lang, err := c.getLanguage()
if err != nil {
return "", argValues, err
}
for _, arg := range c.args {
if arg.value == "" {
if arg.optional {
if arg.defaultValue != "" {
argValues[arg.name] = arg.defaultValue
// default value has been set
argBuf.WriteString(lang.VariableKeyword + arg.name + lang.AssignmentOperator + strings.TrimSpace(arg.defaultValue) + lang.LineDelimiter + "\n")
} else {
argValues[arg.name] = getDefaultValue(arg)
// init empty optionals with default value for their type
argBuf.WriteString(lang.VariableKeyword + arg.name + lang.AssignmentOperator + getDefaultValue(arg) + lang.LineDelimiter + "\n")
}
} else {
// empty value and not optional - error
return "", argValues, errors.New(c.name + " is missing an argument: " + ansi.Red + arg.name + ":" + strings.Title(arg.argType.String()) + cp.Reset)
}
} else {
argValues[arg.name] = arg.value
// write value into buffer
argBuf.WriteString(lang.VariableKeyword + arg.name + lang.AssignmentOperator + arg.value + lang.LineDelimiter + "\n")
}
}
// flush arg values before returning
for _, arg := range c.args {
arg.value = ""
}
return argBuf.String(), argValues, nil
}
func replaceArgs(input string, args map[string]string) (string, error) {
var (
dollar, startOfIdent bool
name string
names []string
)
// replace variables used in ${} notation with global values
for _, char := range input {
if char == '$' {
dollar = true
continue
}
if dollar {
if char == '{' {
startOfIdent = true
continue
}
}
if char == '}' {
// collect string
n := name
names = append(names, n)
// reset state values
name = ""
dollar = false
startOfIdent = false
}
if startOfIdent {
name += string(char)
continue
}
}
for _, n := range names {
if val, ok := args[n]; ok {
input = strings.ReplaceAll(input, "${"+n+"}", val)
} else {
return "", errors.New(input + ": variable is not provided via globals or arguments: ${" + n + "}")
}
}
return input, nil
}
func (c *CommandsFile) replaceGlobals(input string) string {
var (
dollar, startOfIdent bool
name string
names []string
)
// replace variables used in ${} notation with global values
for _, char := range input {
if char == '$' {
dollar = true
continue
}
if dollar {
if char == '{' {
startOfIdent = true
continue
}
}
if char == '}' {
// collect string
n := name
names = append(names, n)
// reset state values
name = ""
dollar = false
startOfIdent = false
}
if startOfIdent {
name += string(char)
continue
}
}
for _, n := range names {
if val, ok := g.Vars[n]; ok {
input = strings.ReplaceAll(input, "${"+n+"}", val)
}
}
return input
}
func resolveEnvironment(input string) string {
var (
dollar, startOfIdent bool
name string
names []string
)
// replace variables used in ${} notation with global values
for _, char := range input {
if char == '$' {
dollar = true
continue
}
if dollar {
if char == '{' {
startOfIdent = true
continue
}
}
if char == '}' {
// collect string
n := name
names = append(names, n)
// reset state values
name = ""
dollar = false
startOfIdent = false
}
if startOfIdent {
name += string(char)
continue
}
}
for _, n := range names {
input = strings.ReplaceAll(input, "${"+n+"}", os.Getenv(n))
}
return input
}
func containsArg(args []*commandArg, name string) (*commandArg, bool) {
for _, a := range args {
if a.name == name {
return a, true
}
}
return nil, false
}