Skip to content

Commit ccdcc95

Browse files
authored
Merge pull request #242 from rimelek/fix-required-envvars-ignored-except-last
Fix required env vars ignored except the last one
2 parents 34d8f70 + 8ce8973 commit ccdcc95

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

interpolation/interpolation_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func TestValidUnexistentInterpolation(t *testing.T) {
114114
{test: "{{{ ${FOO:?foo_} }}}", errMsg: "foo_"},
115115
{test: "{{{ ${FOO:?foo-bar-value} }}}", errMsg: "foo-bar-value"},
116116
{test: "{{{ ${FOO:?foo} ${BAR:-DEFAULT_VALUE} }}}", errMsg: "foo"},
117+
{test: "${FOO:?foo} ${BAR:?bar}", errMsg: "foo"},
117118
{test: "{{{ ${BAR} }}}", expected: "{{{ }}}"},
118119
{test: "${FOO:?baz} }}}", errMsg: "baz"},
119120
{test: "${FOO?baz} }}}", errMsg: "baz"},

template/template.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type SubstituteFunc func(string, Mapping) (string, bool, error)
6262
// It accepts additional substitute function.
6363
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
6464
var outerErr error
65+
var returnErr error
6566

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

9293
if substitution == "" {
9394
outerErr = &InvalidTemplateError{Template: template}
95+
if returnErr == nil {
96+
returnErr = outerErr
97+
}
9498
return ""
9599
}
96100

@@ -101,6 +105,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
101105
)
102106
value, applied, outerErr = subsFunc(substitution, mapping)
103107
if outerErr != nil {
108+
if returnErr == nil {
109+
returnErr = outerErr
110+
}
104111
return ""
105112
}
106113
if applied {
@@ -119,7 +126,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
119126
return value
120127
})
121128

122-
return result, outerErr
129+
return result, returnErr
123130
}
124131

125132
func getSubstitutionFunctionForTemplate(template string) (string, SubstituteFunc) {

0 commit comments

Comments
 (0)