/
template.go
167 lines (127 loc) · 4.63 KB
/
template.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
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"bytes"
"os"
"reflect"
"regexp"
"strings"
"text/template"
"github.com/spf13/afero"
)
// Template wraps the underlying templating logic and provides a means of setting config values just on one place.
// It is intended to be language-agnostic, the file type does not matter (yaml, json, ...)
type Template interface {
ExecuteTemplate(data map[string]string) (string, error)
}
type templateImpl struct {
template *template.Template
}
// NewTemplateFromString creates a new template for the given string content
func NewTemplateFromString(name string, content string) (Template, error) {
templ := template.New(name).Option("missingkey=error")
templ, err := templ.Parse(content)
if err != nil {
return nil, err
}
return newTemplate(templ), nil
}
// NewTemplate creates a new template for the given file
func NewTemplate(fs afero.Fs, fileName string) (Template, error) {
data, err := afero.ReadFile(fs, fileName)
if err != nil {
return nil, err
}
return NewTemplateFromString(fileName, string(data))
}
func newTemplate(templ *template.Template) Template {
// Fail fast on missing variable (key):
templ = templ.Option("missingkey=error")
return &templateImpl{
template: templ,
}
}
// ExecuteTemplate executes the given template. It fills the placeholder variables in the template with the strings
// in the data map. Additionally, it resolves all environment variables present in the template.
// Important: if a variable present in the template has no corresponding entry in the data map, this method will throw
// an error
func (t *templateImpl) ExecuteTemplate(data map[string]string) (string, error) {
tpl := bytes.Buffer{}
dataForTemplating := addEnvVars(data)
dataForTemplating = escapeSpecialCharacters(dataForTemplating)
err := t.template.Execute(&tpl, dataForTemplating)
if CheckError(err, "Could not execute template") {
return "", err
}
return tpl.String(), nil
}
func addEnvVars(properties map[string]string) map[string]interface{} {
data := make(map[string]interface{})
for k := range properties {
data[k] = properties[k]
}
envVars := make(map[string]string)
data["Env"] = envVars
for _, v := range os.Environ() {
split := strings.SplitN(v, "=", 2)
if len(split) != 2 {
continue
}
if _, ok := properties[split[0]]; ok {
Log.Info("Environment variable %s also defined as property. Was that your intention?", split[0])
}
envVars[split[0]] = split[1]
}
return data
}
// escapeSpecialCharacters walks recursively though the map and escapes all special characters that can't just be written to the
// json template. characters that will be escaped: newlines (\n), double quotes (\")
func escapeSpecialCharacters(properties map[string]interface{}) map[string]interface{} {
escapedProperties := make(map[string]interface{}, len(properties))
for key, value := range properties {
switch field := value.(type) {
case string:
escapedProperties[key] = escapeNewlines(field)
case map[string]string:
escapedProperties[key] = escapeSpecialCharactersForStringMap(field)
case map[string]interface{}:
escapedProperties[key] = escapeSpecialCharacters(field)
default:
Log.Debug("Unknown value type %v in property %v.", reflect.TypeOf(value), key)
}
}
return escapedProperties
}
func escapeSpecialCharactersForStringMap(properties map[string]string) map[string]string {
escapedProperties := make(map[string]string, len(properties))
for key, value := range properties {
escapedProperties[key] = escapeNewlines(value)
}
return escapedProperties
}
func escapeNewlines(rawString string) string {
if isListDefinition(rawString) {
return rawString
}
return strings.ReplaceAll(rawString, "\n", `\n`)
}
// pattern matching strings of the format '"value", "value", ...' which are sometimes used to set lists into JSON templates
// these must generally not have their quotes escaped as their JSON template is usually not valid with these values
var listPattern = regexp.MustCompile(`(?:\s*".*?"\s*,\s*".*?"\s*,?)+`)
func isListDefinition(s string) bool {
return listPattern.MatchString(s)
}