blob: 4cde314d7ae630798c9f6cd9bc407b3dce46f7d2 (
plain)
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
|
package config
import (
"bytes"
"errors"
"strings"
"github.com/spf13/viper"
)
type Viper struct {
viper *viper.Viper
Path string
Prefix string
ReadInCfg []byte
}
// Inits config provider.
func (v *Viper) Init() error {
v.viper = viper.New()
// read in environment variables that match
v.viper.AutomaticEnv()
if v.Prefix == "" {
return errors.New("prefix should be set")
}
v.viper.SetEnvPrefix(v.Prefix)
if v.Path == "" {
return errors.New("path should be set")
}
v.viper.SetConfigFile(v.Path)
v.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if v.ReadInCfg != nil {
return v.viper.ReadConfig(bytes.NewBuffer(v.ReadInCfg))
}
return v.viper.ReadInConfig()
}
// Overwrite overwrites existing config with provided values
func (v *Viper) Overwrite(values map[string]interface{}) error {
if len(values) != 0 {
for key, value := range values {
v.viper.Set(key, value)
}
}
return nil
}
// UnmarshalKey reads configuration section into configuration object.
func (v *Viper) UnmarshalKey(name string, out interface{}) error {
err := v.viper.UnmarshalKey(name, &out)
if err != nil {
return err
}
return nil
}
// Get raw config in a form of config section.
func (v *Viper) Get(name string) interface{} {
return v.viper.Get(name)
}
// Has checks if config section exists.
func (v *Viper) Has(name string) bool {
return v.viper.IsSet(name)
}
|