-
Notifications
You must be signed in to change notification settings - Fork 6
/
source.go
65 lines (55 loc) · 1.71 KB
/
source.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
package parser
import (
"io/ioutil"
"strings"
"github.com/twtiger/gosecco/tree"
)
// Source represents a source of parsing data
type Source interface {
Parse() (tree.RawPolicy, error)
}
// FileSource represents the source of parsing coming from a file
type FileSource struct {
// Filename is the name of the file to parse definitions from
Filename string
}
// StringSource contains the definitions as a string
type StringSource struct {
// Name is the name to report for this string during parsing errors
Name string
// Content is the actual string containing definitions
Content string
}
// CombinedSource allow you to combine more than one source and have them parsed as a unit
type CombinedSource struct {
// Sources is a list of the sources to parse
Sources []Source
}
// CombineSources returns a CombinedSource with all the given sources
func CombineSources(s ...Source) *CombinedSource {
return &CombinedSource{s}
}
// Parse implements the Source interface by parsing the file
func (s *FileSource) Parse() (tree.RawPolicy, error) {
file, err := ioutil.ReadFile(s.Filename)
if err != nil {
return tree.RawPolicy{}, err
}
return parseLines(s.Filename, strings.Split(string(file), "\n"))
}
// Parse implements the Source interface by parsing the string
func (s *StringSource) Parse() (tree.RawPolicy, error) {
return parseLines(s.Name, strings.Split(s.Content, "\n"))
}
// Parse implements the Source interface by parsing each one of the sources
func (s *CombinedSource) Parse() (tree.RawPolicy, error) {
var result []interface{}
for _, s := range s.Sources {
rp, e := s.Parse()
if e != nil {
return tree.RawPolicy{}, e
}
result = append(result, rp.RuleOrMacros...)
}
return tree.RawPolicy{result}, nil
}