-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviper.go
63 lines (56 loc) · 1.24 KB
/
viper.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
package giu
import "github.com/spf13/viper"
type ConfigParams struct {
ConfigName string
ConfigType string
ConfigPath []string
AutoEnv bool
}
var _defaultConfigParams = ConfigParams{
ConfigName: "config",
ConfigType: "yaml",
ConfigPath: []string{"."},
AutoEnv: true,
}
func NewLocalConfig(params ConfigParams) (*viper.Viper, error) {
v := viper.New()
v.SetConfigName(params.ConfigName)
if params.ConfigType != "" {
v.SetConfigType(params.ConfigType)
}
if len(params.ConfigPath) > 0 {
for _, path := range params.ConfigPath {
v.AddConfigPath(path)
}
}
if params.AutoEnv {
v.AutomaticEnv()
}
if err := v.ReadInConfig(); err != nil {
return nil, err
}
return v, nil
}
func DefaultConfig() (*viper.Viper, error) {
return NewLocalConfig(_defaultConfigParams)
}
type RemoteConfigParams struct {
Provider string
Endpoint string
Path string
ConfigType string
AutoEnv bool
}
func NewConfigFromRemote(params RemoteConfigParams) (*viper.Viper, error) {
v := viper.New()
if err := v.AddRemoteProvider(params.Provider, params.Endpoint, params.Path); err != nil {
return nil, err
}
if params.ConfigType != "" {
v.SetConfigType(params.ConfigType)
}
if params.AutoEnv {
v.AutomaticEnv()
}
return v, nil
}