-
Notifications
You must be signed in to change notification settings - Fork 0
/
confparser.go
321 lines (262 loc) · 7.21 KB
/
confparser.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
package confparse
import (
"io"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
type IniParser struct {
p *parser
c *config
w *fsnotify.Watcher
fn func(ev fsnotify.Event)
name string
}
// New creates and parse a new configuration from a file name
// returns a valid parsed object and a nil, or an error and nil object
// in the successful case the values are ready to be retrieved
func New(confname string) (*IniParser, error) {
f, err := os.Open(confname)
if err != nil {
return nil, err
}
defer f.Close()
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
p := &IniParser{
p: newParser(f),
c: newConfig(),
w: watcher,
name: confname,
}
p.Parse()
return p, nil
}
// Parse actually parses the object content, note the object is always in a
// valid state, must be called if the Parser has been created with New, in
// case it has been created with NewFromFile it has already been parsed.
func (i *IniParser) Parse() {
var lastsection string
for {
item := i.p.Scan()
switch {
case item.Tok == EOF:
return
case item.Tok == KEY_VALUE:
i.c.C[lastsection][item.Values[0]] = item.Values[1]
case item.Tok == SECTION:
lastsection = item.Values[0]
i.c.C[item.Values[0]] = make(map[string]string, 0)
}
}
}
// Watch add a file system watcher on the config file itself and reloads the
// configuration and parses it every time a write event is received.
func (i *IniParser) Watch() error {
defer i.w.Close()
dir, file := filepath.Split(i.name)
if dir == "" {
cdir, err := os.Getwd()
if err != nil {
return err
}
dir = cdir
}
done := make(chan bool)
go i.eventFilter(file)
if err := i.w.Add(dir); err != nil {
return err
}
<-done
return nil
}
func (i *IniParser) eventFilter(file string) {
var err error
for {
select {
case ev := <-i.w.Events:
_, evfile := filepath.Split(ev.Name)
if file == evfile {
if ev.Op&fsnotify.Write == fsnotify.Write {
i, err = New(i.name)
if err != nil {
log.Println("Fatal: ", err)
return
}
}
i.fn(ev)
}
case err := <-i.w.Errors:
log.Println("Watcher error: ", err)
}
}
}
// OnConfChange accept a single parameter, a function that gets run right after
// every event and receive a copy of it.
func (i *IniParser) OnConfChange(run func(ev fsnotify.Event)) {
i.fn = run
}
// GetBool retrieves a bool value from named section with key name, returns
// either an error and an invalid value or a nil and a valid value.
func (i *IniParser) GetBool(sectionKey string) (bool, error) {
keys := strings.Split(sectionKey, ".")
value, err := i.c.getValue(keys[0], keys[1], i)
if err != nil {
return false, err
}
b, err := strconv.ParseBool(value)
if err != nil {
return false, newParserError(err.Error(), keys[0], keys[1], i.errorLine(keys[1]))
}
return b, nil
}
// GetInt retrieves a int64 value from named section with key name, returns
// either an error and an invalid value or a nil and a valid value.
func (i *IniParser) GetInt(sectionKey string) (int64, error) {
keys := strings.Split(sectionKey, ".")
value, err := i.c.getValue(keys[0], keys[1], i)
if err != nil {
return -1, err
}
n, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return -1, newParserError(err.Error(), keys[0], keys[1], i.errorLine(keys[1]))
}
return n, nil
}
// GetFloat retrieves a float64 value from named section with key name, returns
// either an error and an invalid value or a nil and a valid value.
func (i *IniParser) GetFloat(sectionKey string) (float64, error) {
keys := strings.Split(sectionKey, ".")
value, err := i.c.getValue(keys[0], keys[1], i)
if err != nil {
return -0.1, err
}
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return -1, newParserError(err.Error(), keys[0], keys[1], i.errorLine(keys[1]))
}
return f, nil
}
// GetString retrieves a string value from named section with key name, returns
// either an error and an invalid value or a nil and a valid value.
func (i *IniParser) GetString(sectionKey string) (string, error) {
keys := strings.Split(sectionKey, ".")
value, err := i.c.getValue(keys[0], keys[1], i)
if err != nil {
return "", err
}
return value, nil
}
// GetDuration retrieves a time.Duration value from the named section/key, returns either an error and an -1 or a valid duration and a nil error.
func (i *IniParser) GetDuration(sectionKey string) (time.Duration, error) {
keys := strings.Split(sectionKey, ".")
value, err := i.c.getValue(keys[0], keys[1], i)
if err != nil {
return -1, err
}
duration, err := time.ParseDuration(value)
if err != nil {
return -1, err
}
return duration, nil
}
// GetSlice retrieves a slice value from named section with key name, returns
// either an error and an invalid value or a nil and a valid value.
func (i *IniParser) GetSlice(sectionKey string) ([]string, error) {
keys := strings.Split(sectionKey, ".")
value, err := i.c.getValue(keys[0], keys[1], i)
if err != nil {
return nil, newParserError(err.Error(), keys[0], keys[1], i.errorLine(keys[1]))
}
return strings.Split(value, ","), nil
}
// GetSection retrieves an entire section coverting the values to the appropriate
// type is left to the user everything is a string
func (i *IniParser) GetSection(section string) (map[string]string, error) {
return i.c.getSection(section, i)
}
func (i *IniParser) errorLine(word string) int {
lineno, err := i.p.s.findLine(word)
if err == io.EOF {
return lineno
}
if err != nil {
return -1
}
return lineno
}
type parser struct {
s *lexer
buf struct {
tok token
values []string
n int
}
}
func newParser(r io.Reader) *parser {
return &parser{s: newLexer(r)}
}
func (p *parser) scan() (item *itemType) {
// If we have a token on the buffer, then return it.
if p.buf.values == nil {
p.buf.values = make([]string, 0)
}
if p.buf.n != 0 {
p.buf.n = 0
item.Tok = p.buf.tok
item.Values = append(item.Values, p.buf.values...)
}
// Otherwise read the next token from the scanner.
item = p.s.Scan()
// Save it to the buffer in case we unscan later.
p.buf.tok = item.Tok
p.buf.values = append(p.buf.values, item.Values...)
return
}
func (p *parser) unscan() { p.buf.n = 1 }
//Scan does not take into consideration white spaces ever nor should be
// called directly.
func (p *parser) Scan() (item *itemType) {
item = p.scan()
if item.Tok == WHITESPACE {
item = p.scan()
}
return
}
type config struct {
C map[string]map[string]string
}
func newConfig() *config {
conf := &config{C: make(map[string]map[string]string, 0)}
conf.C["default"] = make(map[string]string, 0)
conf.C["default"]["version"] = "0.1"
return conf
}
func (c *config) getValue(section, key string, i *IniParser) (string, error) {
sec, ok := c.C[section]
if !ok {
return "", newParserError(SEC_NOT_FOUND.Error(), section, key,
i.errorLine(key))
}
val, ok := sec[key]
if !ok {
return "", newParserError(KEY_NOT_FOUND.Error(), section, key,
i.errorLine(key))
}
return val, nil
}
func (c *config) getSection(section string, i *IniParser) (map[string]string, error) {
sectionm, ok := c.C[section]
if !ok {
return nil, newParserError(SEC_NOT_FOUND.Error(), section, "nokey",
i.errorLine(section))
}
return sectionm, nil
}