forked from hashicorp/consul-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_functions.go
468 lines (382 loc) · 10.5 KB
/
template_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
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"time"
dep "github.com/hashicorp/consul-template/dependency"
)
// now is function that represents the current time in UTC. This is here
// primarily for the tests to override times.
var now = func() time.Time { return time.Now().UTC() }
// datacentersFunc returns or accumulates datacenter dependencies.
func datacentersFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]string, error) {
return func(s ...string) ([]string, error) {
result := make([]string, 0)
d, err := dep.ParseDatacenters(s...)
if err != nil {
return result, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]string), nil
}
addDependency(missing, d)
return result, nil
}
}
// fileFunc returns or accumulates file dependencies.
func fileFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) (string, error) {
return func(s string) (string, error) {
if len(s) == 0 {
return "", nil
}
d, err := dep.ParseFile(s)
if err != nil {
return "", err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
if value == nil {
return "", nil
} else {
return value.(string), nil
}
}
addDependency(missing, d)
return "", nil
}
}
// keyFunc returns or accumulates key dependencies.
func keyFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) (string, error) {
return func(s string) (string, error) {
if len(s) == 0 {
return "", nil
}
d, err := dep.ParseStoreKey(s)
if err != nil {
return "", err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
if value == nil {
return "", nil
} else {
return value.(string), nil
}
}
addDependency(missing, d)
return "", nil
}
}
// lsFunc returns or accumulates keyPrefix dependencies.
func lsFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) ([]*dep.KeyPair, error) {
return func(s string) ([]*dep.KeyPair, error) {
result := make([]*dep.KeyPair, 0)
if len(s) == 0 {
return result, nil
}
d, err := dep.ParseStoreKeyPrefix(s)
if err != nil {
return result, err
}
addDependency(used, d)
// Only return non-empty top-level keys
if value, ok := brain.Recall(d); ok {
for _, pair := range value.([]*dep.KeyPair) {
if pair.Key != "" && !strings.Contains(pair.Key, "/") {
result = append(result, pair)
}
}
return result, nil
}
addDependency(missing, d)
return result, nil
}
}
// nodesFunc returns or accumulates catalog node dependencies.
func nodesFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]*dep.Node, error) {
return func(s ...string) ([]*dep.Node, error) {
result := make([]*dep.Node, 0)
d, err := dep.ParseCatalogNodes(s...)
if err != nil {
return nil, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]*dep.Node), nil
}
addDependency(missing, d)
return result, nil
}
}
// serviceFunc returns or accumulates health service dependencies.
func serviceFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]*dep.HealthService, error) {
return func(s ...string) ([]*dep.HealthService, error) {
result := make([]*dep.HealthService, 0)
if len(s) == 0 || s[0] == "" {
return result, nil
}
d, err := dep.ParseHealthServices(s...)
if err != nil {
return nil, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]*dep.HealthService), nil
}
addDependency(missing, d)
return result, nil
}
}
// servicesFunc returns or accumulates catalog services dependencies.
func servicesFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(...string) ([]*dep.CatalogService, error) {
return func(s ...string) ([]*dep.CatalogService, error) {
result := make([]*dep.CatalogService, 0)
d, err := dep.ParseCatalogServices(s...)
if err != nil {
return nil, err
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
return value.([]*dep.CatalogService), nil
}
addDependency(missing, d)
return result, nil
}
}
// treeFunc returns or accumulates keyPrefix dependencies.
func treeFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) ([]*dep.KeyPair, error) {
return func(s string) ([]*dep.KeyPair, error) {
result := make([]*dep.KeyPair, 0)
if len(s) == 0 {
return result, nil
}
d, err := dep.ParseStoreKeyPrefix(s)
if err != nil {
return result, err
}
addDependency(used, d)
// Only return non-empty top-level keys
if value, ok := brain.Recall(d); ok {
for _, pair := range value.([]*dep.KeyPair) {
parts := strings.Split(pair.Key, "/")
if parts[len(parts)-1] != "" {
result = append(result, pair)
}
}
return result, nil
}
addDependency(missing, d)
return result, nil
}
}
// vaultFunc returns or accumulates secret dependencies.
func vaultFunc(brain *Brain,
used, missing map[string]dep.Dependency) func(string) (*dep.Secret, error) {
return func(s string) (*dep.Secret, error) {
result := &dep.Secret{}
if len(s) == 0 {
return result, nil
}
d, err := dep.ParseVaultSecret(s)
if err != nil {
return result, nil
}
addDependency(used, d)
if value, ok := brain.Recall(d); ok {
result = value.(*dep.Secret)
return result, nil
}
addDependency(missing, d)
return result, nil
}
}
// byKey accepts a slice of KV pairs and returns a map of the top-level
// key to all its subkeys. For example:
//
// elasticsearch/a //=> "1"
// elasticsearch/b //=> "2"
// redis/a/b //=> "3"
//
// Passing the result from Consul through byTag would yield:
//
// map[string]map[string]string{
// "elasticsearch": &dep.KeyPair{"a": "1"}, &dep.KeyPair{"b": "2"},
// "redis": &dep.KeyPair{"a/b": "3"}
// }
//
// Note that the top-most key is stripped from the Key value. Keys that have no
// prefix after stripping are removed from the list.
func byKey(pairs []*dep.KeyPair) (map[string]map[string]*dep.KeyPair, error) {
m := make(map[string]map[string]*dep.KeyPair)
for _, pair := range pairs {
parts := strings.Split(pair.Key, "/")
top := parts[0]
key := strings.Join(parts[1:], "/")
if key == "" {
// Do not add a key if it has no prefix after stripping.
continue
}
if _, ok := m[top]; !ok {
m[top] = make(map[string]*dep.KeyPair)
}
newPair := *pair
newPair.Key = key
m[top][key] = &newPair
}
return m, nil
}
// byTag is a template func that takes the provided services and
// produces a map based on Service tags.
//
// The map key is a string representing the service tag. The map value is a
// slice of Services which have the tag assigned.
func byTag(in interface{}) (map[string][]interface{}, error) {
m := make(map[string][]interface{})
switch typed := in.(type) {
case nil:
case []*dep.CatalogService:
for _, s := range typed {
for _, t := range s.Tags {
m[t] = append(m[t], s)
}
}
case []*dep.HealthService:
for _, s := range typed {
for _, t := range s.Tags {
m[t] = append(m[t], s)
}
}
default:
return nil, fmt.Errorf("byTag: wrong argument type %T", in)
}
return m, nil
}
// env returns the value of the environment variable set
func env(s string) (string, error) {
return os.Getenv(s), nil
}
// loop accepts varying parameters and differs its behavior. If given one
// parameter, loop will return a goroutine that begins at 0 and loops until the
// given int, increasing the index by 1 each iteration. If given two parameters,
// loop will return a goroutine that begins at the first parameter and loops
// up to but not including the second parameter.
//
// // Prints 0 1 2 3 4
// for _, i := range loop(5) {
// print(i)
// }
//
// // Prints 5 6 7
// for _, i := range loop(5, 8) {
// print(i)
// }
//
func loop(ints ...int) (<-chan int, error) {
var start, stop int
switch len(ints) {
case 1:
start, stop = 0, ints[0]
case 2:
start, stop = ints[0], ints[1]
default:
return nil, fmt.Errorf("loop: wrong number of arguments, expected 1 or 2"+
", but got %d", len(ints))
}
ch := make(chan int)
go func() {
for i := start; i < stop; i++ {
ch <- i
}
close(ch)
}()
return ch, nil
}
// join is a version of strings.Join that can be piped
func join(sep string, a []string) (string, error) {
return strings.Join(a, sep), nil
}
// parseJSON returns a structure for valid JSON
func parseJSON(s string) (interface{}, error) {
if s == "" {
return make([]interface{}, 0), nil
}
var data interface{}
if err := json.Unmarshal([]byte(s), &data); err != nil {
return nil, err
}
return data, nil
}
// replaceAll replaces all occurrences of a value in a string with the given
// replacement value.
func replaceAll(f, t, s string) (string, error) {
return strings.Replace(s, f, t, -1), nil
}
// regexReplaceAll replaces all occurrences of a regular expression with
// the given replacement value.
func regexReplaceAll(re, pl, s string) (string, error) {
compiled, err := regexp.Compile(re)
if err != nil {
return "", err
}
return compiled.ReplaceAllString(s, pl), nil
}
// regexMatch returns true or false if the string matches
// the given regular expression
func regexMatch(re, s string) (bool, error) {
compiled, err := regexp.Compile(re)
if err != nil {
return false, err
}
return compiled.MatchString(s), nil
}
// split is a version of strings.Split that can be piped
func split(sep, s string) ([]string, error) {
s = strings.TrimSpace(s)
if s == "" {
return []string{}, nil
}
return strings.Split(s, sep), nil
}
// timestamp returns the current UNIX timestamp in UTC. If an argument is
// specified, it will be used to format the timestamp.
func timestamp(s ...string) (string, error) {
switch len(s) {
case 0:
return now().Format(time.RFC3339), nil
case 1:
return now().Format(s[0]), nil
default:
return "", fmt.Errorf("timestamp: wrong number of arguments, expected 0 or 1"+
", but got %d", len(s))
}
}
// toLower converts the given string (usually by a pipe) to lowercase.
func toLower(s string) (string, error) {
return strings.ToLower(s), nil
}
// toTitle converts the given string (usually by a pipe) to titlecase.
func toTitle(s string) (string, error) {
return strings.Title(s), nil
}
// toUpper converts the given string (usually by a pipe) to uppercase.
func toUpper(s string) (string, error) {
return strings.ToUpper(s), nil
}
// addDependency adds the given Dependency to the map.
func addDependency(m map[string]dep.Dependency, d dep.Dependency) {
if _, ok := m[d.HashCode()]; !ok {
m[d.HashCode()] = d
}
}