-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
83 lines (69 loc) · 1.46 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
package main
import (
"bytes"
"io/ioutil"
"example.org/config"
"example.org/logger"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
func MergeConfig(c config.Config) (config.Config, error) {
f, err := loadConfigFromFile(c.ConfigFile)
if err != nil {
logger.Log.Warn("config file error, exiting",
zap.String("file", c.ConfigFile),
zap.Error(err),
)
return c, err
}
logger.Log.Info("loading config flags")
c = loadConfigFromFlags(c, f)
return c, nil
}
func loadConfigFromFile(f string) (config.Config, error) {
c := config.New()
logger.Log.Info("loading config from file",
zap.String("file", f),
)
a, err := ioutil.ReadFile(f)
if err != nil {
logger.Log.Warn("error reading config file",
zap.String("file", f),
zap.Error(err),
)
return c, err
}
b, err := ioutil.ReadAll(bytes.NewReader(a))
if err != nil {
logger.Log.Warn("error loading config file",
zap.String("file", f),
zap.Error(err),
)
return c, err
}
err = yaml.Unmarshal(b, &c)
if err != nil {
logger.Log.Warn("error parsing config file",
zap.String("file", f),
zap.Error(err),
)
return c, err
}
c.ConfigFile = f
return c, nil
}
func loadConfigFromFlags(c config.Config, f config.Config) config.Config {
if c.Web.ListenAddress < 1 {
f.Web.ListenAddress = c.Web.ListenAddress
}
if c.Web.MetricsPath != "" {
f.Web.MetricsPath = c.Web.MetricsPath
}
if c.Timeout != "" {
f.Timeout = c.Timeout
}
if c.Level != "" {
f.Level = c.Level
}
return f
}