-
Notifications
You must be signed in to change notification settings - Fork 414
/
config.go
174 lines (150 loc) · 4.3 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"time"
)
const configFileSizeLimit = 10 << 20
var defaultConfig = &struct {
netTimeout int64
fileDeadtime string
}{
netTimeout: 15,
fileDeadtime: "24h",
}
type Config struct {
Network NetworkConfig `json:network`
Files []FileConfig `json:files`
}
type NetworkConfig struct {
Servers []string `json:servers`
SSLCertificate string `json:"ssl certificate"`
SSLKey string `json:"ssl key"`
SSLCA string `json:"ssl ca"`
Timeout int64 `json:timeout`
timeout time.Duration
}
type FileConfig struct {
Paths []string `json:paths`
Fields map[string]string `json:fields`
DeadTime string `json:"dead time"`
deadtime time.Duration
}
func DiscoverConfigs(file_or_directory string) (files []string, err error) {
fi, err := os.Stat(file_or_directory)
if err != nil {
return nil, err
}
files = make([]string, 0)
if fi.IsDir() {
entries, err := ioutil.ReadDir(file_or_directory)
if err != nil {
return nil, err
}
for _, filename := range entries {
files = append(files, path.Join(file_or_directory, filename.Name()))
}
} else {
files = append(files, file_or_directory)
}
return files, nil
}
// Append values to the 'to' config from the 'from' config, erroring
// if a value would be overwritten by the merge.
func MergeConfig(to *Config, from Config) (err error) {
to.Network.Servers = append(to.Network.Servers, from.Network.Servers...)
to.Files = append(to.Files, from.Files...)
// TODO: Is there a better way to do this in Go?
if from.Network.SSLCertificate != "" {
if to.Network.SSLCertificate != "" {
return fmt.Errorf("SSLCertificate already defined as '%s' in previous config file", to.Network.SSLCertificate)
}
to.Network.SSLCertificate = from.Network.SSLCertificate
}
if from.Network.SSLKey != "" {
if to.Network.SSLKey != "" {
return fmt.Errorf("SSLKey already defined as '%s' in previous config file", to.Network.SSLKey)
}
to.Network.SSLKey = from.Network.SSLKey
}
if from.Network.SSLCA != "" {
if to.Network.SSLCA != "" {
return fmt.Errorf("SSLCA already defined as '%s' in previous config file", to.Network.SSLCA)
}
to.Network.SSLCA = from.Network.SSLCA
}
if from.Network.Timeout != 0 {
if to.Network.Timeout != 0 {
return fmt.Errorf("Timeout already defined as '%d' in previous config file", to.Network.Timeout)
}
to.Network.Timeout = from.Network.Timeout
}
return nil
}
func LoadConfig(path string) (config Config, err error) {
config_file, err := os.Open(path)
if err != nil {
emit("Failed to open config file '%s': %s\n", path, err)
return
}
fi, _ := config_file.Stat()
if size := fi.Size(); size > (configFileSizeLimit) {
emit("config file (%q) size exceeds reasonable limit (%d) - aborting", path, size)
return // REVU: shouldn't this return an error, then?
}
if fi.Size() == 0 {
emit("config file (%q) is empty, skipping", path)
return
}
buffer := make([]byte, fi.Size())
_, err = config_file.Read(buffer)
emit("%s\n", buffer)
buffer, err = StripComments(buffer)
if err != nil {
emit("Failed to strip comments from json: %s\n", err)
return
}
buffer = []byte(os.ExpandEnv(string(buffer)))
err = json.Unmarshal(buffer, &config)
if err != nil {
emit("Failed unmarshalling json: %s\n", err)
return
}
for k, _ := range config.Files {
if config.Files[k].DeadTime == "" {
config.Files[k].DeadTime = defaultConfig.fileDeadtime
}
config.Files[k].deadtime, err = time.ParseDuration(config.Files[k].DeadTime)
if err != nil {
emit("Failed to parse dead time duration '%s'. Error was: %s\n", config.Files[k].DeadTime, err)
return
}
}
return
}
func FinalizeConfig(config *Config) {
if config.Network.Timeout == 0 {
config.Network.Timeout = defaultConfig.netTimeout
}
config.Network.timeout = time.Duration(config.Network.Timeout) * time.Second
}
func StripComments(data []byte) ([]byte, error) {
data = bytes.Replace(data, []byte("\r"), []byte(""), 0) // Windows
lines := bytes.Split(data, []byte("\n"))
filtered := make([][]byte, 0)
for _, line := range lines {
match, err := regexp.Match(`^\s*#`, line)
if err != nil {
return nil, err
}
if !match {
filtered = append(filtered, line)
}
}
return bytes.Join(filtered, []byte("\n")), nil
}