Skip to content

Commit

Permalink
Merge pull request #242 from rimelek/fix-required-envvars-ignored-exc…
Browse files Browse the repository at this point in the history
…ept-last

Fix required env vars ignored except the last one
  • Loading branch information
ulyssessouza committed Sep 7, 2022
2 parents 34d8f70 + 8ce8973 commit ccdcc95
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
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

0 comments on commit ccdcc95

Please sign in to comment.