-
Notifications
You must be signed in to change notification settings - Fork 0
/
viper.go
182 lines (154 loc) · 3.6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package viper
import (
"errors"
"fmt"
"time"
"github.com/advancedlogic/easy/interfaces"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func WithName(name string) interfaces.ConfigurationOption {
return func(i interfaces.Configuration) error {
if name != "" {
v := i.(*Viper)
v.name = name
return nil
}
return errors.New("provider cannot be empty")
}
}
func WithProvider(provider string) interfaces.ConfigurationOption {
return func(i interfaces.Configuration) error {
if provider != "" {
v := i.(*Viper)
v.provider = provider
return nil
}
return errors.New("provider cannot be empty")
}
}
func WithURI(uri string) interfaces.ConfigurationOption {
return func(i interfaces.Configuration) error {
if uri != "" {
v := i.(*Viper)
v.uri = uri
return nil
}
return errors.New("uri cannot be empty")
}
}
func WithLogger(logger *logrus.Logger) interfaces.ConfigurationOption {
return func(i interfaces.Configuration) error {
if logger != nil {
v := i.(*Viper)
v.Logger = logger
return nil
}
return errors.New("logger cannot be nil")
}
}
type Viper struct {
*viper.Viper
*logrus.Logger
name string
provider string
uri string
}
func New(options ...interfaces.ConfigurationOption) (*Viper, error) {
v := &Viper{
Viper: viper.New(),
Logger: logrus.New(),
}
for _, option := range options {
if err := option(v); err != nil {
return nil, err
}
}
return v, nil
}
func (v *Viper) Open(paths ...string) error {
v.SetConfigName(v.name)
if v.provider != "" && v.uri != "" {
if err := v.AddRemoteProvider(v.provider, v.uri, v.name); err != nil {
return err
}
if err := v.ReadRemoteConfig(); err != nil {
return err
}
} else {
v.AddConfigPath(fmt.Sprintf("/etc/%s/", v.name))
v.AddConfigPath(fmt.Sprintf("$HOME/.%s", v.name))
for _, path := range paths {
v.AddConfigPath(path)
}
v.AddConfigPath(".")
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
return err
}
}
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
err := v.ReadInConfig()
if err != nil {
return
}
})
return nil
}
func (v *Viper) Save(data interface{}) error {
return nil
}
func (v *Viper) Set(key string, value interface{}) error {
return nil
}
func (v *Viper) Get(key string) (interface{}, error) {
value, err := v.Get(key)
if err != nil {
return nil, err
}
return value, nil
}
func (v *Viper) GetIntOrDefault(path string, defaultValue int) int {
if value := v.GetInt(path); value != 0 {
return value
}
return defaultValue
}
func (v *Viper) GetBoolOrDefault(path string, defaultValue bool) bool {
if value := v.GetBool(path); value {
return value
}
return defaultValue
}
func (v *Viper) GetFloat64OrDefault(path string, defaultValue float64) float64 {
if value := v.GetFloat64(path); value != 0.0 {
return value
}
return defaultValue
}
func (v *Viper) GetDurationOrDefault(path string, defaultValue time.Duration) time.Duration {
if value := v.GetDuration(path); value != 0.0 {
return value
}
return defaultValue
}
func (v *Viper) GetStringOrDefault(path string, defaultValue string) string {
if value := v.GetString(path); value != "" {
return value
}
return defaultValue
}
func (v *Viper) GetArrayOfStringsOrDefault(path string, defaultValue []string) []string {
if value := v.GetStringSlice(path); value != nil {
return value
}
return defaultValue
}
func (v *Viper) GetMapOfStringOrDefault(path string, defaultValue map[string]string) map[string]string {
if value := v.Viper.GetStringMapString(path); value != nil {
return value
}
return defaultValue
}