Skip to content

Commit

Permalink
Fix required env vars ignored except the last one
Browse files Browse the repository at this point in the history
During the variable substitution, if a missing variable was followed
by an existing one, the empty "err" variable overwrote the previous
non-empty variable.

Unlike with Docker Compose v1, the first found error did not guarantee
that an actual error would be thrown.

Example which would have run before the fix even though var1 is not defined:

```yaml
services:
  bash:
    image: bash:5.0.18-alpine3.15
    environment:
      var12: "_ ${var1:?Error1} _ ${var2:?Error2} _ "
    command:
      - env
```

```bash
# var2=1 docker compose up
```

This change also means that, if multiple variables are missing in one string, the first one will be reported by Docker Compose

Signed-off-by: Ákos Takács <takacs.akos@it-sziget.hu>
  • Loading branch information
rimelek committed Mar 20, 2022
1 parent e3cf279 commit bf8a55f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions interpolation/interpolation_test.go
Expand Up @@ -113,6 +113,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
14 changes: 12 additions & 2 deletions template/template.go
Expand Up @@ -63,8 +63,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
if len(subsFuncs) == 0 {
subsFuncs = getDefaultSortedSubstitutionFunctions(template)
}
var err error
var errReturn error
result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
var err error
closingBraceIndex := getFirstBraceClosingIndex(substring)
rest := ""
if closingBraceIndex > -1 {
Expand All @@ -87,6 +88,9 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su

if substitution == "" {
err = &InvalidTemplateError{Template: template}
if errReturn == nil {
errReturn = err
}
return ""
}

Expand All @@ -98,13 +102,19 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
)
value, applied, err = f(substitution, mapping)
if err != nil {
if errReturn == nil {
errReturn = err
}
return ""
}
if !applied {
continue
}
interpolatedNested, err := SubstituteWith(rest, mapping, pattern, subsFuncs...)
if err != nil {
if errReturn == nil {
errReturn = err
}
return ""
}
return value + interpolatedNested
Expand All @@ -118,7 +128,7 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
return value
})

return result, err
return result, errReturn
}

func getDefaultSortedSubstitutionFunctions(template string, fns ...SubstituteFunc) []SubstituteFunc {
Expand Down

0 comments on commit bf8a55f

Please sign in to comment.