-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
52 lines (44 loc) · 1.04 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
package main
import (
"fmt"
"os"
"sync/atomic"
"github.com/spf13/viper"
)
var config *atomic.Value
type Config struct {
slackWebhook string
tests []*Test
}
func init() {
config = new(atomic.Value)
config.Store(loadConfig())
}
func getConfig() *Config {
return config.Load().(*Config)
}
func readConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/uptime-mon/")
viper.AddConfigPath("$HOME/.config/uptime-mon/")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Failed to read config file:", err)
os.Exit(125)
}
}
func loadConfig() *Config {
readConfig()
settingsInConfig := viper.GetStringMapString("settings")
testsInConfig := viper.Get("tests").([]interface{})
size := len(testsInConfig)
c := &Config{}
c.slackWebhook = settingsInConfig["slack-webhook"]
c.tests = make([]*Test, size)
for i, t := range testsInConfig {
c.tests[i] = NewTest(t.(map[interface{}]interface{}))
}
fmt.Println("Found", size, "tests in config file")
return c
}