-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathviper_adapter.go
220 lines (191 loc) · 6.77 KB
/
viper_adapter.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
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package config
import (
"fmt"
"io"
"strings"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
// ViperAdapter is DataProvider implementation that uses viper library under the hood.
type ViperAdapter struct {
viper *viper.Viper
}
var _ DataProvider = (*ViperAdapter)(nil)
// NewViperAdapter creates a new ViperAdapter.
func NewViperAdapter() *ViperAdapter {
return &ViperAdapter{viper.New()}
}
// UseEnvVars enables the ability to use environment variables for configuration parameters.
// Prefix defines what environment variables will be looked.
// E.g., if your prefix is "spf", the env registry will look for env
// variables that start with "SPF_".
func (va *ViperAdapter) UseEnvVars(prefix string) {
va.viper.AutomaticEnv()
va.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
va.viper.SetEnvPrefix(prefix)
}
// Set sets the value for the key in the override register.
func (va *ViperAdapter) Set(key string, value interface{}) {
va.viper.Set(key, value)
}
// SetDefault sets the default value for this key.
// Default only used when no value is provided by the user via config or ENV.
func (va *ViperAdapter) SetDefault(key string, value interface{}) {
va.viper.SetDefault(key, value)
}
// IsSet checks to see if the key has been set in any of the data locations.
// IsSet is case-insensitive for a key.
func (va *ViperAdapter) IsSet(key string) bool {
return va.viper.IsSet(key)
}
// Get retrieves any value given the key to use.
func (va *ViperAdapter) Get(key string) interface{} {
return va.viper.Get(key)
}
// SetFromFile specifies that discovering and loading configuration data will be performed from file.
func (va *ViperAdapter) SetFromFile(path string, dataType DataType) error {
va.viper.SetConfigType(string(dataType))
va.viper.SetConfigFile(path)
return va.viper.ReadInConfig()
}
// SetFromReader specifies that discovering and loading configuration data will be performed from reader.
func (va *ViperAdapter) SetFromReader(reader io.Reader, dataType DataType) error {
va.viper.SetConfigType(string(dataType))
return va.viper.ReadConfig(reader)
}
// GetInt tries to retrieve the value associated with the key as an integer.
func (va *ViperAdapter) GetInt(key string) (res int, err error) {
res, err = cast.ToIntE(va.Get(key))
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetIntSlice tries to retrieve the value associated with the key as a slice of integers.
func (va *ViperAdapter) GetIntSlice(key string) (res []int, err error) {
val := va.Get(key)
if val == nil {
return
}
res, err = cast.ToIntSliceE(val)
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetFloat32 tries to retrieve the value associated with the key as an float32.
func (va *ViperAdapter) GetFloat32(key string) (res float32, err error) {
res, err = cast.ToFloat32E(va.Get(key))
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetFloat64 tries to retrieve the value associated with the key as an float64.
func (va *ViperAdapter) GetFloat64(key string) (res float64, err error) {
res, err = cast.ToFloat64E(va.Get(key))
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetString tries to retrieve the value associated with the key as a string.
func (va *ViperAdapter) GetString(key string) (res string, err error) {
res, err = cast.ToStringE(va.Get(key))
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetBool tries to retrieve the value associated with the key as a bool.
func (va *ViperAdapter) GetBool(key string) (res bool, err error) {
res, err = cast.ToBoolE(va.Get(key))
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetStringSlice tries to retrieve the value associated with the key as an slice of strings.
func (va *ViperAdapter) GetStringSlice(key string) (res []string, err error) {
val := va.Get(key)
if val == nil {
return
}
res, err = cast.ToStringSliceE(val)
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetSizeInBytes tries to retrieve the value associated with the key as a size in bytes.
func (va *ViperAdapter) GetSizeInBytes(key string) (uint64, error) {
sizeStr, err := va.GetString(key)
if err != nil {
return 0, WrapKeyErrIfNeeded(key, err)
}
if sizeStr == "" {
return 0, nil
}
// Handle k8s power-of-two values.
for _, k8sByteSuffix := range [...]string{"Ki", "Mi", "Gi", "Ti", "Pi", "Ei"} {
if strings.HasSuffix(sizeStr, k8sByteSuffix) {
sizeStr = sizeStr[:len(sizeStr)-1]
break
}
}
res, err := bytefmt.ToBytes(sizeStr)
if err != nil {
return 0, WrapKeyErrIfNeeded(key, err)
}
return res, nil
}
// GetStringFromSet tries to retrieve the value associated with the key as a string from the specified set.
func (va *ViperAdapter) GetStringFromSet(key string, set []string, ignoreCase bool) (string, error) {
str, err := va.GetString(key)
if err != nil {
return "", WrapKeyErrIfNeeded(key, err)
}
for _, s := range set {
if (ignoreCase && strings.EqualFold(str, s)) || str == s {
return str, nil
}
}
return "", WrapKeyErrIfNeeded(key, fmt.Errorf("unknown value %q, should be one of %v", str, set))
}
// GetDuration tries to retrieve the value associated with the key as a duration.
func (va *ViperAdapter) GetDuration(key string) (res time.Duration, err error) {
res, err = cast.ToDurationE(va.Get(key))
err = WrapKeyErrIfNeeded(key, err)
return
}
// GetStringMapString tries to retrieve the value associated with the key as an map where key and value are strings.
func (va *ViperAdapter) GetStringMapString(key string) (res map[string]string, err error) {
val := va.Get(key)
if val == nil {
res = make(map[string]string)
return
}
res, err = cast.ToStringMapStringE(val)
err = WrapKeyErrIfNeeded(key, err)
return
}
// Unmarshal unmarshals the config into a Struct.
func (va *ViperAdapter) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) (err error) {
options := make([]viper.DecoderConfigOption, len(opts))
for i, opt := range opts {
options[i] = viper.DecoderConfigOption(opt)
}
err = va.viper.Unmarshal(rawVal, options...)
return
}
// UnmarshalKey takes a single key and unmarshals it into a Struct.
func (va *ViperAdapter) UnmarshalKey(key string, rawVal interface{}, opts ...DecoderConfigOption) (err error) {
options := make([]viper.DecoderConfigOption, len(opts))
for i, opt := range opts {
options[i] = viper.DecoderConfigOption(opt)
}
err = va.viper.UnmarshalKey(key, rawVal, options...)
err = WrapKeyErrIfNeeded(key, err)
return
}
// WrapKeyErr wraps error adding information about a key where this error occurs.
func (va *ViperAdapter) WrapKeyErr(key string, err error) error {
return WrapKeyErr(key, err)
}
// SaveToFile writes config into file according data type.
func (va *ViperAdapter) SaveToFile(path string, dataType DataType) error {
va.viper.SetConfigType(string(dataType))
return va.viper.WriteConfigAs(path)
}