forked from hashicorp/otto
/
parse.go
144 lines (120 loc) · 2.98 KB
/
parse.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package detect
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/mitchellh/mapstructure"
)
// Parse parses the detector config from the given io.Reader.
//
// Due to current internal limitations, the entire contents of the
// io.Reader will be copied into memory first before parsing.
func Parse(r io.Reader) (*Config, error) {
// Copy the reader into an in-memory buffer first since HCL requires it.
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return nil, err
}
// Parse the buffer
root, err := hcl.Parse(buf.String())
if err != nil {
return nil, fmt.Errorf("error parsing: %s", err)
}
buf.Reset()
// Top-level item should be the object list
list, ok := root.Node.(*ast.ObjectList)
if !ok {
return nil, fmt.Errorf("error parsing: file doesn't contain a root object")
}
var result Config
// Parse the detects
if o := list.Filter("detect"); len(o.Items) > 0 {
if err := parseDetect(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'import': %s", err)
}
}
return &result, nil
}
// ParseFile parses the given path as a single detector config.
func ParseFile(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return Parse(f)
}
// ParseDir parses all the files ending in ".hcl" in a directory,
// sorted alphabetically.
func ParseDir(path string) (*Config, error) {
// Read all the files
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
files, err := f.Readdirnames(-1)
f.Close()
if err != nil {
return nil, err
}
// Sort them
sort.Strings(files)
// Go through each, parse and merge.
var result Config
for _, f := range files {
// We only care if this is an HCL file
if filepath.Ext(f) != ".hcl" {
continue
}
// Stat the file. If it is a directory, ignore it
path := filepath.Join(path, f)
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if fi.IsDir() {
continue
}
// Parse
current, err := ParseFile(path)
if err != nil {
return nil, fmt.Errorf("error parsing %s: %s", path, err)
}
// Merge
if err := result.Merge(current); err != nil {
return nil, fmt.Errorf("error merging %s: %s", path, err)
}
}
return &result, nil
}
func parseDetect(result *Config, list *ast.ObjectList) error {
if len(list.Items) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*Detector, 0, len(list.Items))
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var d Detector
if err := mapstructure.WeakDecode(m, &d); err != nil {
return fmt.Errorf(
"error parsing detector '%s': %s", key, err)
}
d.Type = key
collection = append(collection, &d)
}
result.Detectors = collection
return nil
}