-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
259 lines (213 loc) · 5.64 KB
/
config.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
package main
import (
"fmt"
"os"
"regexp"
"strings"
"gopkg.in/yaml.v3"
)
type ConfigInputOption struct {
Label string
Value string
}
func (option ConfigInputOption) String() string {
return option.Label
}
type ConfigInputOptions []ConfigInputOption
func (options ConfigInputOptions) Len() int {
return len(options)
}
func (options ConfigInputOptions) Contains(value any) bool {
for _, item := range options {
if item.Value == value {
return true
}
}
return false
}
func (x *ConfigInputOptions) UnmarshalYAML(node *yaml.Node) error {
var options ConfigInputOptions
switch node.Kind {
case yaml.SequenceNode:
var seqValue []string
if err := node.Decode(&seqValue); err != nil {
return err
}
for _, option := range seqValue {
options = append(options, ConfigInputOption{
Label: option,
Value: option,
})
}
case yaml.MappingNode:
content := node.Content
for len(content) > 0 {
options = append(options, ConfigInputOption{
Label: content[0].Value,
Value: content[1].Value,
})
content = content[2:]
}
default:
return fmt.Errorf("line %d: unexpected node type", node.Line)
}
*x = options
return nil
}
type ConfigInput struct {
Name string `yaml:"-"`
DefaultValue string `yaml:"default"`
Pattern string
Options ConfigInputOptions
Description string
}
func (input *ConfigInput) SafeName() string {
return strings.ReplaceAll(input.Name, "-", "_")
}
func (input *ConfigInput) Selectable() bool {
return input.Options.Len() > 0
}
func (input *ConfigInput) Valid(value any) bool {
if input.Selectable() {
return input.Options.Contains(value)
} else if input.Pattern == "" {
return true
}
matched, _ := regexp.MatchString(input.Pattern, fmt.Sprintf("%v", value))
return matched
}
type ConfigInputs []ConfigInput
func (x *ConfigInputs) UnmarshalYAML(value *yaml.Node) error {
if value.Kind != yaml.MappingNode || len(value.Content)%2 != 0 {
return fmt.Errorf("line %d: cannot unmarshal inputs into map", value.Line)
}
inputs := ConfigInputs{}
content := value.Content
for len(content) > 0 {
keyNode := content[0]
valueNode := content[1]
if keyNode.Kind != yaml.ScalarNode {
return fmt.Errorf("line %d: unexpected node type", keyNode.Line)
}
var input ConfigInput
switch valueNode.Kind {
case yaml.ScalarNode:
input.Name = keyNode.Value
case yaml.MappingNode:
if err := valueNode.Decode(&input); err != nil {
return err
}
input.Name = keyNode.Value
default:
return fmt.Errorf("line %d: unexpected node type", valueNode.Line)
}
if !validName(input.Name) {
return fmt.Errorf("line %d: invalid input name", valueNode.Line)
}
inputs = append(inputs, input)
content = content[2:]
}
*x = inputs
return nil
}
type ConfigCommand struct {
Name string `yaml:"-"`
Description string
Run string
Shell []string
Env map[string]string
Pure bool
Inputs ConfigInputs
Commands ConfigCommands `yaml:",flow"`
Aliases []string
}
func (command ConfigCommand) String() string {
return command.Name
}
type ConfigCommands []ConfigCommand
func (commands *ConfigCommands) Get(name string) *ConfigCommand {
for _, command := range *commands {
if command.Name == name {
return &command
}
for _, alias := range command.Aliases {
if alias == name {
return &command
}
}
}
return nil
}
func (x *ConfigCommands) UnmarshalYAML(value *yaml.Node) error {
if value.Kind != yaml.MappingNode || len(value.Content)%2 != 0 {
return fmt.Errorf("line %d: cannot unmarshal commands into map", value.Line)
}
commands := ConfigCommands{}
content := value.Content
for len(content) > 0 {
keyNode := content[0]
valueNode := content[1]
if keyNode.Kind != yaml.ScalarNode || !(valueNode.Kind == yaml.MappingNode || valueNode.Kind == yaml.ScalarNode) {
return fmt.Errorf("line %d: invalid definition for command '%s'", keyNode.Line, keyNode.Value)
}
var command ConfigCommand
if valueNode.Kind == yaml.ScalarNode {
command.Name = keyNode.Value
command.Run = valueNode.Value
} else {
if err := valueNode.Decode(&command); err != nil {
return err
}
if numCommands := len(command.Commands); command.Run == "" && numCommands == 0 {
return fmt.Errorf("line %d: command '%s' must have either 'run' or 'commands' attribute", keyNode.Line, keyNode.Value)
}
command.Name = keyNode.Value
}
if !validName(command.Name) {
return fmt.Errorf("line %d: invalid command name '%s'", valueNode.Line, command.Name)
}
for _, alias := range command.Aliases {
if !validName(alias) {
return fmt.Errorf("line %d: invalid command alias '%s'", valueNode.Line, alias)
}
if c := commands.Get(alias); c != nil {
return fmt.Errorf("line %d: alias '%s' already defined by command '%s'", valueNode.Line, alias, c.Name)
}
}
commands = append(commands, command)
content = content[2:]
}
*x = commands
return nil
}
type Config struct {
Description string
Run string
Shell []string
Env map[string]string
Pure bool
Inputs ConfigInputs
Commands ConfigCommands `yaml:",flow"`
}
func (c Config) Runnable() bool {
return c.Run != ""
}
func ParseConfig(content []byte) (Config, error) {
var config Config
if err := yaml.Unmarshal(content, &config); err != nil {
return config, err
}
return config, nil
}
func LoadConfig(path string) (Config, error) {
logger.Printf("Attempting to load config file: %s", path)
if content, err := os.ReadFile(path); err != nil {
return Config{}, err
} else {
return ParseConfig(content)
}
}
func validName(s string) bool {
m, _ := regexp.MatchString("^[a-zA-Z0-9][a-zA-Z0-9-_]*$", s)
return m
}