-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.go
More file actions
345 lines (273 loc) · 7.88 KB
/
Copy pathconfig.go
File metadata and controls
345 lines (273 loc) · 7.88 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/mitchellh/mapstructure"
)
type configMap map[string]interface{}
// inputInfo represents the input specification for an ETL process
type inputInfo struct {
Type string
Config configMap
}
// outputInfo represents the output specification for an ETL process
type outputInfo struct {
Type string
Config configMap
}
// transformInfo represents the transform specification for an ETL process
type transformInfo struct {
Type string
Config configMap
}
// processInfo represents the specification for an ETL process
type processInfo struct {
Name string `hcl:",key"`
Input *inputInfo
Transforms []*transformInfo
Output *outputInfo
}
type unidataInfo struct {
Host string
Username string
Password string
UdtBin string `hcl:"udtbin,"`
UdtHome string `hcl:"udthome,"`
UdtAcct string `hcl:"udtacct,"`
}
// Config is the root configuration object that contains all ETL process specifications
type Config struct {
Processes []*processInfo `hcl:"process,"`
Unidata *unidataInfo
}
// Parse consumes a Reader and returns a Config object
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")
}
// Check for invalid keys
valid := []string{
"process",
"unidata",
}
if err := checkHCLKeys(list, valid); err != nil {
return nil, err
}
var result Config
// Parse the process configs
if o := list.Filter("process"); len(o.Items) > 0 {
if err := parseProcesses(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'process': %s", err)
}
}
// Parse the unidata config
if o := list.Filter("unidata"); len(o.Items) > 0 {
if err := parseUnidata(&result, o); err != nil {
return nil, fmt.Errorf("error parsing 'unidata': %s", err)
}
}
return &result, nil
}
func parseUnidata(result *Config, list *ast.ObjectList) error {
if len(list.Items) > 1 {
return fmt.Errorf("only one 'unidata' block allowed")
}
// Get our one item
item := list.Items[0]
// Check for invalid keys
valid := []string{"host", "username", "password", "udtbin", "udthome", "udtacct"}
if err := checkHCLKeys(item.Val, valid); err != nil {
return multierror.Prefix(err, "unidata:")
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
u := unidataInfo{}
result.Unidata = &u
return mapstructure.WeakDecode(m, &u)
}
func parseProcesses(result *Config, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
// Go through each object and turn it into an actual result.
collection := make([]*processInfo, 0, len(list.Items))
seen := make(map[string]struct{})
for _, item := range list.Items {
n, ok := item.Keys[0].Token.Value().(string)
if !ok {
return fmt.Errorf("process name must be a string, got %q", item.Keys[0].Token.Value())
}
// Make sure we haven't already found this
if _, ok := seen[n]; ok {
return fmt.Errorf("process '%s' defined more than once", n)
}
seen[n] = struct{}{}
// Check for invalid keys
valid := []string{"input", "output", "transform"}
if err := checkHCLKeys(item.Val, valid); err != nil {
return multierror.Prefix(err, fmt.Sprintf(
"process '%s':", n))
}
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return fmt.Errorf("process '%s': should be an object", n)
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var process processInfo
process.Name = n
// Parse input
if o := listVal.Filter("input"); len(o.Items) == 0 {
return fmt.Errorf("you must specify an 'input' block for process '%s'", process.Name)
} else if err := parseInputs(&process, o); err != nil {
return fmt.Errorf("error parsing 'input': %s", err)
}
// Parse transforms
if o := listVal.Filter("transform"); len(o.Items) > 0 {
if err := parseTransforms(&process, o); err != nil {
return fmt.Errorf("error parsing 'transform': %s", err)
}
}
// Parse outputs
if o := listVal.Filter("output"); len(o.Items) == 0 {
return fmt.Errorf("you must specify an 'output' block for process '%s'", process.Name)
} else if err := parseOutputs(&process, o); err != nil {
return fmt.Errorf("error parsing 'output': %s", err)
}
collection = append(collection, &process)
}
// Set the results
result.Processes = collection
return nil
}
func parseInputs(result *processInfo, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
return nil
}
item := list.Items[0]
if len(item.Keys) == 0 {
return fmt.Errorf("you may only specify a type for inputs")
}
key, ok := item.Keys[0].Token.Value().(string)
if !ok {
return fmt.Errorf("input name must be a string, got %q", item.Keys[0].Token.Value())
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var input inputInfo
input.Type = strings.ToLower(key)
input.Config = m
result.Input = &input
return nil
}
func parseTransforms(result *processInfo, list *ast.ObjectList) error {
// Go through each object and turn it into an actual result.
collection := make([]*transformInfo, 0, len(list.Items))
for _, item := range list.Items {
if len(item.Keys) == 0 {
return fmt.Errorf("you may only specify a type for transforms")
}
key, ok := item.Keys[0].Token.Value().(string)
if !ok {
return fmt.Errorf("transform name must be a string, got %q", item.Keys[0].Token.Value())
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var c transformInfo
c.Type = strings.ToLower(key)
c.Config = m
collection = append(collection, &c)
}
result.Transforms = collection
return nil
}
func parseOutputs(result *processInfo, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) != 1 {
return fmt.Errorf("you may only specify one 'output'")
}
item := list.Items[0]
if len(item.Keys) == 0 {
return fmt.Errorf("you may only specify a type for outputs")
}
key, ok := item.Keys[0].Token.Value().(string)
if !ok {
return fmt.Errorf("output name must be a string, got %q", item.Keys[0].Token.Value())
}
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
return err
}
var c outputInfo
c.Type = strings.ToLower(key)
c.Config = m
result.Output = &c
return nil
}
// LoadConfig loads configuration info from a file
func LoadConfig(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("opening configuration file (%s): %s", path, err)
}
config, err := Parse(file)
if err != nil {
return nil, fmt.Errorf("parsing configuration file (%s): %s", path, err)
}
return config, nil
}
func checkHCLKeys(node ast.Node, valid []string) error {
var list *ast.ObjectList
switch n := node.(type) {
case *ast.ObjectList:
list = n
case *ast.ObjectType:
list = n.List
default:
return fmt.Errorf("cannot check HCL keys of type %T", n)
}
validMap := make(map[string]struct{}, len(valid))
for _, v := range valid {
validMap[v] = struct{}{}
}
var result error
for _, item := range list.Items {
key := item.Keys[0].Token.Value().(string)
if _, ok := validMap[key]; !ok {
result = multierror.Append(result, fmt.Errorf(
"invalid key '%s' on line %d", key, item.Assign.Line))
}
}
return result
}