-
Notifications
You must be signed in to change notification settings - Fork 16
/
ini.go
358 lines (290 loc) · 7.06 KB
/
ini.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
Package ini is a ini config file/data manage implement
Source code and other details for the project are available at GitHub:
https://github.com/gookit/ini
INI parser is: https://github.com/gookit/ini/parser
*/
package ini
import (
"errors"
"io/ioutil"
"os"
"regexp"
"strings"
"sync"
)
// some default constants
const (
SepSection = "."
DefTagName = "ini"
)
var (
errEmptyKey = errors.New("ini: key name cannot be empty")
errNotFound = errors.New("ini: key does not exist in the config")
errReadonly = errors.New("ini: config manager instance in 'readonly' mode")
// default instance
dc = New()
)
// Section in INI config
type Section map[string]string
// Ini config data manager
type Ini struct {
err error
opts *Options
lock sync.RWMutex
data map[string]Section
// regex for match user var
varRegex *regexp.Regexp
}
/*************************************************************
* config instance
*************************************************************/
// New a config instance, with default options
func New() *Ini {
return &Ini{
data: make(map[string]Section),
opts: newDefaultOptions(),
}
}
// NewWithOptions new an instance and with some options
//
// Usage:
//
// ini.NewWithOptions(ini.ParseEnv, ini.Readonly)
func NewWithOptions(opts ...func(*Options)) *Ini {
c := New()
// apply options
c.WithOptions(opts...)
return c
}
// Default config instance
func Default() *Ini { return dc }
// ResetStd instance
func ResetStd() { dc = New() }
func (c *Ini) ensureInit() {
if !c.IsEmpty() {
return
}
if c.data == nil {
c.data = make(map[string]Section)
}
if c.opts == nil {
c.opts = newDefaultOptions()
}
// build var regex. default is `%\(([\w-:]+)\)s`
if c.opts.ParseVar && c.varRegex == nil {
// regexStr := `%\([\w-:]+\)s`
l := regexp.QuoteMeta(c.opts.VarOpen)
r := regexp.QuoteMeta(c.opts.VarClose)
// build like: `%\(([\w-:]+)\)s`
regStr := l + `([\w-` + c.opts.SectionSep + `]+)` + r
c.varRegex = regexp.MustCompile(regStr)
}
}
/*************************************************************
* options func
*************************************************************/
// GetOptions get options info.
//
// Notice: return is value. so, cannot change Ini instance
func GetOptions() Options {
return dc.Options()
}
// Options get options info.
//
// Notice: return is value. so, cannot change options
func (c *Ini) Options() Options {
return *c.opts
}
// WithOptions apply some options
func WithOptions(opts ...func(*Options)) {
dc.WithOptions(opts...)
}
// WithOptions apply some options
func (c *Ini) WithOptions(opts ...func(*Options)) {
if !c.IsEmpty() {
panic("ini: cannot set options after data has been load")
}
// apply options
for _, opt := range opts {
opt(c.opts)
}
}
// DefSection get default section name
func DefSection() string {
return dc.opts.DefSection
}
// DefSection get default section name
func (c *Ini) DefSection() string {
return c.opts.DefSection
}
/*************************************************************
* data load
*************************************************************/
// LoadFiles load data from files
func LoadFiles(files ...string) error { return dc.LoadFiles(files...) }
// LoadFiles load data from files
func (c *Ini) LoadFiles(files ...string) (err error) {
c.ensureInit()
for _, file := range files {
err = c.loadFile(file, false)
if err != nil {
return
}
}
return
}
// LoadExists load files, will ignore not exists
func LoadExists(files ...string) error { return dc.LoadExists(files...) }
// LoadExists load files, will ignore not exists
func (c *Ini) LoadExists(files ...string) (err error) {
c.ensureInit()
for _, file := range files {
err = c.loadFile(file, true)
if err != nil {
return
}
}
return
}
// LoadStrings load data from strings
func LoadStrings(strings ...string) error { return dc.LoadStrings(strings...) }
// LoadStrings load data from strings
func (c *Ini) LoadStrings(strings ...string) (err error) {
c.ensureInit()
for _, str := range strings {
err = c.parse(str)
if err != nil {
return
}
}
return
}
// LoadData load data map
func LoadData(data map[string]Section) error { return dc.LoadData(data) }
// LoadData load data map
func (c *Ini) LoadData(data map[string]Section) (err error) {
c.ensureInit()
if len(c.data) == 0 {
c.data = data
return
}
// append or override setting data
for name, sec := range data {
err = c.SetSection(name, sec)
if err != nil {
return
}
}
return
}
func (c *Ini) loadFile(file string, loadExist bool) (err error) {
// open file
fd, err := os.Open(file)
if err != nil {
// skip not exist file
if os.IsNotExist(err) && loadExist {
return nil
}
return
}
//noinspection GoUnhandledErrorResult
defer fd.Close()
// read file content
bts, err := ioutil.ReadAll(fd)
if err == nil {
err = c.parse(string(bts))
if err != nil {
return
}
}
return
}
/*************************************************************
* helper methods
*************************************************************/
// HasKey check key exists
func HasKey(key string) bool { return dc.HasKey(key) }
// HasKey check key exists
func (c *Ini) HasKey(key string) (ok bool) {
_, ok = c.GetValue(key)
return
}
// Delete value by key
func Delete(key string) bool { return dc.Delete(key) }
// Delete value by key
func (c *Ini) Delete(key string) (ok bool) {
if c.opts.Readonly {
return
}
key = c.formatKey(key)
if key == "" {
return
}
sec, key := c.splitSectionAndKey(key)
mp, ok := c.data[sec]
if !ok {
return
}
// key in a section
if _, ok = mp[key]; ok {
delete(mp, key)
c.data[sec] = mp
}
return
}
// Reset all data for the default
func Reset() { dc.Reset() }
// Reset all data
func (c *Ini) Reset() {
c.data = make(map[string]Section)
}
// IsEmpty config data is empty
func IsEmpty() bool { return len(dc.data) == 0 }
// IsEmpty config data is empty
func (c *Ini) IsEmpty() bool {
return len(c.data) == 0
}
// Data get all data from default instance
func Data() map[string]Section { return dc.data }
// Data get all data
func (c *Ini) Data() map[string]Section {
return c.data
}
// Error get
func Error() error { return dc.Error() }
// Error get
func (c *Ini) Error() error {
return c.err
}
/*************************************************************
* internal helper methods
*************************************************************/
func (c *Ini) splitSectionAndKey(key string) (string, string) {
sep := c.opts.SectionSep
// default find from default Section
name := c.opts.DefSection
// get val by path. eg "log.dir"
if strings.Contains(key, sep) {
ss := strings.SplitN(key, sep, 2)
name, key = strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
}
return name, key
}
// format key by some options
func (c *Ini) formatKey(key string) string {
sep := c.opts.SectionSep
key = strings.Trim(strings.TrimSpace(key), sep)
if c.opts.IgnoreCase {
key = strings.ToLower(key)
}
return key
}
func mapKeyToLower(src map[string]string) map[string]string {
newMp := make(map[string]string)
for k, v := range src {
k = strings.ToLower(k)
newMp[k] = v
}
return newMp
}