-
Notifications
You must be signed in to change notification settings - Fork 33
/
reader.go
377 lines (360 loc) · 13.4 KB
/
reader.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
Copyright 2019 HAProxy Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package parser
import (
"bufio"
"bytes"
"io"
"os"
"strconv"
"strings"
"github.com/haproxytech/config-parser/v5/common"
"github.com/haproxytech/config-parser/v5/parsers/extra"
"github.com/haproxytech/config-parser/v5/types"
)
func (p *configParser) LoadData(path string) error {
if p.Options.Log {
p.Options.Logger.Debugf("%sreading data from %s", p.Options.LogPrefix, path)
}
p.Options.Path = path
dat, err := os.ReadFile(p.Options.Path)
if err != nil {
return err
}
return p.Process(bytes.NewReader(dat))
}
func (p *configParser) Process(reader io.Reader) error {
p.Init()
parsers := ConfiguredParsers{
State: "",
ActiveComments: nil,
Active: p.Parsers[Comments][CommentsSectionName],
Comments: p.Parsers[Comments][CommentsSectionName],
Global: p.Parsers[Global][GlobalSectionName],
}
bufferedScanner := bufio.NewScanner(reader)
var line string
if p.Options.Log {
p.Options.Logger.Debugf("%sprocessing of data started", p.Options.LogPrefix)
}
for bufferedScanner.Scan() {
line = bufferedScanner.Text()
if line == "" {
if parsers.State == "" {
parsers.State = "#"
}
continue
}
parts, comment := common.StringSplitWithCommentIgnoreEmpty(line)
if len(parts) == 0 && comment != "" {
switch {
case strings.HasPrefix(line, "# _version"):
parts = []string{"# _version"}
case strings.Contains(line, "config-snippet"):
parts = []string{"config-snippet"}
case strings.HasPrefix(line, "# _md5hash"):
parts = []string{"# _md5hash"}
default:
parts = []string{""}
}
}
if len(parts) == 0 {
continue
}
if p.Options.Log {
p.Options.Logger.Tracef("%sprocessing line: %s", p.Options.LogPrefix, line)
}
parsers = p.ProcessLine(line, parts, comment, parsers)
}
if parsers.ActiveComments != nil {
parsers.Active.PostComments = parsers.ActiveComments
}
if parsers.ActiveSectionComments != nil {
parsers.Active.PostComments = append(parsers.Active.PostComments, parsers.ActiveSectionComments...)
}
if p.Options.Log {
p.Options.Logger.Debugf("%sprocessing of data ended", p.Options.LogPrefix)
}
return nil
}
// ProcessLine parses line plus determines if we need to change state
func (p *configParser) ProcessLine(line string, parts []string, comment string, config ConfiguredParsers) ConfiguredParsers { //nolint:gocognit,gocyclo,cyclop,maintidx
if config.State != "" {
if parts[0] == "" && comment != "" && comment != "##_config-snippet_### BEGIN" && comment != "##_config-snippet_### END" {
if line[0] == ' ' {
if config.State != "snippet_beg" {
config.ActiveComments = append(config.ActiveComments, comment)
return config
}
} else {
config.ActiveSectionComments = append(config.ActiveSectionComments, comment)
return config
}
}
}
parsers := make([]ParserInterface, 0, 2)
if !p.Options.DisableUnProcessed {
parsers = append(parsers, config.Active.Parsers[""])
}
if config.HasDefaultParser {
// Default parser name is given in position 0 of ParserSequence
parsers = append(parsers, config.Active.Parsers[string(config.Active.ParserSequence[0])])
}
// We add iteratively the different parts to form a potential parser name
for i := 1; i <= len(parts) && !config.HasDefaultParser; i++ {
parserName := strings.Join(parts[:i], " ")
if parserName == "" {
continue
}
if parserFound, ok := config.Active.Parsers[parserName]; ok {
parsers = append(parsers, parserFound)
break
}
}
if len(parsers) < 2 && len(parts) == 1 && parts[0] == "" {
if parserFound, ok := config.Active.Parsers["#"]; ok {
parsers = append(parsers, parserFound)
}
}
if (len(parsers) < 2) && len(parts) > 0 && parts[0] == "no" {
for i := 2; i <= len(parts) && !config.HasDefaultParser; i++ {
if parserFound, ok := config.Active.Parsers[strings.Join(parts[1:i], " ")]; ok {
parsers = append(parsers, parserFound)
break
}
}
}
for i := len(parsers) - 1; i >= 0; i-- {
parser := parsers[i]
if p.Options.Log {
p.Options.Logger.Tracef("%susing parser [%s]", p.Options.LogPrefix, parser.GetParserName())
}
if newState, err := parser.PreParse(line, parts, config.ActiveComments, comment); err == nil {
if newState != "" {
if p.Options.Log {
p.Options.Logger.Debugf("%schange active section to %s\n", p.Options.LogPrefix, newState)
}
if config.ActiveComments != nil {
config.Active.PostComments = config.ActiveComments
}
config.State = newState
switch config.State {
case "":
config.Active = config.Comments
case "defaults":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
if data.Name == "" {
var name string
for i := 1; ; i++ {
name = "unnamed_defaults_" + strconv.Itoa(i)
_, exists := p.Parsers[Defaults][name]
if !exists {
break
}
}
data.Name = name
}
config.Defaults = p.getDefaultParser()
if data.FromDefaults != "" {
config.Defaults.DefaultSectionName = data.FromDefaults
}
p.Parsers[Defaults][data.Name] = config.Defaults
config.Active = config.Defaults
if p.Options.Log {
p.Options.Logger.Tracef("%defaults section %s active", p.Options.LogPrefix, data.Name)
}
if !p.Options.NoNamedDefaultsFrom {
p.lastDefaultsSectionName = data.Name
}
DefaultSectionName = data.Name
case "global":
config.Active = config.Global
case "frontend":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Frontend = p.getFrontendParser()
if data.FromDefaults != "" {
config.Frontend.DefaultSectionName = data.FromDefaults
} else {
config.Frontend.DefaultSectionName = p.lastDefaultsSectionName
}
p.Parsers[Frontends][data.Name] = config.Frontend
config.Active = config.Frontend
if p.Options.Log {
p.Options.Logger.Tracef("%sfrontend section %s active", p.Options.LogPrefix, data.Name)
}
case "backend":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Backend = p.getBackendParser()
if data.FromDefaults != "" {
config.Backend.DefaultSectionName = data.FromDefaults
} else {
config.Backend.DefaultSectionName = p.lastDefaultsSectionName
}
p.Parsers[Backends][data.Name] = config.Backend
config.Active = config.Backend
if p.Options.Log {
p.Options.Logger.Tracef("%sbackend section %s active", p.Options.LogPrefix, data.Name)
}
case "listen":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Listen = p.getListenParser()
if data.FromDefaults != "" {
config.Listen.DefaultSectionName = data.FromDefaults
} else {
config.Listen.DefaultSectionName = p.lastDefaultsSectionName
}
p.Parsers[Listen][data.Name] = config.Listen
config.Active = config.Listen
if p.Options.Log {
p.Options.Logger.Tracef("%slisten section %s active", p.Options.LogPrefix, data.Name)
}
case "resolvers":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Resolver = p.getResolverParser()
p.Parsers[Resolvers][data.Name] = config.Resolver
config.Active = config.Resolver
if p.Options.Log {
p.Options.Logger.Tracef("%sresolvers section %s active", p.Options.LogPrefix, data.Name)
}
case "userlist":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Userlist = p.getUserlistParser()
p.Parsers[UserList][data.Name] = config.Userlist
config.Active = config.Userlist
if p.Options.Log {
p.Options.Logger.Tracef("%suserlist section %s active", p.Options.LogPrefix, data.Name)
}
case "fcgi-app":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.FCGIApp = p.getFcgiAppParser()
p.Parsers[FCGIApp][data.Name] = config.FCGIApp
config.Active = config.FCGIApp
if p.Options.Log {
p.Options.Logger.Tracef("%sfcgi-app section %s active", p.Options.LogPrefix, data.Name)
}
case "peers":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Peers = p.getPeersParser()
p.Parsers[Peers][data.Name] = config.Peers
config.Active = config.Peers
if p.Options.Log {
p.Options.Logger.Tracef("%speers section %s active", p.Options.LogPrefix, data.Name)
}
case "mailers":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Mailers = p.getMailersParser()
p.Parsers[Mailers][data.Name] = config.Mailers
config.Active = config.Mailers
if p.Options.Log {
p.Options.Logger.Tracef("%smailers section %s active", p.Options.LogPrefix, data.Name)
}
case "cache":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Cache = p.getCacheParser()
p.Parsers[Cache][data.Name] = config.Cache
config.Active = config.Cache
if p.Options.Log {
p.Options.Logger.Tracef("%scache section %s active", p.Options.LogPrefix, data.Name)
}
case "program":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Program = p.getProgramParser()
p.Parsers[Program][data.Name] = config.Program
config.Active = config.Program
if p.Options.Log {
p.Options.Logger.Tracef("%sprogram section %s active", p.Options.LogPrefix, data.Name)
}
case "http-errors":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.HTTPErrors = p.getHTTPErrorsParser()
p.Parsers[HTTPErrors][data.Name] = config.HTTPErrors
config.Active = config.HTTPErrors
if p.Options.Log {
p.Options.Logger.Tracef("%shttp-errors section %s active", p.Options.LogPrefix, data.Name)
}
case "ring":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.Ring = p.getRingParser()
p.Parsers[Ring][data.Name] = config.Ring
config.Active = config.Ring
if p.Options.Log {
p.Options.Logger.Tracef("%sring section %s active", p.Options.LogPrefix, data.Name)
}
case "log-forward":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.LogForward = p.getLogForwardParser()
p.Parsers[LogForward][data.Name] = config.LogForward
config.Active = config.LogForward
if p.Options.Log {
p.Options.Logger.Tracef("%log-forward section %s active", p.Options.LogPrefix, data.Name)
}
case "crt-store":
parserSectionName := parser.(*extra.Section) //nolint:forcetypeassert
rawData, _ := parserSectionName.Get(false)
data := rawData.(*types.Section) //nolint:forcetypeassert
config.CrtStore = p.getCrtStoreParser()
p.Parsers[CrtStore][data.Name] = config.CrtStore
config.Active = config.CrtStore
if p.Options.Log {
p.Options.Logger.Tracef("%scrt-store section %s active", p.Options.LogPrefix, data.Name)
}
case "snippet_beg":
config.Previous = config.Active
config.Active = &Parsers{
Parsers: map[string]ParserInterface{"config-snippet": parser},
ParserSequence: []Section{"config-snippet"},
}
config.HasDefaultParser = true
case "snippet_end":
config.Active = config.Previous
config.HasDefaultParser = false
}
if config.ActiveSectionComments != nil {
config.Active.PreComments = config.ActiveSectionComments
config.ActiveSectionComments = nil
}
}
config.ActiveComments = nil
break
}
}
return config
}