-
Notifications
You must be signed in to change notification settings - Fork 96
/
template.go
123 lines (94 loc) · 3.41 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
/*
* @license
* Copyright 2023 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 template
import (
"bytes"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/errutils"
"github.com/dynatrace/dynatrace-configuration-as-code/v2/internal/log"
"os"
"strings"
"text/template" // nosemgrep: go.lang.security.audit.xss.import-text-template.import-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
// In v2 config templating this is replaced by renderer.go/Render
func (t *templateImpl) ExecuteTemplate(data map[string]string) (string, error) {
tpl := bytes.Buffer{}
dataForTemplating := addEnvVars(data)
dataForTemplating, err := EscapeSpecialCharacters(dataForTemplating)
if errutils.CheckError(err, "Failed to prepare config properties for templating") {
return "", err
}
err = t.template.Execute(&tpl, dataForTemplating)
if errutils.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
}