-
Notifications
You must be signed in to change notification settings - Fork 4
/
search_check_builder.go
58 lines (54 loc) · 1.49 KB
/
search_check_builder.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
package bun
import (
"fmt"
)
// SearchCheckBuilder builds a check which searches for the specified
// string in the the specified files. If the pattern
// is found, the check is considered problematic.
// The number of the found line and its content appear in the Check.Problems of the check.
// The check searches only for the first appearance of the line.
type SearchCheckBuilder struct {
Name string `yaml:"name"` // Required
Description string `yaml:"description"` // Optional
FileTypeName string `yaml:"fileTypeName"` // Required
SearchString string `yaml:"searchString"` // Required
}
// Build creates a bun.Check.
func (b SearchCheckBuilder) Build() Check {
if b.FileTypeName == "" {
panic("FileTypeName should be specified.")
}
if b.SearchString == "" {
panic("SearchString should be set.")
}
builder := CheckBuilder{
Name: b.Name,
Description: b.Description,
Aggregate: DefaultAggregate,
}
t := GetFileType(b.FileTypeName)
for _, dirType := range t.DirTypes {
switch dirType {
case DTMaster:
builder.CollectFromMasters = b.collect
case DTAgent:
builder.CollectFromAgents = b.collect
case DTPublicAgent:
builder.CollectFromPublicAgents = b.collect
}
}
return builder.Build()
}
func (b SearchCheckBuilder) collect(host Host) (ok bool, details interface{},
err error) {
n, line, err := host.FindLine(b.FileTypeName, b.SearchString)
if err != nil {
return
}
if n != 0 {
details = fmt.Sprintf("%v: %v", n, line)
return
}
ok = true
return
}