-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
642 lines (590 loc) · 15.7 KB
/
functions.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
package template
import (
"bytes"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig"
"github.com/magiconair/properties"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v3"
"net"
"os"
"reflect"
"regexp"
"strings"
"text/template"
)
func funcMap() template.FuncMap {
f := sprig.TxtFuncMap()
// Add some extra functionality
extra := template.FuncMap{
"heygodub": func() string { return "Hello :)" },
// Env functions
"hasEnv": hasEnv,
"fromEnv": fromEnv,
"envToMap": envToMap,
"envToProp": envToProp,
// Map functions
"excludeKeys": excludeKeys,
"replaceKeyPrefix": replaceKeyPrefix,
"toPropertiesKey": toPropertiesKey,
// String functions
"kvCsvToMap": kvCsvToMap,
// List functions
"filterHasPrefix": filterHasPrefix,
// Verify functions
"required": required,
// Network functions
"ipAddresses": ipAddresses,
"ipAddress": ipAddress,
"anyIpAddress": anyIpAddress,
// Format functions
"toYAML": toYAML,
"fromYAML": fromYAML,
"toJSON": toJSON,
"toJSONPretty": toJSONPretty,
"fromJSON": fromJSON,
"toTOML": toTOML,
"fromTOML": fromTOML,
"toProperties": toProperties,
"fromProperties": fromProperties,
}
for k, v := range extra {
f[k] = v
}
return f
}
// checks if an environment variable with the given key exists
func hasEnv(key string) bool {
_, has := os.LookupEnv(key)
return has
}
func fromEnv() map[string]interface{} {
env := make(map[string]interface{})
for _, setting := range os.Environ() {
pair := strings.SplitN(setting, "=", 2)
env[pair[0]] = pair[1]
}
return env
}
// custom function that returns key/value for all environment variable keys matching prefix
func envToMap(prefix string) map[string]interface{} {
env := make(map[string]interface{})
for _, setting := range os.Environ() {
pair := strings.SplitN(setting, "=", 2)
if strings.HasPrefix(pair[0], prefix) {
env[pair[0]] = pair[1]
}
}
return env
}
// Converts environment variables with a prefix into key/value properties
// in order to support wildcard handling of properties. Naming convention
// is to convert env vars to lower case and replace '_' with '.'.
// Additionally, two underscores '__' are replaced with a single underscore '_'
// and three underscores '___' are replaced with a dash '-'.
//
// For example: if these are set in the environment
//
// CONTROL_CENTER_STREAMS_NUM_STREAM_THREADS=4
// CONTROL_CENTER_STREAMS_SECURITY_PROTOCOL=SASL_SSL
// CONTROL_CENTER_STREAMS_WITH__UNDERSCORE=foo
// CONTROL_CENTER_STREAMS_WITH___DASH=bar
// CONTROL_CENTER_STREAMS_SASL_KERBEROS_SERVICE_NAME=kafka
//
// then
//
// env_to_props('CONTROL_CENTER_STREAMS_', 'confluent.controlcenter.streams.', exclude=['CONTROL_CENTER_STREAMS_NUM_STREAM_THREADS'])
//
// will produce
//
// {
// 'confluent.controlcenter.streams.security.protocol': 'SASL_SSL',
// 'confluent.controlcenter.streams.with_underscore': 'foo',
// 'confluent.controlcenter.streams.with_dash': 'bar',
// 'confluent.controlcenter.streams.sasl.kerberos.service.name': 'kafka'
// }
//
// Args:
//
// env_prefix: prefix of environment variables to include. (e.g. CONTROL_CENTER_STREAMS_)
// prop_prefix: prefix of the resulting properties (e.g. confluent.controlcenter.streams.)
// exclude: list of environment variables to exclude
//
// Returns:
//
// Map of matching properties.
//
// See:
//
// Original dub: https://github.com/confluentinc/confluent-docker-utils/blob/master/confluent/docker_utils/dub.py
func envToProp(env_prefix string, prop_prefix string, exclude ...interface{}) map[string]interface{} {
return toPropertiesKey(replaceKeyPrefix(env_prefix, prop_prefix, excludeKeys(exclude, envToMap(env_prefix))))
}
func excludeKeys(exclude interface{}, sourceMap interface{}) map[string]interface{} {
sourceMapVal := reflect.ValueOf(sourceMap)
switch sourceMapVal.Kind() {
case reflect.Map:
resultMap := make(map[string]interface{})
excludeStrings := toFlatListOfStrings(exclude)
iter := sourceMapVal.MapRange()
for iter.Next() {
key := strval(iter.Key().Interface())
value := iter.Value().Interface()
if !contains(excludeStrings, key) {
resultMap[key] = value
}
}
return resultMap
default:
panic(fmt.Errorf("must be a map but was %T", sourceMap))
}
}
func strval(v interface{}) string {
switch t := v.(type) {
case nil:
return ""
case string:
return t
case []byte:
return string(t)
case error:
return t.Error()
case fmt.Stringer:
return t.String()
default:
return fmt.Sprint(t)
}
}
func replaceKeyPrefix(prefix string, replacement string, sourceMap interface{}) map[string]interface{} {
sourceMapVal := reflect.ValueOf(sourceMap)
switch sourceMapVal.Kind() {
case reflect.Map:
resultMap := make(map[string]interface{})
iter := sourceMapVal.MapRange()
for iter.Next() {
key := strval(iter.Key().Interface())
value := iter.Value().Interface()
resultMap[replacement+strings.TrimPrefix(key, prefix)] = value
}
return resultMap
default:
panic(fmt.Errorf("must be a map but was %T", sourceMap))
}
}
func toPropertiesKey(sourceMap interface{}) map[string]interface{} {
sourceMapVal := reflect.ValueOf(sourceMap)
switch sourceMapVal.Kind() {
case reflect.Map:
props := make(map[string]interface{})
to_dot_pattern := regexp.MustCompile("[^_](_)[^_]")
iter := sourceMapVal.MapRange()
for iter.Next() {
key := strval(iter.Key().Interface())
value := iter.Value().Interface()
raw_name := strings.ToLower(key)
var prop_dot string = raw_name
for matches := to_dot_pattern.FindAllString(prop_dot, -1); len(matches) > 0; matches = to_dot_pattern.FindAllString(prop_dot, -1) {
for _, frac := range matches {
prop_dot = strings.Replace(prop_dot, frac, strings.ReplaceAll(frac, "_", "."), 1)
}
}
prop_dash := strings.Join(strings.Split(prop_dot, "___"), "-")
prop_underscore := strings.Join(strings.Split(prop_dash, "__"), "_")
props[prop_underscore] = value
}
return props
default:
panic(fmt.Errorf("must be a map but was %T", sourceMap))
}
}
// Parses a list of key/value pairs separated by commas.
//
// For example for "foo.bar=DEBUG,baz.bam=TRACE"
//
// the this function will return {"foo.bar: "DEBUG", "baz.bam": "TRACE"}
//
// Args:
//
// kvList: String containing the comma separated list of key/value pairs.
//
// Returns:
//
// Map of key/value pairs.
//
// See:
//
// Original dub: https://github.com/confluentinc/confluent-docker-utils/blob/master/confluent/docker_utils/dub.py
func kvCsvToMap(kvList string) map[string]interface{} {
props := make(map[string]interface{})
for _, override := range strings.Split(kvList, ",") {
tokens := strings.SplitN(override, "=", 2)
if len(tokens) == 2 {
props[tokens[0]] = tokens[1]
}
}
return props
}
func filterHasPrefix(prefix string, list interface{}) (interface{}, error) {
tp := reflect.TypeOf(list).Kind()
switch tp {
case reflect.Slice, reflect.Array:
filteredList := make([]interface{}, 0)
l2 := reflect.ValueOf(list)
if l2.Len() == 0 {
return filteredList, nil
}
for i := 0; i < l2.Len(); i++ {
result, err := filterHasPrefix(prefix, l2.Index(i).Interface())
if err != nil {
return nil, err
}
if result != nil {
filteredList = append(filteredList, result)
}
}
return filteredList, nil
default:
switch t := list.(type) {
case string:
if strings.HasPrefix(t, prefix) {
return t, nil
}
return nil, nil
case fmt.Stringer:
if strings.HasPrefix(t.String(), prefix) {
return t, nil
}
return nil, nil
case []byte:
if strings.HasPrefix(string(t), prefix) {
return t, nil
}
return nil, nil
case error:
if strings.HasPrefix(t.Error(), prefix) {
return t, nil
}
return nil, nil
case nil:
return nil, nil
default:
return nil, fmt.Errorf("should be type of slice, array, string, fmt.Stringer, []byte or error, but %s", tp)
}
}
}
// contains checks if a string is present in a slice
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func toFlatListOfStrings(args ...interface{}) []string {
stringList := make([]string, 0)
for _, arg := range args {
switch reflect.TypeOf(arg).Kind() {
case reflect.Slice, reflect.Array:
value := reflect.ValueOf(arg)
for i := 0; i < value.Len(); i++ {
stringList = append(stringList, toFlatListOfStrings(value.Index(i).Interface())...)
}
default:
switch t := arg.(type) {
case nil:
break
case string:
stringList = append(stringList, t)
case []byte:
stringList = append(stringList, string(t))
case error:
stringList = append(stringList, t.Error())
case fmt.Stringer:
stringList = append(stringList, t.String())
default:
stringList = append(stringList, fmt.Sprintf("%v", t))
}
}
}
return stringList
}
func required(warn string, val interface{}) (interface{}, error) {
if val == nil || reflect.ValueOf(val).IsNil() {
return nil, fmt.Errorf(warn)
} else if _, ok := val.(string); ok {
if val == "" || val == "<nil>" {
return nil, fmt.Errorf(warn)
}
}
return val, nil
}
const (
require string = "require"
prefer string = "prefer"
)
const (
ipv4 string = "ipv4"
ipv6 string = "ipv6"
)
func anyIpAddress() (*net.IP, error) {
return ipAddress(prefer, ipv4, 0)
}
func ipAddress(preference string, ipVersion string, iface int) (*net.IP, error) {
addresses, err := ipAddresses(preference, ipVersion)
if err != nil {
return nil, err
}
if len(addresses) <= iface {
return nil, fmt.Errorf("less than %d interfaces are available with a global unicast address matching '%s %s'", iface+1, preference, ipVersion)
}
return addresses[iface], nil
}
func ipAddresses(preference string, ipVersion string) ([]*net.IP, error) {
if preference != require && preference != prefer {
return nil, fmt.Errorf("preference argument must be one of [%s, %s], but was: %s", require, prefer, preference)
}
if ipVersion != ipv4 && ipVersion != ipv6 {
return nil, fmt.Errorf("IP version argument must be one of [%s, %s], but was: %s", ipv4, ipv6, ipVersion)
}
addresses := make([]*net.IP, 0)
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
var foundIfaceAddr *net.IP
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.IsGlobalUnicast() {
if foundIfaceAddr == nil || ipAddrVersion(*foundIfaceAddr) != ipVersion {
foundIfaceAddr = &ip
}
if ipAddrVersion(*foundIfaceAddr) == ipVersion {
break
}
}
}
if foundIfaceAddr != nil && (preference == prefer || ipAddrVersion(*foundIfaceAddr) == ipVersion) {
addresses = append(addresses, foundIfaceAddr)
}
}
return addresses, nil
}
func ipAddrVersion(ip net.IP) string {
if ip.To4() != nil {
return ipv4
}
return ipv6
}
// toYAML takes an interface, marshals it to yaml, and returns a string.
func toYAML(v interface{}) (string, error) {
data, err := yaml.Marshal(v)
if err != nil {
return "", err
}
return strings.TrimSuffix(string(data), "\n"), nil
}
func fromYAML(text string) (interface{}, error) {
var out interface{}
err := yaml.Unmarshal([]byte(text), &out)
if err != nil {
return nil, err
}
return out, nil
}
// toJSON takes an interface, marshals it to json, and returns a string.
func toJSON(v interface{}) (string, error) {
data, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(data), nil
}
func toJSONPretty(indent string, v interface{}) (string, error) {
data, err := json.MarshalIndent(v, "", indent)
if err != nil {
return "", err
}
return string(data), nil
}
func fromJSON(text string) (interface{}, error) {
var out interface{}
err := json.Unmarshal([]byte(text), &out)
if err != nil {
return nil, err
}
return out, nil
}
// toTOML takes an interface, marshals it to toml, and returns a string.
func toTOML(v interface{}) (string, error) {
b := bytes.NewBuffer(nil)
e := toml.NewEncoder(b)
err := e.Encode(v)
if err != nil {
return "", err
}
return b.String(), nil
}
func fromTOML(text string) (interface{}, error) {
var out interface{}
_, err := toml.Decode(text, &out)
if err != nil {
return nil, err
}
return out, nil
}
// toProperties takes an interface, marshals it to properties, and returns a string.
func toProperties(v interface{}) (string, error) {
ofStrings, err := toPropertiesStringMap(v)
if err != nil {
return "", err
}
props := properties.LoadMap(ofStrings)
return props.String(), nil
}
func fromProperties(text string) (map[string]interface{}, error) {
propsMap := make(map[string]interface{})
props, err := properties.LoadString(text)
for key, value := range props.Map() {
propsMap[key] = value
}
if err != nil {
return nil, err
}
return propsMap, nil
}
func toPropertiesStringMap(v interface{}) (map[string]string, error) {
switch t := v.(type) {
case nil:
return map[string]string{}, nil
case map[string]string:
return t, nil
default:
val := reflect.ValueOf(v)
switch val.Kind() {
case reflect.Map:
stringMap := make(map[string]string)
iter := val.MapRange()
for iter.Next() {
key := iter.Key().Interface()
value := iter.Value().Interface()
stringKey, err := toPropertiesString(key)
if err != nil {
return nil, err
}
stringValue, err := toPropertiesString(value)
if err != nil {
return nil, err
}
stringMap[stringKey] = stringValue
}
return stringMap, nil
case reflect.Array, reflect.Slice:
stringMap := make(map[string]string)
l := val.Len()
for i := 0; i < l; i++ {
value := val.Index(i).Interface()
subMap, err := toPropertiesStringMap(value)
if err != nil {
return nil, err
}
for subKey, subValue := range subMap {
stringMap[subKey] = subValue
}
}
return stringMap, nil
}
if isStruct(t) {
m, err := structToMap(t)
if err != nil {
return nil, err
}
return toPropertiesStringMap(m)
}
stringKey, err := toPropertiesString(t)
if err != nil {
return nil, err
}
return map[string]string{stringKey: ""}, nil
}
}
func toPropertiesString(v interface{}) (string, error) {
switch t := v.(type) {
case nil:
return "", nil
case string:
return t, nil
case []byte:
return string(t), nil
case error:
return t.Error(), nil
case fmt.Stringer:
return t.String(), nil
default:
val := reflect.ValueOf(v)
switch val.Kind() {
case reflect.Map:
stringList := make([]string, 0)
iter := val.MapRange()
for iter.Next() {
key := iter.Key().Interface()
value := iter.Value().Interface()
stringKey, err := toPropertiesString(key)
if err != nil {
return "", err
}
stringValue, err := toPropertiesString(value)
if err != nil {
return "", err
}
stringList = append(stringList, fmt.Sprintf("%s=%s", stringKey, stringValue))
}
return strings.Join(stringList, ","), nil
case reflect.Array, reflect.Slice:
l := val.Len()
stringList := make([]string, 0)
for i := 0; i < l; i++ {
value := val.Index(i).Interface()
subString, err := toPropertiesString(value)
if err != nil {
return "", err
}
stringList = append(stringList, subString)
}
return strings.Join(stringList, ","), nil
}
if isStruct(t) {
m, err := structToMap(t)
if err != nil {
return "", err
}
return toPropertiesString(m)
}
return fmt.Sprint(t), nil
}
}
func isStruct(v interface{}) bool {
return reflect.ValueOf(v).Kind() == reflect.Ptr &&
reflect.ValueOf(v).Elem().Kind() == reflect.Struct
}
func structToMap(v interface{}) (map[string]interface{}, error) {
if !isStruct(v) {
return nil, fmt.Errorf("%T is not a ptr to a struct", v)
}
out := make(map[string]interface{})
err := mapstructure.Decode(v, &out)
return out, err
}