/
global.go
282 lines (226 loc) · 7.07 KB
/
global.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package viper
import (
"io"
"time"
"github.com/spf13/cast"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
// SupportedExts describes the supported configuration formats
// for the wrapper to avoid importing the spf13/viper package.
// NOTE: This is a copy of spf13/viper.SupportedExts,
// with the exception of the "ini" format, since its marshaller
// does not have a thread-safe implementation.
SupportedExts = []string{"dotenv", "env", "hcl", "json", "properties", "props", "prop", "toml", "yaml", "yml"}
// vi stores the Viper global instance.
vi = &Viper{viper: viper.GetViper()}
)
// AddConfigPath wraps viper's method.
func AddConfigPath(in string) {
vi.AddConfigPath(in)
}
// AddRemoteProvider wraps viper's method.
func AddRemoteProvider(provider, endpoint, path string) error {
return vi.AddRemoteProvider(provider, endpoint, path)
}
// AddSecureRemoteProvider wraps viper's method.
func AddSecureRemoteProvider(provider, endpoint, path, secret string) error {
return vi.AddSecureRemoteProvider(provider, endpoint, path, secret)
}
// AllKeys wraps viper's method.
func AllKeys() []string {
return vi.AllKeys()
}
// AllSettings wraps viper's method.
func AllSettings() map[string]interface{} {
return vi.AllSettings()
}
// BindPFlags wraps viper's method.
func BindPFlags(flags *pflag.FlagSet) error {
return vi.BindPFlags(flags)
}
// BindPFlag wraps viper's method.
func BindPFlag(key string, flag *pflag.Flag) error {
return vi.BindPFlag(key, flag)
}
// BindFlagValues wraps viper's method.
func BindFlagValues(flags viper.FlagValueSet) error {
return vi.BindFlagValues(flags)
}
// BindFlagValue wraps viper's method.
func BindFlagValue(key string, flag viper.FlagValue) error {
return vi.BindFlagValue(key, flag)
}
// BindEnv wraps viper's method.
func BindEnv(in ...string) error {
return vi.BindEnv(in...)
}
// Debug wraps viper's method.
func Debug() {
vi.Debug()
}
// Get wraps viper's method.
func Get(key string) interface{} {
return vi.Get(key)
}
// GetBool returns the value associated with the key as a boolean.
func GetBool(key string) bool {
return cast.ToBool(vi.Get(key))
}
// GetDuration returns the value associated with the key as a duration.
func GetDuration(key string) time.Duration {
return cast.ToDuration(vi.Get(key))
}
// GetFloat64 returns the value associated with the key as a float64.
func GetFloat64(key string) float64 {
return cast.ToFloat64(vi.Get(key))
}
// GetInt returns the value associated with the key as an integer.
func GetInt(key string) int {
return cast.ToInt(vi.Get(key))
}
// GetInt32 returns the value associated with the key as an integer.
func GetInt32(key string) int32 {
return cast.ToInt32(vi.Get(key))
}
// GetInt64 returns the value associated with the key as an integer.
func GetInt64(key string) int64 {
return cast.ToInt64(vi.Get(key))
}
// GetIntSlice returns the value associated with the key as a slice of int values.
func GetIntSlice(key string) []int {
return cast.ToIntSlice(vi.Get(key))
}
// GetSizeInBytes wraps viper's method.
func GetSizeInBytes(key string) uint {
return vi.GetSizeInBytes(key)
}
// GetString returns the value associated with the key as a string.
func GetString(key string) string {
return cast.ToString(vi.Get(key))
}
// GetStringMap returns the value associated with the key as a map of interfaces.
func GetStringMap(key string) map[string]interface{} {
return cast.ToStringMap(vi.Get(key))
}
// GetStringMapString returns the value associated with the key as a map of strings.
func GetStringMapString(key string) map[string]string {
return cast.ToStringMapString(vi.Get(key))
}
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
func GetStringMapStringSlice(key string) map[string][]string {
return cast.ToStringMapStringSlice(vi.Get(key))
}
// GetStringSlice returns the value associated with the key as a slice of strings.
func GetStringSlice(key string) []string {
return cast.ToStringSlice(vi.Get(key))
}
// GetTime returns the value associated with the key as time.
func GetTime(key string) time.Time {
return cast.ToTime(vi.Get(key))
}
// GetUint returns the value associated with the key as an unsigned integer.
func GetUint(key string) uint {
return cast.ToUint(vi.Get(key))
}
// GetUint32 returns the value associated with the key as an unsigned integer.
func GetUint32(key string) uint32 {
return cast.ToUint32(vi.Get(key))
}
// GetUint64 returns the value associated with the key as an unsigned integer.
func GetUint64(key string) uint64 {
return cast.ToUint64(vi.Get(key))
}
// GetViper returns the global viper instance.
func GetViper() *Viper {
return vi
}
// InConfig wraps viper's method.
func InConfig(key string) bool {
return vi.InConfig(key)
}
// IsSet wraps viper's method.
func IsSet(key string) bool {
return vi.IsSet(key)
}
// MergeConfig wraps viper's method.
func MergeConfig(in io.Reader) error {
return vi.MergeConfig(in)
}
// MergeConfigMap wraps viper's method.
func MergeConfigMap(cfg map[string]interface{}) error {
return vi.MergeConfigMap(cfg)
}
// MergeInConfig wraps viper's method.
func MergeInConfig() error {
return vi.MergeInConfig()
}
// ReadConfig wraps viper's method.
func ReadConfig(in io.Reader) error {
return vi.ReadConfig(in)
}
// ReadConfigFile wraps viper's method.
func ReadConfigFile(path string) error {
return vi.ReadConfigFile(path)
}
// ReadRemoteConfig wraps viper's method.
func ReadRemoteConfig() error {
return vi.ReadRemoteConfig()
}
// RegisterAlias wraps viper's method.
func RegisterAlias(alias string, key string) {
vi.RegisterAlias(alias, key)
}
// SafeWriteConfig wraps viper's method.
func SafeWriteConfig() error {
return vi.SafeWriteConfig()
}
// SafeWriteConfigAs wraps viper's method.
func SafeWriteConfigAs(filename string) error {
return vi.SafeWriteConfigAs(filename)
}
// Set wraps viper's method.
func Set(key string, val interface{}) {
vi.Set(key, val)
}
// SetDefault wraps viper's method.
func SetDefault(key string, val interface{}) {
vi.SetDefault(key, val)
}
// Sub wraps viper's method.
func Sub(key string) *Viper {
return vi.Sub(key)
}
// Unmarshal wraps viper's method.
func Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return vi.Unmarshal(rawVal, opts...)
}
// UnmarshalExact wraps viper's method.
func UnmarshalExact(rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return vi.UnmarshalExact(rawVal, opts...)
}
// UnmarshalKey wraps viper's method.
func UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error {
return vi.UnmarshalKey(key, rawVal, opts...)
}
// WatchConfig wraps viper's method.
func WatchConfig() {
vi.WatchConfig()
}
// WatchRemoteConfig wraps viper's method.
func WatchRemoteConfig() error {
return vi.WatchRemoteConfig()
}
// WatchRemoteConfigOnChannel wraps viper's method.
func WatchRemoteConfigOnChannel() error {
return vi.WatchRemoteConfigOnChannel()
}
// WriteConfig wraps viper's method.
func WriteConfig() error {
return vi.WriteConfig()
}
// WriteConfigFile wraps viper's method.
func WriteConfigFile(filename string) error {
return vi.WriteConfigFile(filename)
}