forked from shirou/mqforward
/
config.go
58 lines (47 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
53
54
55
56
57
58
package main
import (
"os"
"runtime"
"strings"
log "github.com/Sirupsen/logrus"
gcfg "gopkg.in/gcfg.v1"
)
type GeneralConf struct {
Debug bool
}
type Config struct {
General GeneralConf
Mqtt MqttConf `gcfg:"mqforward-mqtt"`
InfluxDB InfluxDBConf `gcfg:"mqforward-influxdb"`
}
func UserHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
func LoadConf(path string) (MqttConf, InfluxDBConf, error) {
home := UserHomeDir()
path = strings.Replace(path, "~", home, 1)
var cfg Config
err := gcfg.ReadFileInto(&cfg, path)
if err != nil {
return MqttConf{}, InfluxDBConf{}, err
}
em := MqttConf{}
ei := InfluxDBConf{}
if cfg.Mqtt == em {
log.Fatal(`empty "mqforward-mqtt" configuration`)
}
if cfg.InfluxDB == ei {
log.Fatal(`empty "mqforward-influxdb" configuration`)
}
if cfg.General.Debug {
log.SetLevel(log.DebugLevel)
}
return cfg.Mqtt, cfg.InfluxDB, nil
}