-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
44 lines (33 loc) · 885 Bytes
/
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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/spf13/viper"
)
var config globalConfig
func loadConfig() {
log.Println("Loading configuration...")
// load the config
viper.SetConfigName("config")
viper.AddConfigPath("/etc/hostdb-collector-vrops")
viper.AddConfigPath(".")
// load env vars
viper.SetEnvPrefix("hostdb_collector_vrops")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// read the config file, and handle any errors
if err := viper.ReadInConfig(); err != nil {
log.Fatal(fmt.Errorf("fatal error config file: %s", err))
}
// unmarshal into our struct
if err := viper.Unmarshal(&config); err != nil {
log.Fatal(fmt.Errorf("unable to decode into struct, %v", err))
}
// debug
if config.Collector.Debug {
log.Println(fmt.Sprintf("%v", os.Environ()))
log.Println(fmt.Sprintf("%v", config))
}
}