-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
232 lines (195 loc) · 5.88 KB
/
config.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
package elio
import (
"fmt"
"strings"
"github.com/spf13/viper"
)
// Config config
type Config struct {
viper *viper.Viper
}
// String object to string
func (c *Config) String() string {
return fmt.Sprintf("Config::%p", c)
}
// NewConfig new config
func NewConfig() (c *Config) {
c = new(Config)
if nil != c {
c.viper = viper.New()
}
return c
}
/*// viper config precedence order
Viper uses the following precedence order. Each item takes precedence over the item below it:
* explicit call to Set
* flag
* env
* config
* key/value store
* default
//*/
// Load load
func (c *Config) Load(path string) (err error) {
c.viper.AddConfigPath("./")
c.viper.AutomaticEnv()
AppTrace().Str(LogObject, c.String()).Msgf("begin to load config:%s", path)
c.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) // .env notOK, env Ok
if "" != path {
c.viper.SetConfigFile(path)
c.viper.SetConfigType("yaml")
err = c.viper.ReadInConfig()
if nil != err {
return err
}
}
// bind env
err = c.viper.BindEnv("elio.log.level", "ELIO_LOG_LEVEL")
err = c.viper.BindEnv("elio.log.out", "ELIO_LOG_OUT")
err = c.viper.BindEnv("elio.log.json", "ELIO_LOG_JSON")
err = c.viper.BindEnv("elio.log.color", "ELIO_LOG_COLOR")
err = c.viper.BindEnv("elio.log.shortCaller", "ELIO_LOG_SHORTCALLER")
err = c.viper.BindEnv("elio.app.intervalMs", "ELIO_APP_INTERVALMS")
err = c.viper.BindEnv("elio.app.fetchLimit", "ELIO_APP_FETCHLIMIT")
// viper expects SYSENV_* as system environment variables
c.viper.SetEnvPrefix(viper.GetString("sysenv"))
c.viper.SetConfigType("env")
//c.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) // .env notOK, env Ok
if nil == c.viper.MergeInConfig() {
}
////////////////////////////////////////////////////////////////
// // .env override 에 이슈가 없는 코드 블럭
//AppTrace().Str(elio.LogObject, c.String()).Msg("begin to load env var")
c.viper.SetConfigFile(".env")
//c.viper.SetEnvKeyReplacer(strings.NewReplacer("_", ".")) // .env OK, env notOk
if nil == c.viper.MergeInConfig() {
}
////////////////////////////////////////////////////////////////
return err
}
// Exists check key exists
func (c *Config) Exists(path string) bool {
return c.viper.IsSet(path)
}
// Get get
func (c *Config) Get(key string) interface{} {
return c.viper.Get(key)
}
// GetOrDefault get or default
func (c *Config) GetOrDefault(key string, d interface{}) (interface{}, bool) {
if c.Exists(key) {
return c.viper.Get(key), true
}
return d, false
}
// Set set interface value of key
func (c *Config) Set(key string, value interface{}) {
c.viper.Set(key, value)
}
// GetString get string value of key
func (c *Config) GetString(key string) string {
return c.viper.GetString(key)
}
// GetStringOrDefault get string or default value of key
func (c *Config) GetStringOrDefault(key string, d string) (string, bool) {
if c.Exists(key) {
return c.viper.GetString(key), true
}
return d, false
}
// // GetString get string value of key
// func (c *Config) GetString(key string) string {
// return c.viper.GetString(key)
// }
// GetBool get bool value of key
func (c *Config) GetBool(key string) bool {
return c.viper.GetBool(key)
}
// GetBoolOrDefault get bool or default value or of key
func (c *Config) GetBoolOrDefault(key string, d bool) (bool, bool) {
if c.Exists(key) {
return c.viper.GetBool(key), true
}
return d, false
}
// SetBool set bool value of key
func (c *Config) SetBool(key string, value bool) {
c.viper.Set(key, value)
}
// GetInt get int value of key
func (c *Config) GetInt(key string) int {
return c.viper.GetInt(key)
}
// GetIntOrDefault get int or default value of key
func (c *Config) GetIntOrDefault(key string, d int) (int, bool) {
if c.Exists(key) {
return c.viper.GetInt(key), true
}
return d, false
}
// GetUint get uint value of key
func (c *Config) GetUint(key string) uint {
return c.viper.GetUint(key)
}
// GetUintOrDefault get uint or default value of key
func (c *Config) GetUintOrDefault(key string, d uint) (uint, bool) {
if c.Exists(key) {
return c.viper.GetUint(key), true
}
return d, false
}
// GetInt32 get int32 value of key
func (c *Config) GetInt32(key string) int32 {
return c.viper.GetInt32(key)
}
// GetInt32OrDefault get int32 or default value of key
func (c *Config) GetInt32OrDefault(key string, d int32) (int32, bool) {
if c.Exists(key) {
return c.viper.GetInt32(key), true
}
return d, false
}
// GetUint32 get Uint32 value of key
func (c *Config) GetUint32(key string) uint32 {
return c.viper.GetUint32(key)
}
// GetUint32OrDefault get uint32 or default value of key
func (c *Config) GetUint32OrDefault(key string, d uint32) (uint32, bool) {
if c.Exists(key) {
return c.viper.GetUint32(key), true
}
return d, false
}
// GetInt64 get int64 value of key
func (c *Config) GetInt64(key string) int64 {
return c.viper.GetInt64(key)
}
// GetInt64OrDefault get int64 or default value of key
func (c *Config) GetInt64OrDefault(key string, d int64) (int64, bool) {
if c.Exists(key) {
return c.viper.GetInt64(key), true
}
return d, false
}
// GetUint64 get uint64 value of key
func (c *Config) GetUint64(key string) uint64 {
return c.viper.GetUint64(key)
}
// GetUint64OrDefault get uint64 or default value of key
func (c *Config) GetUint64OrDefault(key string, d uint64) (uint64, bool) {
if c.Exists(key) {
return c.viper.GetUint64(key), true
}
return d, false
}
// GetFloat64 get float64 value of key
func (c *Config) GetFloat64(key string) float64 {
return c.viper.GetFloat64(key)
}
// GetFloat64OrDefault get float64 or default value of key
func (c *Config) GetFloat64OrDefault(key string, d float64) (float64, bool) {
if c.Exists(key) {
return c.viper.GetFloat64(key), true
}
return d, false
}