-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
47 lines (38 loc) · 890 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
package groot
import (
"os"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
)
type config struct {
LogLevel string `yaml:"log_level"`
LogFormat string `yaml:"log_format"`
InsecureRegistries []string `yaml:"insecure_registries"`
}
func parseConfig(configFilePath string) (conf config, err error) {
defer func() {
if err == nil {
conf = applyDefaults(conf)
}
}()
if configFilePath == "" {
return conf, nil
}
contents, err := os.ReadFile(configFilePath)
if err != nil {
return config{}, errors.Wrap(err, "reading config file")
}
if err := yaml.Unmarshal(contents, &conf); err != nil {
return config{}, errors.Wrap(err, "parsing config file")
}
return conf, nil
}
func applyDefaults(conf config) config {
if conf.LogLevel == "" {
conf.LogLevel = "info"
}
if conf.LogFormat == "" {
conf.LogFormat = "rfc3339"
}
return conf
}