forked from Tinkoff/prometheus-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
67 lines (60 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
67
package main
import (
"errors"
"fmt"
"io/ioutil"
"time"
"gopkg.in/yaml.v2"
)
const (
defaultListenAddress = "0.0.0.0:9333"
)
type Config struct {
ListenAddress string `yaml:"listenAddress"`
PrometheusURL string `yaml:"prometheusURL"`
RepeatInterval time.Duration `yaml:"repeatInterval"`
CommandTimeout time.Duration `yaml:"commandTimeout"`
CooldownPeriod time.Duration `yaml:"cooldownPeriod"`
Actions []*Action `yaml:"actions"`
}
func LoadConfig(filename string) (*Config, error) {
out, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
config := &Config{}
err = yaml.Unmarshal(out, config)
if err != nil {
return nil, err
}
config.SpecifyDefaults()
return config, nil
}
func (c *Config) SpecifyDefaults() {
if len(c.ListenAddress) == 0 {
c.ListenAddress = defaultListenAddress
}
}
func (c *Config) Validate() error {
if len(c.Actions) == 0 {
return errors.New("actions must be specified")
}
if c.RepeatInterval <= time.Second {
return errors.New("repeatInterval must be greater than second")
}
if c.CommandTimeout <= time.Second {
return errors.New("commandTimeout must be greater than second")
}
uniqueActions := make(map[string]struct{})
for i, action := range c.Actions {
err := action.Validate()
if err != nil {
return fmt.Errorf("action %d error: %v", i, err)
}
if _, ok := uniqueActions[action.Name]; ok {
return fmt.Errorf("duplicate of %s action", action.Name)
}
uniqueActions[action.Name] = struct{}{}
}
return nil
}