-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
82 lines (67 loc) · 1.57 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
package main
import (
"errors"
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v3"
)
type Config struct {
StatsdHost string `yaml:"statsd_host"`
StatsdPort int `yaml:"statsd_port"`
StatsdProtocol string `yaml:"statsd_protocol"`
URLs []Url
}
type Url struct {
Label string `yaml:"-"`
Url string `yaml:"-"`
}
func (u Url) String() string {
return u.Label
}
var ConfigPaths = [6]string{
"connectivity.yml",
"connectivity.yaml",
"~/.connectivity.yml",
"~/.connectivity.yaml",
"/etc/connectivity.yml",
"/etc/connectivity.yaml"}
func FindConfig() (string, error) {
for _, path := range ConfigPaths {
if _, err := os.Stat(path); err == nil {
return path, nil
}
}
return "", errors.New("Failed to locate a config file: ./connectivity.yml ~/.connectivity.yml or /etc/connectivity.yml")
}
func LoadConfig(path string) *Config {
if path == "" {
return &Config{}
}
f, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Failed to open config file (%s): %v", path, err)
}
log.Printf("Loading config from %s", path)
var configMap map[string]string
err = yaml.Unmarshal(f, &configMap)
if err != nil {
log.Fatalf("Failed to parse YAML config file (%s): %v", path, err)
}
var cfg Config
// Extract the URL labels & values from the struct
for k, v := range configMap {
cfg.URLs = append(cfg.URLs, Url{Label: k, Url: v})
}
// Apply some default values
if cfg.StatsdHost == "" {
cfg.StatsdHost = "127.0.0.1"
}
if cfg.StatsdPort == 0 {
cfg.StatsdPort = 8125
}
if cfg.StatsdProtocol == "" {
cfg.StatsdProtocol = "udp"
}
return &cfg
}