Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the autoindent regex stricter #18

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (t *TemplateResolver) processForAutoIndent(str string) string {
// This is not a very strict regex as occasionally, a user will make a mistake such as
// `config: '{{ "hello\nworld" | autoindent }}'`. In that event, `autoindent` will change to
// `indent 1`, but `indent` properly handles this.
re := regexp.MustCompile(`( *)(?:.*` + d1 + `.*\| *autoindent *` + d2 + `)`)
re := regexp.MustCompile(`( *)(?:'|")?(` + d1 + `.*\| *autoindent *` + d2 + `)`)
glog.V(glogDefLvl).Infof("\n Pattern: %v\n", re.String())

submatches := re.FindAllStringSubmatch(str, -1)
Expand All @@ -261,7 +261,7 @@ func (t *TemplateResolver) processForAutoIndent(str string) string {
processed := str
for _, submatch := range submatches {
numSpaces := len(submatch[1]) - int(t.config.AdditionalIndentation)
matchStr := submatch[0]
matchStr := submatch[2]
newMatchStr := strings.Replace(matchStr, "autoindent", fmt.Sprintf("indent %d", numSpaces), 1)
processed = strings.Replace(processed, matchStr, newMatchStr, 1)
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ func TestResolveTemplate(t *testing.T) {
"spec:\n config1: |\n hello\n world",
nil,
},
{
"spec:\n autoindent-test: '{{ " + `"hello\nworld\nagain\n"` + " | autoindent }}'\n",
Config{AdditionalIndentation: 4},
struct{}{},
"spec:\n autoindent-test: hello world again",
nil,
},
{
`test: '{{ printf "hello %s" "world" }}'`,
Config{},
Expand Down