-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathconfigparser.go
More file actions
447 lines (372 loc) · 10.5 KB
/
configparser.go
File metadata and controls
447 lines (372 loc) · 10.5 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright (c) 2013 - Alex Yu <alex@alexyu.se>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package configparser provides a simple parser for reading/writing configuration (INI) files.
//
// Supports reading/writing the INI file format in addition to:
//
// - Reading/writing duplicate section names (ex: MySQL NDB engine's config.ini)
// - Options without values (ex: can be used to group a set of hostnames)
// - Options without a named section (ex: a simple option=value file)
// - Find sections with regexp pattern matching on section names, ex: dc1.east.webservers where regex is '.webservers'
// - # or ; as comment delimiter
// - = or : as value delimiter
//
package configparser
import (
"bufio"
"container/list"
"errors"
"fmt"
"os"
"path"
"regexp"
"strings"
"sync"
)
// Delimiter is the delimiter to be used between section key and values.
var Delimiter = "="
// Configuration represents a configuration file with its sections and options.
type Configuration struct {
filePath string // configuration file
sections map[string]*list.List // fully qualified section name as key
orderedSections []string // track the order of section names as they are parsed
mutex sync.RWMutex
}
// A Section in a configuration.
type Section struct {
fqn string
options map[string]string
orderedOptions []string // track the order of the options as they are parsed
mutex sync.RWMutex
}
// NewConfiguration returns a new Configuration instance with an empty file path.
func NewConfiguration() *Configuration {
return newConfiguration("")
}
// Read parses a specified configuration file and returns a Configuration instance.
func Read(filePath string) (*Configuration, error) {
filePath = path.Clean(filePath)
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
config := newConfiguration(filePath)
activeSection := config.addSection("global")
scanner := bufio.NewScanner(bufio.NewReader(file))
for scanner.Scan() {
// TODO: maybe trim spaces here
line := scanner.Text()
if (len(line) < 0) {
continue
}
if isSection(line) {
fqn := strings.Trim(line, " []")
activeSection = config.addSection(fqn)
continue
}
// save options and comments
addOption(activeSection, line)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return config, nil
}
// Save the Configuration to file. Creates a backup (.bak) if file already exists.
func Save(c *Configuration, filePath string) (err error) {
c.mutex.Lock()
err = os.Rename(filePath, filePath+".bak")
if err != nil {
if !os.IsNotExist(err) { // fine if the file does not exists
return err
}
}
f, err := os.Create(filePath)
if err != nil {
return err
}
defer func() {
err = f.Close()
}()
w := bufio.NewWriter(f)
defer func() {
err = w.Flush()
}()
c.mutex.Unlock()
s, err := c.AllSections()
if err != nil {
return err
}
c.mutex.Lock()
defer c.mutex.Unlock()
for _, v := range s {
w.WriteString(v.String())
}
return err
}
// NewSection creates and adds a new Section with the specified name.
func (c *Configuration) NewSection(fqn string) *Section {
return c.addSection(fqn)
}
// FilePath returns the configuration file path.
func (c *Configuration) FilePath() string {
return c.filePath
}
// SetFilePath sets the Configuration file path.
func (c *Configuration) SetFilePath(filePath string) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.filePath = filePath
}
// StringValue returns the string value for the specified section and option.
func (c *Configuration) StringValue(section, option string) (value string, err error) {
s, err := c.Section(section)
if err != nil {
return
}
value = s.ValueOf(option)
return
}
// Delete deletes the specified sections matched by a regex name and returns the deleted sections.
func (c *Configuration) Delete(regex string) (sections []*Section, err error) {
sections, err = c.Find(regex)
c.mutex.Lock()
defer c.mutex.Unlock()
if err == nil {
for _, s := range sections {
delete(c.sections, s.fqn)
}
// remove also from ordered list
var matched bool
for i := len(c.orderedSections) - 1; i >= 0; i-- {
if matched, err = regexp.MatchString(regex, c.orderedSections[i]); matched {
c.orderedSections = append(c.orderedSections[:i], c.orderedSections[i+1:]...)
} else {
if err != nil {
return nil, err
}
}
}
}
return sections, err
}
// Section returns the first section matching the fully qualified section name.
func (c *Configuration) Section(fqn string) (*Section, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
if l, ok := c.sections[fqn]; ok {
for e := l.Front(); e != nil; e = e.Next() {
s := e.Value.(*Section)
return s, nil
}
}
return nil, errors.New("Unable to find " + fqn)
}
// AllSections returns a slice of all sections available.
func (c *Configuration) AllSections() ([]*Section, error) {
return c.Sections("")
}
// Sections returns a slice of Sections matching the fully qualified section name.
func (c *Configuration) Sections(fqn string) ([]*Section, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
var sections []*Section
f := func(lst *list.List) {
for e := lst.Front(); e != nil; e = e.Next() {
s := e.Value.(*Section)
sections = append(sections, s)
}
}
if fqn == "" {
// Get all sections.
for _, fqn := range c.orderedSections {
if lst, ok := c.sections[fqn]; ok {
f(lst)
}
}
} else {
if lst, ok := c.sections[fqn]; ok {
f(lst)
} else {
return nil, errors.New("Unable to find " + fqn)
}
}
return sections, nil
}
// Find returns a slice of Sections matching the regexp against the section name.
func (c *Configuration) Find(regex string) ([]*Section, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
var sections []*Section
for key, lst := range c.sections {
if matched, err := regexp.MatchString(regex, key); matched {
for e := lst.Front(); e != nil; e = e.Next() {
s := e.Value.(*Section)
sections = append(sections, s)
}
} else {
if err != nil {
return nil, err
}
}
}
return sections, nil
}
// PrintSection prints a text representation of all sections matching the fully qualified section name.
func (c *Configuration) PrintSection(fqn string) {
c.mutex.RLock()
defer c.mutex.RUnlock()
sections, err := c.Sections(fqn)
if err == nil {
for _, section := range sections {
fmt.Print(section)
}
} else {
fmt.Printf("Unable to find section %v\n", err)
}
}
// String returns the text representation of a parsed configuration file.
func (c *Configuration) String() string {
c.mutex.RLock()
defer c.mutex.RUnlock()
var parts []string
for _, fqn := range c.orderedSections {
sections, _ := c.Sections(fqn)
for _, section := range sections {
parts = append(parts, section.String())
}
}
return strings.Join(parts, "")
}
// Name returns the name of the section
func (s *Section) Name() string {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.fqn
}
// Exists returns true if the option exists
func (s *Section) Exists(option string) (ok bool) {
s.mutex.RLock()
defer s.mutex.RUnlock()
_, ok = s.options[option]
return
}
// ValueOf returns the value of specified option.
func (s *Section) ValueOf(option string) string {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.options[option]
}
// SetValueFor sets the value for the specified option and returns the old value.
func (s *Section) SetValueFor(option string, value string) string {
s.mutex.Lock()
defer s.mutex.Unlock()
var oldValue string
oldValue, s.options[option] = s.options[option], value
return oldValue
}
// Add adds a new option to the section. Adding and existing option will overwrite the old one.
// The old value is returned
func (s *Section) Add(option string, value string) (oldValue string) {
s.mutex.Lock()
defer s.mutex.Unlock()
var ok bool
if oldValue, ok = s.options[option]; !ok {
s.orderedOptions = append(s.orderedOptions, option)
}
s.options[option] = value
return oldValue
}
// Delete removes the specified option from the section and returns the deleted option's value.
func (s *Section) Delete(option string) (value string) {
s.mutex.Lock()
defer s.mutex.Unlock()
value = s.options[option]
delete(s.options, option)
for i, opt := range s.orderedOptions {
if opt == option {
s.orderedOptions = append(s.orderedOptions[:i], s.orderedOptions[i+1:]...)
}
}
return value
}
// Options returns a map of options for the section.
func (s *Section) Options() map[string]string {
return s.options
}
// OptionNames returns a slice of option names in the same order as they were parsed.
func (s *Section) OptionNames() []string {
return s.orderedOptions
}
// String returns the text representation of a section with its options.
func (s *Section) String() string {
s.mutex.RLock()
defer s.mutex.RUnlock()
var parts []string
sName := "[" + s.fqn + "]\n"
if s.fqn == "global" {
sName = ""
}
parts = append(parts, sName)
for _, opt := range s.orderedOptions {
value := s.options[opt]
if value != "" {
parts = append(parts, opt, Delimiter, value, "\n")
} else {
parts = append(parts, opt, "\n")
}
}
return strings.Join(parts, "")
}
//
// Private
//
// newConfiguration creates a new Configuration instance.
func newConfiguration(filePath string) *Configuration {
return &Configuration{
filePath: filePath,
sections: make(map[string]*list.List),
}
}
func isSection(section string) bool {
return strings.HasPrefix(section, "[")
}
func addOption(s *Section, option string) {
var opt, value string
if opt, value = parseOption(option); value != "" {
s.options[opt] = value
} else {
// only insert keys. ex list of hosts
s.options[opt] = ""
}
s.orderedOptions = append(s.orderedOptions, opt)
}
func parseOption(option string) (opt, value string) {
split := func(i int, delim string) (opt, value string) {
// strings.Split cannot handle wsrep_provider_options settings
opt = strings.Trim(option[:i], " ")
value = strings.Trim(option[i+1:], " ")
return
}
if i := strings.Index(option, "="); i != -1 {
opt, value = split(i, "=")
} else if i := strings.Index(option, ":"); i != -1 {
opt, value = split(i, ":")
} else {
opt = option
}
return
}
func (c *Configuration) addSection(fqn string) *Section {
section := &Section{fqn: fqn, options: make(map[string]string)}
var lst *list.List
if lst = c.sections[fqn]; lst == nil {
lst = list.New()
c.sections[fqn] = lst
c.orderedSections = append(c.orderedSections, fqn)
}
lst.PushBack(section)
return section
}