-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
282 lines (241 loc) · 5.97 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
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
package main
import (
"encoding/json"
"fmt"
"math"
"os"
"strconv"
)
type config struct {
Listen ListenConfig
ClientConnInfo string
StartupParameters map[string]string
Databases VirtualDatabaseConfiguration
Prometheus PrometheusConfig
}
var Config = config{
// These are the defaults
Listen: ListenConfig{6433, "localhost", true},
ClientConnInfo: "host=localhost port=5432 sslmode=disable",
StartupParameters: nil,
Databases: nil,
Prometheus: PrometheusConfig{
Enabled: false,
Listen: ListenConfig{},
},
}
func readIntValue(dst *int, val interface{}, option string) error {
var err error
switch val := val.(type) {
case float64:
if math.Trunc(val) == val {
*dst = int(val)
} else {
err = fmt.Errorf("input must be an integer")
}
case string:
*dst, err = strconv.Atoi(val)
default:
err = fmt.Errorf("input must be an integer")
}
if err != nil {
return fmt.Errorf("invalid value for option %q: %s", option, err.Error())
}
return nil
}
func readTextValue(dst *string, val interface{}, option string) error {
var err error
switch val := val.(type) {
case string:
*dst = val
err = nil
default:
err = fmt.Errorf("input must be a text string")
}
if err != nil {
return fmt.Errorf("invalid value for option %q: %s", option, err.Error())
}
return nil
}
func readBooleanValue(dst *bool, val interface{}, option string) error {
var err error
switch val := val.(type) {
case bool:
*dst = val
err = nil
default:
err = fmt.Errorf("input must be a boolean")
}
if err != nil {
return fmt.Errorf("invalid value for option %q: %s", option, err.Error())
}
return nil
}
func readListenSection(c *ListenConfig, val interface{}, option string) error {
data, ok := val.(map[string]interface{})
if !ok {
return fmt.Errorf(`section %q must be a JSON object`, option)
}
for key, value := range data {
var err error
switch key {
case "port":
err = readIntValue(&c.Port, value, option + ".port")
case "host":
err = readTextValue(&c.Host, value, option + ".host")
case "keepalive":
err = readBooleanValue(&c.KeepAlive, value, option + ".keepalive")
default:
err = fmt.Errorf("unrecognized configuration option %q", option+"."+key)
}
if err != nil {
return err
}
}
return nil
}
func readConnectSection(c *config, val interface{}) error {
data, ok := val.(string)
if !ok {
return fmt.Errorf(`section "connect" must be a connection string`)
}
c.ClientConnInfo = data
return nil
}
func readStartupParameterSection(c *config, val interface{}) error {
data, ok := val.(map[string]interface{})
if !ok {
return fmt.Errorf(`section "startup_parameters" must be a set of key-value pairs`)
}
c.StartupParameters = make(map[string]string)
for k, v := range data {
vs, ok := v.(string)
if !ok {
return fmt.Errorf(`all startup parameters must be strings`)
}
c.StartupParameters[k] = vs
}
return nil
}
func readAuthSection(c *AuthConfig, val interface{}, option string) error {
data, ok := val.(map[string]interface{})
if !ok {
return fmt.Errorf(`section %q must be a JSON object`, option)
}
for key, value := range data {
var err error
switch key {
case "method":
err = readTextValue(&c.method, value, option+".method")
case "user":
err = readTextValue(&c.user, value, option+".user")
case "password":
err = readTextValue(&c.password, value, option+".password")
default:
err = fmt.Errorf("unrecognized configuration option %q", option+"."+key)
}
if err != nil {
return err
}
}
switch c.method {
case "md5":
case "trust":
default:
return fmt.Errorf("unrecognized authentication method %q in %q", c.method, option)
}
return nil
}
func readDatabaseSection(c *config, val interface{}) error {
array, ok := val.([]interface{})
if !ok {
return fmt.Errorf(`section "databases" must be a JSON array`)
}
for dbindex, el := range array {
data, ok := el.(map[string]interface{})
if !ok {
return fmt.Errorf(`elements within the "databases" array must be JSON objects`)
}
option := fmt.Sprintf("databases[%d]", dbindex)
var db virtualDatabase
for key, value := range data {
var err error
switch key {
case "name":
err = readTextValue(&db.name, value, option+".name")
case "auth":
err = readAuthSection(&db.auth, value, option+".auth")
default:
err = fmt.Errorf("unrecognized configuration option %q", option+"."+key)
}
if err != nil {
return err
}
}
for _, pedb := range c.Databases {
if pedb.name == db.name {
return fmt.Errorf("database name %q is not unique", db.name)
}
}
c.Databases = append(c.Databases, db)
}
return nil
}
func readPrometheusSection(c *config, val interface{}) error {
data, ok := val.(map[string]interface{})
if !ok {
return fmt.Errorf(`section "prometheus" must be a JSON object`)
}
for key, value := range data {
var err error
switch key {
case "listen":
err = readListenSection(&c.Prometheus.Listen, value, "prometheus.listen")
default:
err = fmt.Errorf("unrecognized configuration option %q", "prometheus." + key)
}
if err != nil {
return err
}
}
c.Prometheus.Enabled = true
return nil
}
func readConfigFile(filename string) error {
var ci interface{}
fh, err := os.Open(filename)
if err != nil {
return err
}
defer fh.Close()
dec := json.NewDecoder(fh)
err = dec.Decode(&ci)
if err != nil {
return err
}
sections, ok := ci.(map[string]interface{})
if !ok {
return fmt.Errorf("configuration must be a JSON object")
}
for key, value := range sections {
var err error
switch key {
case "listen":
err = readListenSection(&Config.Listen, value, "listen")
case "connect":
err = readConnectSection(&Config, value)
case "startup_parameters":
err = readStartupParameterSection(&Config, value)
case "databases":
err = readDatabaseSection(&Config, value)
case "prometheus":
err = readPrometheusSection(&Config, value)
default:
err = fmt.Errorf("unrecognized configuration section %q", key)
}
if err != nil {
return err
}
}
return nil
}