diff --git a/cli/compose/template/template.go b/cli/compose/template/template.go index 1762ab11a9d8..b958bde4015c 100644 --- a/cli/compose/template/template.go +++ b/cli/compose/template/template.go @@ -176,15 +176,21 @@ func extractVariable(value interface{}, pattern *regexp.Regexp) ([]extractedValu // Soft default (fall back if unset or empty) func softDefault(substitution string, mapping Mapping) (string, bool, error) { - return withDefault(substitution, mapping, "-:") + sep := ":-" + if !strings.Contains(substitution, sep) { + return "", false, nil + } + name, defaultValue := partition(substitution, sep) + value, ok := mapping(name) + if !ok || value == "" { + return defaultValue, true, nil + } + return value, true, nil } // Hard default (fall back if-and-only-if empty) func hardDefault(substitution string, mapping Mapping) (string, bool, error) { - return withDefault(substitution, mapping, "-") -} - -func withDefault(substitution string, mapping Mapping, sep string) (string, bool, error) { + sep := "-" if !strings.Contains(substitution, sep) { return "", false, nil } diff --git a/cli/compose/template/template_test.go b/cli/compose/template/template_test.go index 672a7e75d95b..ce3690410ffb 100644 --- a/cli/compose/template/template_test.go +++ b/cli/compose/template/template_test.go @@ -78,6 +78,12 @@ func TestEmptyValueWithSoftDefault(t *testing.T) { assert.Check(t, is.Equal("ok def", result)) } +func TestValueWithSoftDefault(t *testing.T) { + result, err := Substitute("ok ${FOO:-def}", defaultMapping) + assert.NilError(t, err) + assert.Check(t, is.Equal("ok first", result)) +} + func TestEmptyValueWithHardDefault(t *testing.T) { result, err := Substitute("ok ${BAR-def}", defaultMapping) assert.NilError(t, err)