-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
92 lines (79 loc) · 2.28 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"os"
"time"
flag "github.com/spf13/pflag"
"github.com/vetinari/brickd_exporter/mqtt"
"gopkg.in/yaml.v2"
)
const (
defaultListenAddress = ":9639"
defaultMetricsPath = "/metrics"
)
type LocalConfig struct {
Listen ListenConfig `yaml:"listen"`
Brickd BrickdConfig `yaml:"brickd"`
Collector CollectorConfig `yaml:"collector"`
MQTT *mqtt.MQTT `yaml:"mqtt"`
}
type ListenConfig struct {
Address string `yaml:"address"`
MetricsPath string `yaml:"metrics_path"`
}
type BrickdConfig struct {
Address string `yaml:"address"`
User string `yaml:"user"`
Password string `yaml:"password"`
}
type CollectorConfig struct {
LogLevel string `yaml:"log_level"`
CallbackPeriod time.Duration `yaml:"callback_period"`
IgnoredUIDs []string `yaml:"ignored_uids"`
Labels map[string]string `yaml:"labels"`
SensorLabels map[string]map[string]map[string]string `yaml:"sensor_labels"`
LEDStatus string `yaml:"led_status"`
Expire time.Duration `yaml:"expire_period"`
}
func parseConfig() (*LocalConfig, error) {
var configFile = flag.String("config.file", "", "Path to configuration file.")
flag.Parse()
if *configFile == "" {
return defaultConfig()
}
file, err := os.Open(*configFile)
if err != nil {
return nil, fmt.Errorf("can not open config file: %s", err)
}
config := &LocalConfig{}
if err := yaml.NewDecoder(file).Decode(config); err != nil {
return nil, fmt.Errorf("error decoding config file %q: %s", *configFile, err)
}
return config, nil
}
func defaultConfig() (*LocalConfig, error) {
return &LocalConfig{
Brickd: BrickdConfig{
Address: "localhost:4223",
},
Listen: ListenConfig{
Address: defaultListenAddress,
MetricsPath: defaultMetricsPath,
},
Collector: CollectorConfig{
LogLevel: "info",
CallbackPeriod: 10 * time.Second,
Expire: 0,
LEDStatus: "on",
},
MQTT: &mqtt.MQTT{
Enabled: false,
Topic: "brickd/",
HomeAssistant: mqtt.HomeAssistant{
Enabled: false,
DiscoveryBase: "homeassistant/",
Interval: 5 * time.Minute,
},
},
}, nil
}