Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cli/compose/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ type regexper interface {

// DefaultSubstituteFuncs contains the default SubstituteFunc used by the docker cli
var DefaultSubstituteFuncs = []SubstituteFunc{
softDefault,
hardDefault,
requiredNonEmpty,
required,
softDefault, // :-
requiredNonEmpty, // :?
required, // ?
hardDefault, // - (after ? operators so hyphens in error messages are safe)
}

// InvalidTemplateError is returned when a variable template is not in a valid
Expand Down Expand Up @@ -212,8 +212,14 @@ func softDefault(substitution string, mapping Mapping) (string, bool, error) {
return value, true, nil
}

// Hard default (fall back if-and-only-if empty)
// Hard default (fall back if-and-only-if unset)
func hardDefault(substitution string, mapping Mapping) (string, bool, error) {
// "?" / ":?" error messages may contain hyphens (e.g. "must be set - try again").
// Those operators are handled by required/requiredNonEmpty; do not treat the
// hyphen inside the message as the default-value separator.
if strings.Contains(substitution, "?") {
return "", false, nil
}
sep := "-"
if !strings.Contains(substitution, sep) {
return "", false, nil
Expand Down
9 changes: 9 additions & 0 deletions cli/compose/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ func TestMandatoryVariableErrors(t *testing.T) {
template: "not ok ${UNSET_VAR?}",
expectedError: "required variable UNSET_VAR is missing a value",
},
{
// Error message itself contains a hyphen; must not be treated as ${VAR-default}.
template: "not ok ${UNSET_VAR:?must be set - hyphen in this message}",
expectedError: "required variable UNSET_VAR is missing a value: must be set - hyphen in this message",
},
{
template: "not ok ${UNSET_VAR?must be set - hyphen in this message}",
expectedError: "required variable UNSET_VAR is missing a value: must be set - hyphen in this message",
},
}

for _, tc := range testCases {
Expand Down