-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
54 lines (48 loc) · 1.1 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
package horenso
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
)
type handlers []string
type config struct {
Reporter handlers `yaml:"reporter"`
Noticer handlers `yaml:"noticer"`
Timestamp bool `yaml:"timestamp"`
Tag string `yaml:"tag"`
OverrideStatus bool `yaml:"overrideStatus"`
Logfile string `yaml:"log"`
}
func (ha *handlers) UnmarshalYAML(unmarshal func(interface{}) error) error {
var aux interface{}
if err := unmarshal(&aux); err != nil {
return err
}
switch raw := aux.(type) {
case string:
*ha = []string{raw}
case []interface{}:
list := make([]string, len(raw))
for i, r := range raw {
v, ok := r.(string)
if !ok {
return fmt.Errorf("handlers should be a string or an array of string: %v", aux)
}
list[i] = v
}
*ha = list
default:
return fmt.Errorf("handlers should be a string or an array of string: %v", aux)
}
return nil
}
func loadConfig(file string) (*config, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
c := &config{}
err = yaml.NewDecoder(f).Decode(c)
return c, err
}