-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
331 lines (285 loc) · 7.21 KB
/
conf.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package ela
import (
"fmt"
"github.com/gogather/com"
// "github.com/gogather/com/log"
"errors"
"regexp"
"strconv"
"strings"
)
type Config struct {
conf map[string]map[string]interface{}
path string
rawContent string
content string
rawArrayContainer []string
warning []string
}
func NewConfig(path string) *Config {
conf := newConfig()
conf.path = path
conf.parseIniFile()
return conf
}
func NewEmptyConfig() *Config {
return newConfig()
}
func newConfig() *Config {
cfg := &Config{}
cfg.conf = map[string]map[string]interface{}{}
return cfg
}
func (cfg *Config) ReloadConfig(path string) {
cfg.path = path
cfg.parseIniFile()
}
func (cfg *Config) readConfigFile() (string, error) {
rawContent, err := com.ReadFileString(cfg.path)
cfg.rawContent = rawContent
return cfg.rawContent, err
}
// filter the code comment
func (cfg *Config) filterComment() string {
reg := regexp.MustCompile(`[#;][\d\D][^\n]*\n*`)
rep := []byte("\n")
cfg.content = string(reg.ReplaceAll([]byte(cfg.rawContent), rep))
return cfg.content
}
// split lines into array
func (cfg *Config) arraylize() []string {
cfg.rawArrayContainer = strings.Split(cfg.content, "\n")
return cfg.rawArrayContainer
}
// parse array items as config items
func (cfg *Config) parseItems() {
count := len(cfg.rawArrayContainer)
cfg.conf["_"] = map[string]interface{}{}
cfg.warning = nil
currentSection := "_"
for i := 0; i < count; i++ {
item := cfg.rawArrayContainer[i]
item = strings.TrimSpace(item)
hasEqualMark, err1 := regexp.Match(`=`, []byte(item))
hasSectionMark, err2 := regexp.Match(`\[[\d\D][^\[\]]+]$`, []byte(item))
switch {
case len(item) <= 0:
//empty line, skip
case hasEqualMark && (err1 == nil):
//normal key value item
reg := regexp.MustCompile(`([\d\D][^=]+)=([\d\D]+)$`)
kvArray := reg.FindSubmatch([]byte(item))
if len(kvArray) > 2 {
key := strings.TrimSpace(string(kvArray[1]))
value := strings.TrimSpace(string(kvArray[2]))
cfg.conf[currentSection][key] = cfg.parseValue(value)
}
case hasSectionMark && (err2 == nil):
// section mark line
reg := regexp.MustCompile(`\[([\d\D][^\[\]]+)]$`)
result := reg.FindSubmatch([]byte(item))
if len(result) > 1 {
currentSection = string(result[1])
cfg.conf[currentSection] = map[string]interface{}{}
}
default:
cfg.warning = append(cfg.warning, fmt.Sprintf("INI file SyntaxError in Line %d", i+1))
}
}
}
// parse value
func (cfg *Config) parseValue(content string) interface{} {
reg := regexp.MustCompile(`\"([\d\D][^\"]+)"$`)
result := reg.FindSubmatch([]byte(content))
if len(result) > 1 {
return string(result[1])
}
boolValue, err := strconv.ParseBool(content)
if err == nil {
return boolValue
}
intValue, err := strconv.ParseInt(content, 0, 64)
if err == nil {
return intValue
}
floatValue, err := strconv.ParseFloat(content, 64)
if err == nil {
return floatValue
}
return content
}
// parse ini file
func (cfg *Config) parseIniFile() (map[string]map[string]interface{}, error) {
_, err := cfg.readConfigFile()
if err != nil {
return nil, err
} else {
cfg.filterComment()
cfg.arraylize()
cfg.parseItems()
return cfg.conf, nil
}
}
// serialize config as ini file
func (cfg *Config) serialize() string {
sectionContentMap := map[string]string{}
content := ""
for section, val := range cfg.conf {
if section == "_" {
sectionMap := val
sectionContent := ""
for key, value := range sectionMap {
sectionContent = sectionContent + fmt.Sprintf("%s = %v\n", key, value)
}
sectionContentMap["_"] = sectionContent
} else {
// section title
title := "\n[" + section + "]\n"
sectionMap := val
sectionContent := ""
for key, value := range sectionMap {
sectionContent = sectionContent + fmt.Sprintf("%s = %v\n", key, value)
}
sectionContentMap[section] = title + sectionContent
}
}
for section, sectionVal := range sectionContentMap {
if section == "_" {
content = sectionVal + content
} else {
content = content + sectionVal
}
}
return content
}
func (cfg *Config) GetWarnings() []string {
return cfg.warning
}
func (cfg *Config) Get(section, key string) (interface{}, error) {
sectionMap, ok := cfg.conf[section]
if !ok {
return nil, fmt.Errorf("section %s not exist", section)
}
value, ok := sectionMap[key]
if !ok {
return nil, fmt.Errorf("key %s in section %s not exist", key, section)
}
return value, nil
}
func (cfg *Config) GetBool(section, key string) (bool, error) {
value, err := cfg.Get(section, key)
if err != nil {
return false, err
}
valueBool, ok := value.(bool)
if !ok {
valueString, ok := value.(string)
if ok {
if strings.ToLower(valueString) == "true" {
return true, nil
} else {
return false, nil
}
} else {
return false, errors.New("value not bool type")
}
} else {
return valueBool, nil
}
}
func (cfg *Config) GetInt(section, key string) (int64, error) {
value, err := cfg.Get(section, key)
if err != nil {
return 0, err
}
valueInt, ok := value.(int64)
if ok {
return valueInt, nil
} else {
return 0, errors.New("value not int type")
}
}
func (cfg *Config) GetFloat(section, key string) (float64, error) {
value, err := cfg.Get(section, key)
if err != nil {
return 0, err
}
valueFloat, ok := value.(float64)
if ok {
return valueFloat, nil
} else {
return 0, errors.New("value not float type")
}
}
func (cfg *Config) GetString(section, key string) (string, error) {
value, err := cfg.Get(section, key)
if err != nil {
return "", err
}
valueString, ok := value.(string)
if ok {
return valueString, nil
} else {
return "", errors.New("value not string type")
}
}
func (cfg *Config) GetBoolDefault(section, key string, defaultValue bool) bool {
value, err := cfg.GetBool(section, key)
if err != nil {
return defaultValue
} else {
return value
}
}
func (cfg *Config) GetIntDefault(section, key string, defaultValue int64) int64 {
value, err := cfg.GetInt(section, key)
if err != nil {
return defaultValue
} else {
return value
}
}
func (cfg *Config) GetFloatDefault(section, key string, defaultValue float64) float64 {
value, err := cfg.GetFloat(section, key)
if err != nil {
return defaultValue
} else {
return value
}
}
func (cfg *Config) GetStringDefault(section, key string, defaultValue string) string {
value, err := cfg.GetString(section, key)
if err != nil {
return defaultValue
} else {
return value
}
}
func (cfg *Config) set(section, key string, value interface{}) {
sectionMap, ok := cfg.conf[section]
if !ok {
sectionMap = map[string]interface{}{}
}
sectionMap[key] = value
cfg.conf[section] = sectionMap
}
func (cfg *Config) SetInt(section, key string, value int64) {
cfg.set(section, key, value)
}
func (cfg *Config) SetBool(section, key string, value bool) {
cfg.set(section, key, value)
}
func (cfg *Config) SetFloat(section, key string, value float64) {
cfg.set(section, key, value)
}
func (cfg *Config) SetString(section, key string, value string) {
cfg.set(section, key, value)
}
func (cfg *Config) Save(path string) error {
content := cfg.serialize()
cfg.rawContent = content
cfg.path = path
cfg.content = content
cfg.arraylize()
return com.WriteFileWithCreatePath(path, cfg.content)
}