forked from reddit/mcsauna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
54 lines (47 loc) · 1.38 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
package main
import (
"encoding/json"
"errors"
)
type RegexpConfig struct {
Name string `json:"name"`
Re string `json:"re"`
}
type Config struct {
Regexps []RegexpConfig `json:"regexps"`
Interval int `json:"interval"`
Interface string `json:"interface"`
Port int `json:"port"`
NumItemsToReport int `json:"num_items_to_report"`
Quiet bool `json:"quiet"`
OutputFile string `json:"output_file"`
ShowErrors bool `json:"show_errors"`
/* When using regexps, include a list of keys that did not match in the
* output. Useful for debugging regular expressions.
*/
ShowUnmatched bool `json:"show_unmatched"`
}
func NewConfig(config_data []byte) (config Config, err error) {
config = Config{
Regexps: []RegexpConfig{},
Interval: 5,
Interface: "any",
Port: 11211,
NumItemsToReport: 20,
Quiet: false,
ShowErrors: true,
ShowUnmatched: false,
}
err = json.Unmarshal(config_data, &config)
if err != nil {
return config, err
}
// Validate config
for _, regexp_config := range config.Regexps {
if regexp_config.Name == "" || regexp_config.Re == "" {
return config, errors.New(
"Config error: regular expressions must have both a 're' and 'name' field.")
}
}
return config, nil
}