forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
33 lines (24 loc) · 785 Bytes
/
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
package template
import (
"encoding/json"
"fmt"
"regexp"
"github.com/hashicorp/go-multierror"
)
var templateFormatRegex = regexp.MustCompile(`\{\{([-\w\p{L}]+)\}\}`)
func Evaluate(content []byte, variables Variables) ([]byte, error) {
var variableErrors error
return templateFormatRegex.ReplaceAllFunc(content, func(match []byte) []byte {
key := string(templateFormatRegex.FindSubmatch(match)[1])
value, found := variables[key]
if !found {
variableErrors = multierror.Append(variableErrors, fmt.Errorf("unbound variable in template: '%s'", key))
return match
}
saveValue, _ := json.Marshal(value)
return []byte(saveValue)
}), variableErrors
}
func EvaluateEmpty(content []byte) []byte {
return templateFormatRegex.ReplaceAll(content, []byte(`""`))
}