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

Fix required env vars ignored except the last one #242

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions interpolation/interpolation_test.go
Expand Up @@ -114,6 +114,7 @@ func TestValidUnexistentInterpolation(t *testing.T) {
{test: "{{{ ${FOO:?foo_} }}}", errMsg: "foo_"},
{test: "{{{ ${FOO:?foo-bar-value} }}}", errMsg: "foo-bar-value"},
{test: "{{{ ${FOO:?foo} ${BAR:-DEFAULT_VALUE} }}}", errMsg: "foo"},
{test: "${FOO:?foo} ${BAR:?bar}", errMsg: "foo"},
{test: "{{{ ${BAR} }}}", expected: "{{{ }}}"},
{test: "${FOO:?baz} }}}", errMsg: "baz"},
{test: "${FOO?baz} }}}", errMsg: "baz"},
Expand Down
9 changes: 8 additions & 1 deletion template/template.go
Expand Up @@ -62,6 +62,7 @@ type SubstituteFunc func(string, Mapping) (string, bool, error)
// It accepts additional substitute function.
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
var outerErr error
var returnErr error

result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
_, subsFunc := getSubstitutionFunctionForTemplate(substring)
Expand Down Expand Up @@ -91,6 +92,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su

if substitution == "" {
outerErr = &InvalidTemplateError{Template: template}
if returnErr == nil {
returnErr = outerErr
}
return ""
}

Expand All @@ -101,6 +105,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
)
value, applied, outerErr = subsFunc(substitution, mapping)
if outerErr != nil {
if returnErr == nil {
returnErr = outerErr
}
return ""
}
if applied {
Expand All @@ -119,7 +126,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
return value
})

return result, outerErr
return result, returnErr
}

func getSubstitutionFunctionForTemplate(template string) (string, SubstituteFunc) {
Expand Down