Skip to content

Commit

Permalink
caddyfile: fix env var expansion after Go template (caddyserver#2304)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhcleek authored and mholt committed Oct 30, 2018
1 parent 1f8d1df commit f46da40
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,12 @@ func replaceEnvVars(s string) string {
func replaceEnvReferences(s, refStart, refEnd string) string {
index := strings.Index(s, refStart)
for index != -1 {
endIndex := strings.Index(s, refEnd)
endIndex := strings.Index(s[index:], refEnd)
if endIndex == -1 {
break
}

endIndex += index
if endIndex > index+len(refStart) {
ref := s[index : endIndex+len(refEnd)]
s = strings.Replace(s, ref, os.Getenv(ref[len(refStart):len(ref)-len(refEnd)]), -1)
Expand Down
7 changes: 7 additions & 0 deletions caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,13 @@ func TestEnvironmentReplacement(t *testing.T) {
if actual, expected := blocks[0].Tokens["dir1"][1].Text, "Test foobar test"; expected != actual {
t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual)
}

// after end token
p = testParser(":1234\nanswer \"{{ .Name }} {$FOOBAR}\"")
blocks, _ = p.parseAll()
if actual, expected := blocks[0].Tokens["answer"][1].Text, "{{ .Name }} foobar"; expected != actual {
t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual)
}
}

func testParser(input string) parser {
Expand Down

0 comments on commit f46da40

Please sign in to comment.