forked from openziti-test-kitchen/cf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.go
49 lines (42 loc) · 1.26 KB
/
variables.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
package cf
import (
"github.com/pkg/errors"
"regexp"
"strings"
)
func init() {
variableRegex = regexp.MustCompile("^\\$\\{([a-zA-Z0-9_.]+)\\}$")
inlineVariableRegex = regexp.MustCompile("\\$\\{([a-zA-Z0-9_.]+)\\}")
}
var variableRegex *regexp.Regexp
var inlineVariableRegex *regexp.Regexp
func variableReference(v interface{}) (string, bool) {
if vt, ok := v.(string); ok {
if vmatch := variableRegex.FindSubmatch([]byte(strings.TrimSpace(vt))); vmatch != nil {
return string(vmatch[1]), true
}
}
return "", false
}
func inlineVariablesFound(in string) bool {
vmatch := inlineVariableRegex.FindSubmatch([]byte(strings.TrimSpace(in)))
return vmatch != nil
}
func replaceInlineVariables(in string, opt *Options) (string, error) {
out := strings.TrimSpace(in)
vmatch := inlineVariableRegex.FindSubmatchIndex([]byte(out))
for vmatch != nil {
vname := out[vmatch[2]:vmatch[3]]
vvalue, resolved := opt.resolveVariable(vname)
if !resolved {
return "", errors.Errorf("variable ${%s} not found", vname)
}
vvstring, ok := vvalue.(string)
if !ok {
return "", errors.Errorf("variable ${%s} not string value", vname)
}
out = out[:vmatch[0]]+vvstring+out[vmatch[1]:]
vmatch = inlineVariableRegex.FindSubmatchIndex([]byte(out))
}
return out, nil
}