-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
93 lines (76 loc) · 1.79 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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
"time"
)
type config struct {
Rules []*rule `yaml:"rules"`
Global global `yaml:"global"`
defaultDelay time.Duration
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}
type global struct {
ModeButton string `yaml:"mode_button"`
DefaultDelay string `yaml:"default_delay"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}
type rule struct {
Name string `yaml:"name"`
Activate string `yaml:"activate"`
Scenario []step `yaml:"scenario"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
}
func loadFile(filename string) (*config, error) {
if stat, err := os.Stat(filename); err != nil {
return nil, fmt.Errorf("cannot get file info: %s", err)
} else if stat.IsDir() {
return nil, fmt.Errorf("is a directory")
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cfg, err := load(string(content))
if err != nil {
return nil, err
}
err = cfg.validate()
return cfg, nil
}
func load(s string) (*config, error) {
cfg := &config{}
err := yaml.Unmarshal([]byte(s), cfg)
if err != nil {
return nil, err
}
err = checkOverflow(cfg.XXX)
if err != nil {
return nil, err
}
return cfg, nil
}
func (cfg *config) validate() error {
dur, err := time.ParseDuration(cfg.Global.DefaultDelay)
if err != nil {
return err
}
cfg.defaultDelay = dur
return nil
}
func checkOverflow(m map[string]interface{}) error {
if len(m) > 0 {
var keys []string
for k := range m {
keys = append(keys, k)
}
return fmt.Errorf("unknown fields in config: %s", strings.Join(keys, ", "))
}
return nil
}