forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
55 lines (43 loc) · 1.33 KB
/
util.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
package harvester
import (
"regexp"
"time"
"github.com/elastic/beats/filebeat/harvester/processor"
"github.com/elastic/beats/libbeat/logp"
)
// readLine reads a full line into buffer and returns it.
// In case of partial lines, readLine does return and error and en empty string
// This could potentialy be improved / replaced by https://github.com/elastic/beats/libbeat/tree/master/common/streambuf
func readLine(reader processor.LineProcessor) (time.Time, string, int, error) {
for {
l, err := reader.Next()
// Full line read to be returned
if l.Bytes != 0 && err == nil {
logp.Debug("harvester", "full line read")
return l.Ts, string(l.Content), l.Bytes, err
}
return time.Time{}, "", 0, err
}
}
// InitRegexps initializes a list of compiled regular expressions.
func InitRegexps(exprs []string) ([]*regexp.Regexp, error) {
result := []*regexp.Regexp{}
for _, exp := range exprs {
rexp, err := regexp.CompilePOSIX(exp)
if err != nil {
logp.Err("Fail to compile the regexp %s: %s", exp, err)
return nil, err
}
result = append(result, rexp)
}
return result, nil
}
// MatchAnyRegexps checks if the text matches any of the regular expressions
func MatchAnyRegexps(regexps []*regexp.Regexp, text string) bool {
for _, rexp := range regexps {
if rexp.MatchString(text) {
return true
}
}
return false
}