-
Notifications
You must be signed in to change notification settings - Fork 0
/
viper.go
259 lines (210 loc) · 5.47 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
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
package viper
import (
"time"
"bytes"
"strings"
"github.com/spf13/viper"
"github.com/fsnotify/fsnotify"
"github.com/deatil/lakego-doak/lakego/path"
"github.com/deatil/lakego-doak/lakego/config/adapter"
)
// 构造函数
func New() *Viper {
v := &Viper{}
v.viper = viper.New()
return v
}
/**
* Viper 适配器
*
* @create 2021-9-25
* @author deatil
*/
type Viper struct {
adapter.Adapter
viper *viper.Viper
// 路径
path string
}
// 环境变量前缀
func (this *Viper) SetEnvPrefix(prefix string) {
this.viper.SetEnvPrefix(prefix)
this.viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}
// 环境变量
func (this *Viper) AutomaticEnv() {
this.viper.AutomaticEnv()
this.viper.AllowEmptyEnv(true)
}
// 设置文件夹
func (this *Viper) WithPath(path string) {
// 配置文件所在目录
this.path = path
}
// 配置路径
func (this *Viper) setConfigName(path string, name string, typ string) {
// 配置文件所在目录
this.viper.AddConfigPath(path)
this.viper.SetConfigName(name)
this.viper.SetConfigType(typ)
// 合并配置
this.viper.MergeInConfig()
}
// 配置文件
func (this *Viper) setConfigFile(file string) {
// 指定配置文件路径
this.viper.SetConfigFile(path.FormatPath(file))
// 合并配置
this.viper.MergeInConfig()
}
// 要读取的文件
func (this *Viper) WithFile(fileName ...string) {
/*
// 配置里读取
if err := this.viper.ReadInConfig(); err != nil {
panic("配置初始化失败:" + err.Error())
}
*/
// 设置配置文件类型(后缀)为 yml
nameType := ""
if len(fileName) > 1 {
nameType = fileName[1]
} else {
nameType = "yml"
}
if len(fileName) > 0 {
// 导入自定义配置
configFiles := adapter.GetPath(fileName[0])
if len(configFiles) > 0 {
for _, configFile := range configFiles {
// 指定配置文件路径
this.setConfigFile(path.FormatPath(configFile))
}
}
this.setConfigName(this.path, fileName[0], nameType)
}
this.viper.WatchConfig()
}
// 要读取的数据
func (this *Viper) WithBytes(data []byte, typ string) {
this.viper.SetConfigType(typ)
this.viper.ReadConfig(bytes.NewBuffer(data))
// 合并配置
this.viper.MergeInConfig()
}
// 获取
func (this *Viper) GetViper() *viper.Viper {
return this.viper
}
// 设置默认值
func (this *Viper) SetDefault(keyName string, value any) {
this.viper.SetDefault(keyName, value)
}
// 设置
func (this *Viper) Set(keyName string, value any) {
this.viper.Set(keyName, value)
}
// 是否设置
func (this *Viper) IsSet(keyName string) bool {
return this.viper.IsSet(keyName)
}
// Get 一个原始值
func (this *Viper) Get(keyName string) any {
value := this.viper.Get(keyName)
return value
}
// 事件
func (this *Viper) OnConfigChange(f func(string)) {
// 事件
this.viper.OnConfigChange(func(changeEvent fsnotify.Event) {
// WRITE
opString := changeEvent.Op.String()
f(opString)
})
}
// ================================================
// GetString
func (this *Viper) GetString(keyName string) string {
value := this.viper.GetString(keyName)
return value
}
// GetBool
func (this *Viper) GetBool(keyName string) bool {
value := this.viper.GetBool(keyName)
return value
}
// GetInt
func (this *Viper) GetInt(keyName string) int {
value := this.viper.GetInt(keyName)
return value
}
// GetInt32
func (this *Viper) GetInt32(keyName string) int32 {
value := this.viper.GetInt32(keyName)
return value
}
// GetInt64
func (this *Viper) GetInt64(keyName string) int64 {
value := this.viper.GetInt64(keyName)
return value
}
// GetUint
func (this *Viper) GetUint(keyName string) uint {
value := this.viper.GetUint(keyName)
return value
}
// GetUint32
func (this *Viper) GetUint32(keyName string) uint32 {
value := this.viper.GetUint32(keyName)
return value
}
// GetUint64
func (this *Viper) GetUint64(keyName string) uint64 {
value := this.viper.GetUint64(keyName)
return value
}
// float64
func (this *Viper) GetFloat64(keyName string) float64 {
value := this.viper.GetFloat64(keyName)
return value
}
// GetTime
func (this *Viper) GetTime(keyName string) time.Time {
value := this.viper.GetTime(keyName)
return value
}
// GetDuration
func (this *Viper) GetDuration(keyName string) time.Duration {
value := this.viper.GetDuration(keyName)
return value
}
// GetIntSlice
func (this *Viper) GetIntSlice(keyName string) []int {
value := this.viper.GetIntSlice(keyName)
return value
}
// GetStringSlice
func (this *Viper) GetStringSlice(keyName string) []string {
value := this.viper.GetStringSlice(keyName)
return value
}
// GetStringMap
func (this *Viper) GetStringMap(keyName string) map[string]any {
value := this.viper.GetStringMap(keyName)
return value
}
// GetStringMapString
func (this *Viper) GetStringMapString(keyName string) map[string]string {
value := this.viper.GetStringMapString(keyName)
return value
}
// GetStringMapStringSlice
func (this *Viper) GetStringMapStringSlice(keyName string) map[string][]string {
value := this.viper.GetStringMapStringSlice(keyName)
return value
}
// GetSizeInBytes, 暂未使用
func (this *Viper) GetSizeInBytes(keyName string) uint {
value := this.viper.GetSizeInBytes(keyName)
return value
}