-
Notifications
You must be signed in to change notification settings - Fork 9
/
templates.go
85 lines (67 loc) · 1.86 KB
/
templates.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
package main
import (
"bytes"
"errors"
. "github.com/ahmetalpbalkan/go-linq"
"github.com/bsideup/configo/parsers"
"github.com/op/go-logging"
"os"
"strings"
"text/template"
)
const configoPrefix = "CONFIGO:"
var customFuncs = func() template.FuncMap {
result := template.FuncMap{
"encrypt": encrypt,
"decrypt": decrypt,
}
for _, el := range []string{"JSON", "YAML", "HCL", "TOML", "Properties"} {
format := el
result["from"+format] = func(source string) (map[string]interface{}, error) {
parser := parsers.GetParser(format)
if parser == nil {
return nil, errors.New("Unknown format " + format)
}
result := make(map[string]interface{})
return result, parser.Parse([]byte(source), result)
}
}
return result
}()
func processTemplatedEnvs(environ []string) error {
envMap := make(map[string]string)
// Calculate fresh map of environment variables
fromEnviron(os.Environ()).All(func(kv T) (bool, error) {
envMap[kv.(env).key] = kv.(env).value
return true, nil
})
count, err := fromEnviron(environ).
Where(func(kv T) (bool, error) { return strings.HasPrefix(kv.(env).value, configoPrefix), nil }).
CountBy(func(kv T) (bool, error) {
tmpl, err := template.New(kv.(env).key).Funcs(customFuncs).Parse(strings.TrimPrefix(kv.(env).value, configoPrefix))
if err != nil {
return false, err
}
var buffer bytes.Buffer
if err = tmpl.Execute(&buffer, envMap); err != nil {
return false, err
}
key := kv.(env).key
value := buffer.String()
log.Infof("Setting templated variable `%s` to `%#v`", key, value)
err = os.Setenv(key, value)
if err != nil {
return false, err
}
return true, nil
})
if err != nil {
return err
}
if count > 0 {
if log.IsEnabledFor(logging.DEBUG) {
log.Debugf("Environment variables after templates:\n\t%s", strings.Join(os.Environ(), "\n\t"))
}
}
return nil
}