-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathdetector.go
49 lines (42 loc) · 1.58 KB
/
detector.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
package detector
import (
"github.com/Checkmarx/kics/pkg/model"
"github.com/rs/zerolog"
)
type kindDetectLine interface {
DetectLine(file *model.FileMetadata, searchKey string,
logWithFields *zerolog.Logger, outputLines int) model.VulnerabilityLines
}
// DetectLine is a struct that associates a kindDetectLine to its FileKind
type DetectLine struct {
detectors map[model.FileKind]kindDetectLine
outputLines int
logWithFields *zerolog.Logger
defaultDetector kindDetectLine
}
// NewDetectLine creates a new DetectLine's reference
func NewDetectLine(outputLines int) *DetectLine {
return &DetectLine{
detectors: make(map[model.FileKind]kindDetectLine),
logWithFields: &zerolog.Logger{},
outputLines: outputLines,
defaultDetector: defaultDetectLine{},
}
}
// SetupLogs will change the logger feild to be used in kindDetectLine DetectLine method
func (d *DetectLine) SetupLogs(logger *zerolog.Logger) {
d.logWithFields = logger
}
// Add adds a new kindDetectLine to the caller and returns it
func (d *DetectLine) Add(detector kindDetectLine, kind model.FileKind) *DetectLine {
d.detectors[kind] = detector
return d
}
// DetectLine will use the correct kindDetectLine according to the files kind
// if file kind is not in detectors default detect line is called
func (d *DetectLine) DetectLine(file *model.FileMetadata, searchKey string) model.VulnerabilityLines {
if det, ok := d.detectors[file.Kind]; ok {
return det.DetectLine(file, searchKey, d.logWithFields, d.outputLines)
}
return d.defaultDetector.DetectLine(file, searchKey, d.logWithFields, d.outputLines)
}