-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
66 lines (55 loc) · 1.5 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
package main
import (
"fmt"
"os"
"github.com/creasty/defaults"
"gopkg.in/yaml.v2"
)
type Config struct {
MaxWorkers int `yaml:"maxWorkers" default:"10"`
InputDir string `yaml:"inputDir"`
RefDir string `yaml:"refDir"`
TestGenCmd []string `yaml:"testGenCmd"`
TestGenDir string `yaml:"testGenDir"`
Execs []Exec `yaml:"execs"`
}
type Exec struct {
Name string `yaml:"name"`
Cmd []string `yaml:"cmd"`
Timeout float64 `yaml:"time" default:"1"`
Ignore bool `yaml:"ignore" default:"false"`
}
func ParseConfig(filename string) (Config, error) {
bytes, err := os.ReadFile(filename)
if err != nil {
return Config{}, fmt.Errorf("failed to read config file: %w", err)
}
var config Config
defaults.Set(&config)
if err := yaml.Unmarshal([]byte(bytes), &config); err != nil {
return Config{}, fmt.Errorf("failed to parse config file: %w", err)
}
for i := 0; i < len(config.Execs); i += 1 {
defaults.Set(&config.Execs[i])
if config.Execs[i].Ignore {
config.Execs = append(config.Execs[:i], config.Execs[i+1:]...)
i -= 1
}
}
for _, exec := range config.Execs {
if len(exec.Cmd) <= 0 {
msg := "the cmd of an exec cannot be empty"
if len(exec.Name) > 0 {
msg = fmt.Sprintf("the cmd of %s cannot be empty", exec.Name)
}
return Config{}, fmt.Errorf(msg)
}
}
// Add "default" names for execs which don't have one specified.
for i, exec := range config.Execs {
if exec.Name == "" {
config.Execs[i].Name = exec.Cmd[0]
}
}
return config, nil
}