-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
68 lines (56 loc) · 1.38 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
package main
import (
"fmt"
"os"
flag "github.com/spf13/pflag"
"gopkg.in/yaml.v2"
)
const (
defaultListenAddress = ":9871"
defaultMetricsPath = "/metrics"
)
type LocalConfig struct {
Listen ListenConfig `yaml:"listen"`
Collector CollectorConfig `yaml:"collector"`
MQTT MQTT `yaml:"mqtt"`
}
type ListenConfig struct {
Address string `yaml:"address"`
MetricsPath string `yaml:"metrics_path"`
}
type CollectorConfig struct {
LogLevel string `yaml:"log_level"`
LogFormat string `yaml:"log_format"`
Labels map[string]string `yaml:"labels"`
}
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{
Listen: ListenConfig{
Address: defaultListenAddress,
MetricsPath: defaultMetricsPath,
},
Collector: CollectorConfig{
LogLevel: "info",
LogFormat: "text",
},
MQTT: MQTT{
Enabled: false,
},
}, nil
}