fix(template): support surrounding variables with braces

This commit is contained in:
Jan De Dobbeleer 2022-01-18 09:55:27 +01:00 committed by Jan De Dobbeleer
parent e6d4413ae7
commit 9e77d0f939
2 changed files with 6 additions and 1 deletions

View file

@ -105,7 +105,7 @@ func (t *textTemplate) cleanTemplate() {
return splitted[0], true
}
knownVariables := []string{"Root", "PWD", "Folder", "Shell", "UserName", "HostName", "Env", "Data", "Code", "OS"}
matches := findAllNamedRegexMatch(`(?: |{)(?P<var>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
matches := findAllNamedRegexMatch(`(?: |{|\()(?P<var>(\.[a-zA-Z_][a-zA-Z0-9]*)+)`, t.Template)
for _, match := range matches {
if variable, OK := unknownVariable(match["var"], &knownVariables); OK {
pattern := fmt.Sprintf(`\.%s\b`, variable)

View file

@ -175,6 +175,11 @@ func TestCleanTemplate(t *testing.T) {
Expected: "{{ .Env.HELLO }} {{ .Data.World }} {{ .Data.World }}",
Template: "{{ .Env.HELLO }} {{ .World }} {{ .World }}",
},
{
Case: "Braces",
Expected: "{{ if or (.Data.Working.Changed) (.Data.Staging.Changed) }}#FF9248{{ end }}",
Template: "{{ if or (.Working.Changed) (.Staging.Changed) }}#FF9248{{ end }}",
},
}
for _, tc := range cases {
template := &textTemplate{