/
config.go
73 lines (61 loc) · 1.49 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
package bootstrap
import (
"io/ioutil"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
type Config interface {
GetNetwork() string
GetAddress() string
IsLoggerEnabled() bool
GetSecret() string
GetSecretWhitelist() []string
}
var _ Config = &ConfigStruct{}
type ConfigStruct struct {
Network string `yaml:"network"`
Address string `yaml:"address"`
LoggerEnabled bool `yaml:"logger-enabled"`
Secret string `yaml:"secret"`
SecretWhitelist []string `yaml:"secret-whitelist"`
}
func (cfg *ConfigStruct) GetNetwork() string {
return cfg.Network
}
func (cfg *ConfigStruct) GetAddress() string {
return cfg.Address
}
func (cfg *ConfigStruct) IsLoggerEnabled() bool {
return cfg.LoggerEnabled
}
func (cfg *ConfigStruct) GetSecret() string {
return cfg.Secret
}
func (cfg *ConfigStruct) GetSecretWhitelist() []string {
return cfg.SecretWhitelist
}
// DecodeConfig loads config from yaml bytes
func DecodeConfig(data []byte, cfg interface{}) error {
var tmp map[string]interface{}
err := yaml.Unmarshal(data, &tmp)
if err != nil {
return err
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
ErrorUnused: true,
TagName: "yaml",
Result: cfg,
})
if err != nil {
return err
}
return decoder.Decode(tmp)
}
// DecodeConfigFile loads config from yaml file
func DecodeConfigFile(path string, cfg interface{}) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return DecodeConfig(data, cfg)
}