-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig_yaml.go
More file actions
86 lines (74 loc) · 1.94 KB
/
Copy pathconfig_yaml.go
File metadata and controls
86 lines (74 loc) · 1.94 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
/*
* Copyright (c) 2016 Yanko Bolanos
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
package main
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"regexp"
log "github.com/Sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// scan for regexes
var exp1 = regexp.MustCompile(`line_regex\:\s?\#(?P<logfile>[^\s]+)\s(?P<exp>.*)`)
var exp2 = regexp.MustCompile(`front_split_regex\:\s?\#(?P<logfile>[^\s]+)\s(?P<exp>.*)`)
var exp3 = regexp.MustCompile(`kv_regex\:\s?\#(?P<logfile>[^\s]+)\s(?P<exp>.*)`)
func parseYamlConfig(src io.Reader) ConfigFile {
data, err := ioutil.ReadAll(src)
if err != nil {
log.Fatalf(err.Error())
}
config := ConfigFile{}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatalf("error: %v", err)
}
r := bufio.NewReader(bytes.NewReader(data))
for {
line, _, err := r.ReadLine()
if err != nil && err == io.EOF {
break
} else if err != nil {
log.Fatalf(err.Error())
}
setConfigLogfileRegex(config, exp1, line)
setConfigLogfileRegex(config, exp2, line)
setConfigLogfileRegex(config, exp3, line)
}
gApp = config.App
setAppVer(config.AppVer)
return config
}
func setConfigLogfileRegex(config ConfigFile, regex *regexp.Regexp, line []byte) {
matches := regex.FindSubmatch(line)
if len(matches) == 3 {
logfileName := string(matches[1])
expstr := string(matches[2])
exp := regexp.MustCompile(expstr)
for n, logfile := range config.Logfiles {
if logfile.Name == logfileName {
switch regex {
case exp1:
config.Logfiles[n].KvRegex = exp
config.Logfiles[n].KvRegexStr = expstr
break
case exp2:
config.Logfiles[n].FrontSplitRegex = exp
config.Logfiles[n].FrontSplitRegexStr = expstr
break
case exp3:
config.Logfiles[n].KvRegex = exp
config.Logfiles[n].KvRegexStr = expstr
break
}
}
}
}
}