alrm/config/config.go

53 lines
1.0 KiB
Go
Raw Normal View History

package config
import (
2021-11-14 07:04:25 -09:00
yaml "gopkg.in/yaml.v2"
"io"
"os"
"time"
)
type Config struct {
2021-11-14 07:04:25 -09:00
Interval time.Duration `yaml:"interval"`
Listen string `yaml:"listen"`
DebugLevel int `yaml:"debug_level"`
APIKey string `yaml:"api_key"`
APIKeyFile string `yaml:"api_key_file"`
WebRoot string `yaml:"web_root"`
Hosts []Host `yaml:"hosts"`
Alarms []Alarm `yaml:"alarms"`
Groups map[string][]Host `yaml:"groups"`
2021-01-17 05:24:13 -09:00
}
2021-11-14 07:04:25 -09:00
func New() *Config {
return &Config{
Interval: 30 * time.Second,
Listen: "127.0.0.1:8282",
DebugLevel: 0,
WebRoot: "/",
}
2021-11-14 07:04:25 -09:00
}
2021-11-14 07:04:25 -09:00
func ReadConfig(fn string, debuglvl int) (*Config, error) {
f, err := os.Open(fn)
if err != nil {
return nil, err
}
2021-11-14 07:04:25 -09:00
h, err := io.ReadAll(f)
if err != nil {
2021-11-14 07:04:25 -09:00
return nil, err
}
2021-11-14 07:04:25 -09:00
cfg := New()
err = yaml.UnmarshalStrict(h, &cfg)
if err != nil {
return nil, err
}
2021-11-14 07:04:25 -09:00
err = cfg.Validate()
if err != nil {
return nil, err
2021-02-20 17:08:27 -09:00
}
return cfg, nil
}