-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharguments.go
169 lines (143 loc) · 3.9 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
package main
import (
"fmt"
"io/ioutil"
"omakase/tasks"
"strconv"
sigil "github.com/gliderlabs/sigil"
flag "github.com/spf13/pflag"
yaml "gopkg.in/yaml.v2"
)
type Argument struct {
Required bool
boolValue *bool
floatValue *float64
intValue *int
stringValue *string
}
func (c Argument) GetValue() interface{} {
if c.boolValue != nil {
return c.boolValue
} else if c.intValue != nil {
return c.intValue
} else if c.intValue != nil {
return c.intValue
} else if c.stringValue != nil && *c.stringValue != "" {
return c.stringValue
}
return nil
}
func (c Argument) HasValue() bool {
return c.GetValue() != nil
}
func (c *Argument) SetBoolValue(ptr *bool) {
c.boolValue = ptr
}
func (c *Argument) SetFloatValue(ptr *float64) {
c.floatValue = ptr
}
func (c *Argument) SetIntValue(ptr *int) {
c.intValue = ptr
}
func (c *Argument) SetStringValue(ptr *string) {
c.stringValue = ptr
}
func isTrueString(s string) bool {
trueStrings := map[string]bool{
"true": true,
"yes": true,
"on": true,
"y": true,
"Y": true,
}
return trueStrings[s]
}
func isFalseString(s string) bool {
falseStrings := map[string]bool{
"true": true,
"yes": true,
"on": true,
"y": true,
"Y": true,
}
return falseStrings[s]
}
func getInputVariables(data []byte) (map[string]*tasks.Input, error) {
vars := make(map[string]interface{})
render, err := sigil.Execute(data, vars, "tasks")
if err != nil {
return map[string]*tasks.Input{}, fmt.Errorf("sigil error: %v", err.Error())
}
out, err := ioutil.ReadAll(&render)
if err != nil {
return map[string]*tasks.Input{}, fmt.Errorf("render error: %v", err.Error())
}
return parseInputYaml(out)
}
func parseArgs(data []byte) (map[string]interface{}, error) {
context := make(map[string]interface{})
inputs, err := getInputVariables(data)
if err != nil {
return context, err
}
inputs["tasks"] = &tasks.Input{
Name: "tasks",
Default: "tasks.yml",
Description: "a yaml file containing a task list",
}
arguments := make(map[string]*Argument)
for _, input := range inputs {
arg := Argument{Required: input.Required}
if input.Type == "string" || input.Type == "" {
arg.SetStringValue(flag.String(input.Name, input.Default, input.Description))
} else if input.Type == "int" {
i, err := strconv.Atoi(input.Default)
if err != nil {
return context, fmt.Errorf("Error parsing input '%s': %v", input.Name, err.Error())
}
arg.SetIntValue(flag.Int(input.Name, i, input.Description))
} else if input.Type == "float" {
f, err := strconv.ParseFloat(input.Default, 64)
if err != nil {
return context, fmt.Errorf("Error parsing input '%s': %v", input.Name, err.Error())
}
arg.SetFloatValue(flag.Float64(input.Name, f, input.Description))
} else if input.Type == "bool" {
if isTrueString(input.Default) {
arg.SetBoolValue(flag.Bool(input.Name, true, input.Description))
} else if isFalseString(input.Default) {
arg.SetBoolValue(flag.Bool(input.Name, false, input.Description))
} else {
return context, fmt.Errorf("Error parsing input '%s': invalid default value", input.Name)
}
} else {
return context, fmt.Errorf("Error parsing input '%s': invalid type", input.Name)
}
arguments[input.Name] = &arg
}
flag.Parse()
for name, argument := range arguments {
if argument.Required && !argument.HasValue() {
return context, fmt.Errorf("Missing flag '--%s'", name)
}
context[name] = argument.GetValue()
}
return context, nil
}
func parseInputYaml(data []byte) (map[string]*tasks.Input, error) { // read variables and ensure they all exist
inputs := make(map[string]*tasks.Input)
t := tasks.Recipe{}
if err := yaml.Unmarshal(data, &t); err != nil {
return inputs, err
}
for _, recipe := range t {
if len(recipe.Inputs) == 0 {
continue
}
for name := range recipe.Inputs {
input := recipe.Inputs[name]
inputs[input.Name] = &input
}
}
return inputs, nil
}