This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
confkey.go
378 lines (300 loc) · 8.84 KB
/
confkey.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Package confkey looks for tags on a structure and set values
// based on the tag rather than the struct item names
//
// Features
//
// Defaults are supported and can be fetched from the shell environment
//
// The tags can specify some formating like comma splits and other
// commonly seen patterns in config files.
//
// Conversion of []string, ints, strings, time.Duration and booleans are support
//
// Validations can be done on a struct basis using the github.com/choria-io/go-validators
// package
//
// A sample structure might look like this, the package contains utilities to
// set values, apply defaults and perform validations
//
// type Config struct {
// Loglevel string `confkey:"loglevel" default:"warn" validate:"enum=debug,info,warn,error"`
// Mode string `confkey:"mode" default:"server" validate:"enum=server,client"`
// Servers []string `confkey:"servers" type:"comma_split" environment:"SERVERS"`
// Path []string `confkey:"path" type:"path_split" default:"/bin:/usr/bin"`
// I time.Duration `confkey:"interval" type:"duration" default:"1h"`
// }
//
// The utilities here will let you parse any config file that might have keys like loglevel etc
// and map the string values read from the text file onto the structure
package confkey
import (
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"unicode"
validator "github.com/choria-io/go-validator"
)
// Validate validates the struct
func Validate(target interface{}) error {
_, err := validator.ValidateStruct(target)
return err
}
// SetStructDefaults extract defaults out of the tags and set them to the key
func SetStructDefaults(target interface{}) error {
if reflect.TypeOf(target).Kind() != reflect.Ptr {
return errors.New("pointer is required")
}
st := reflect.TypeOf(target).Elem()
for i := 0; i <= st.NumField()-1; i++ {
field := st.Field(i)
if key, ok := field.Tag.Lookup("confkey"); ok {
if value, ok := field.Tag.Lookup("default"); ok {
err := SetStructFieldWithKey(target, key, value)
if err != nil {
return err
}
}
}
}
return nil
}
// StringFieldWithKey retrieves a string from target that matches key, "" when not found
func StringFieldWithKey(target interface{}, key string) string {
item, err := fieldWithKey(target, key)
if err != nil {
return ""
}
field := reflect.ValueOf(target).Elem().FieldByName(item)
if field.Kind() == reflect.String {
ptr := field.Addr().Interface().(*string)
return string(*ptr)
}
return ""
}
// StringListWithKey retrieves a []string from target that matches key, empty when not found
func StringListWithKey(target interface{}, key string) []string {
item, err := fieldWithKey(target, key)
if err != nil {
return []string{}
}
field := reflect.ValueOf(target).Elem().FieldByName(item)
if field.Kind() == reflect.Slice {
ptr := field.Addr().Interface().(*[]string)
if *ptr == nil {
return []string{}
}
return []string(*ptr)
}
return []string{}
}
// BoolWithKey retrieves a bool from target that matches key, false when not found
func BoolWithKey(target interface{}, key string) bool {
item, err := fieldWithKey(target, key)
if err != nil {
return false
}
field := reflect.ValueOf(target).Elem().FieldByName(item)
if field.Kind() == reflect.Bool {
ptr := field.Addr().Interface().(*bool)
return bool(*ptr)
}
return false
}
// IntWithKey retrieves an int from target that matches key, 0 when not found
func IntWithKey(target interface{}, key string) int {
item, err := fieldWithKey(target, key)
if err != nil {
return 0
}
field := reflect.ValueOf(target).Elem().FieldByName(item)
if field.Kind() == reflect.Int {
ptr := field.Addr().Interface().(*int)
return int(*ptr)
}
return 0
}
// Int64WithKey retrieves an int from target that matches key, 0 when not found
func Int64WithKey(target interface{}, key string) int64 {
item, err := fieldWithKey(target, key)
if err != nil {
return 0
}
field := reflect.ValueOf(target).Elem().FieldByName(item)
if field.Kind() == reflect.Int64 {
ptr := field.Addr().Interface().(*int64)
return int64(*ptr)
}
return 0
}
// SetStructFieldWithKey finds the struct key that matches the confkey on target and assign the value to it
func SetStructFieldWithKey(target interface{}, key string, value interface{}) error {
if reflect.TypeOf(target).Kind() != reflect.Ptr {
return errors.New("pointer is required")
}
item, err := fieldWithKey(target, key)
if err != nil {
return err
}
if tag, ok := tag(target, item, "environment"); ok {
if v, ok := os.LookupEnv(tag); ok {
value = v
}
}
field := reflect.ValueOf(target).Elem().FieldByName(item)
switch field.Kind() {
case reflect.Slice:
ptr := field.Addr().Interface().(*[]string)
if tag, ok := tag(target, item, "type"); ok {
switch tag {
case "comma_split":
// specifically clear it since these are one line split like 'collectives'
*ptr = []string{}
vals := strings.Split(value.(string), ",")
for _, v := range vals {
*ptr = append(*ptr, strings.TrimSpace(v))
}
case "colon_split":
// these are like libdir, but we want to always use : to split and not
// os path like path_split would do
vals := strings.Split(value.(string), ":")
for _, v := range vals {
*ptr = append(*ptr, strings.TrimSpace(v))
}
case "path_split":
// these are like libdir, either a one line split or a multiple occurance with splits
vals := strings.Split(value.(string), string(os.PathListSeparator))
for _, v := range vals {
*ptr = append(*ptr, strings.TrimSpace(v))
}
}
} else {
*ptr = append(*ptr, strings.TrimSpace(value.(string)))
}
case reflect.Int:
ptr := field.Addr().Interface().(*int)
i, err := strconv.Atoi(value.(string))
if err != nil {
return err
}
*ptr = i
case reflect.Int64:
if tag, ok := tag(target, item, "type"); ok {
if tag == "duration" {
ptr := field.Addr().Interface().(*time.Duration)
intonly, err := regexp.MatchString("\\A\\d+\\z", value.(string))
if err != nil {
return err
}
if intonly {
i, err := strconv.Atoi(value.(string))
if err != nil {
return err
}
*ptr = time.Second * time.Duration(i)
break
}
d, err := time.ParseDuration(value.(string))
if err != nil {
return err
}
*ptr = d
}
}
case reflect.String:
ptr := field.Addr().Interface().(*string)
*ptr = value.(string)
if tag, ok := tag(target, item, "type"); ok {
switch tag {
case "title_string":
a := []rune(value.(string))
a[0] = unicode.ToUpper(a[0])
*ptr = string(a)
case "path_string":
a := strings.TrimSpace(value.(string))
if a[0] == '~' {
home, err := homeDir()
if err != nil {
return err
}
a = strings.Replace(a, "~", home, 1)
}
*ptr = a
}
}
case reflect.Bool:
ptr := field.Addr().Interface().(*bool)
b, _ := strToBool(value.(string))
*ptr = b
}
_, err = validator.ValidateStructField(target, item)
return err
}
func homeDir() (string, error) {
if runtime.GOOS == "windows" {
drive := os.Getenv("HOMEDRIVE")
home := os.Getenv("HOMEDIR")
if home == "" || drive == "" {
return "", fmt.Errorf("Cannot determine home dir, ensure HOMEDRIVE and HOMEDIR is set")
}
return filepath.Join(os.Getenv("HOMEDRIVE"), os.Getenv("HOMEDIR")), nil
}
home := os.Getenv("HOME")
if home == "" {
return "", fmt.Errorf("Cannot determine home dir, ensure HOME is set")
}
return home, nil
}
// determines the struct key name that is tagged with a certain confkey
func fieldWithKey(s interface{}, key string) (string, error) {
st := reflect.TypeOf(s)
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
for i := 0; i <= st.NumField()-1; i++ {
field := st.Field(i)
if confkey, ok := field.Tag.Lookup("confkey"); ok {
if confkey == key {
return field.Name, nil
}
}
}
return "", fmt.Errorf("can't find any structure element configured with confkey '%s'", key)
}
// retrieve a tag for a struct field
func tag(s interface{}, field string, tag string) (string, bool) {
st := reflect.TypeOf(s)
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
for i := 0; i <= st.NumField()-1; i++ {
f := st.Field(i)
if f.Name == field {
if value, ok := f.Tag.Lookup(tag); ok {
return value, true
}
}
}
return "", false
}
// StrToBool converts a typical boolianish string to bool.
//
// 1, yes, true, y, t will be true
// 0, no, false, n, f will be false
// anything else will be false with an error
func strToBool(s string) (bool, error) {
clean := strings.TrimSpace(s)
if regexp.MustCompile(`(?i)^(1|yes|true|y|t)$`).MatchString(clean) {
return true, nil
}
if regexp.MustCompile(`(?i)^(0|no|false|n|f)$`).MatchString(clean) {
return false, nil
}
return false, errors.New("cannot convert string value '" + clean + "' into a boolean.")
}