-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.go
51 lines (41 loc) · 913 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
45
46
47
48
49
50
51
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 ExpandPath(path string) string {
return strings.Replace(path, "~", UserHomeDir(), 1)
}
func LoadConf(path string) (MqttConf, InfluxDBConf, error) {
path = ExpandPath(path)
var cfg Config
err := gcfg.ReadFileInto(&cfg, path)
if err != nil {
return MqttConf{}, InfluxDBConf{}, err
}
if cfg.General.Debug {
log.SetLevel(log.DebugLevel)
}
return cfg.Mqtt, cfg.InfluxDB, nil
}