-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
111 lines (94 loc) · 2.39 KB
/
config.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
package main
import (
"fmt"
"traductio/internal/inputreader"
"traductio/internal/sink"
"gopkg.in/yaml.v2"
)
type Config struct {
Input inputreader.InputConfig `yaml:"input"`
Validators Validators `yaml:"validators"`
Process ProcessConfig `yaml:"process"`
Output sink.Config `yaml:"output"`
}
type Validators []Validator
func (v Validators) ValidateContent(data []byte) (bool, []error) {
errs := []error{}
ok := true
for _, check := range v {
v, err := queryBytes(data, check.Selector)
if err != nil {
ok = false
errs = append(errs, err)
}
value := string(v)
if value != check.Expect {
ok = false
err = fmt.Errorf("value at '%s' is expected to be '%s', is '%s'", check.Selector, value, check.Expect)
errs = append(errs, err)
}
}
return ok, errs
}
type Validator struct {
Selector string `yaml:"selector"`
Expect string `yaml:"expect"`
}
type ProcessConfig struct {
Iterator Iterator `yaml:"iterator"`
NoTrim bool `yaml:"no_trim"`
}
type Iterator struct {
Selector string `yaml:"selector"`
Time TimeSet `yaml:"time"`
Tags map[string]string `yaml:"tags"`
FixedTags map[string]string `yaml:"fixed_tags"`
Values map[string]string `yaml:"values"`
FixedValues map[string]float64 `yaml:"fixed_values"`
Iterator *Iterator `yaml:"iterator"`
}
type TimeSet struct {
Selector string `yaml:"selector"`
Format string `yaml:"format"`
}
func (i Iterator) GetStructure() (tags []string, values []string) {
for key := range i.Tags {
tags = append(tags, key)
}
for key := range i.FixedTags {
tags = append(tags, key)
}
for key := range i.Values {
values = append(values, key)
}
for key := range i.FixedValues {
values = append(values, key)
}
if i.Iterator != nil {
t, v := i.Iterator.GetStructure()
tags = append(tags, t...)
values = append(values, v...)
}
return
}
func ReadConfig(cfgFile string) (Config, error) {
c := Config{}
i, err := inputreader.NewInput(inputreader.InputConfig{URL: cfgFile}, map[string]string{})
if err != nil {
return c, err
}
data, err := i.Fetch()
if err != nil {
return c, err
}
err = yaml.Unmarshal(data, &c)
if err != nil {
err = fmt.Errorf("error while parsing %s: %s", cfgFile, err.Error())
return c, err
}
return c, nil
}
func (c Config) String() string {
b, _ := yaml.Marshal(c)
return string(b)
}