Skip to content
Closed
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
39 changes: 27 additions & 12 deletions cli/compose/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,40 @@ func extractVariable(value any, pattern regexper) ([]extractedValue, bool) {
}
name := val
var defaultValue string
switch {
case strings.Contains(val, ":?"):
name, _ = partition(val, ":?")
case strings.Contains(val, "?"):
name, _ = partition(val, "?")
case strings.Contains(val, ":-"):
name, defaultValue = partition(val, ":-")
case strings.Contains(val, "-"):
name, defaultValue = partition(val, "-")
switch sep := operatorSep(val); sep {
case ":?", "?":
name, _ = partition(val, sep)
case ":-", "-":
name, defaultValue = partition(val, sep)
}
values = append(values, extractedValue{name: name, value: defaultValue})
}
return values, len(values) > 0
}

// operatorSep is the interpolation operator that applies to substitution.
// The earliest of :?, :-, ? and - wins, so a hyphen or question mark later
// in an error message or default is not treated as another operator.
func operatorSep(substitution string) string {
bestAt := -1
best := ""
for _, sep := range []string{":?", ":-", "?", "-"} {
at := strings.Index(substitution, sep)
if at < 0 {
continue
}
if bestAt < 0 || at < bestAt || (at == bestAt && len(sep) > len(best)) {
bestAt = at
best = sep
}
}
return best
}

// Soft default (fall back if unset or empty)
func softDefault(substitution string, mapping Mapping) (string, bool, error) {
sep := ":-"
if !strings.Contains(substitution, sep) {
if operatorSep(substitution) != sep {
return "", false, nil
}
name, defaultValue := partition(substitution, sep)
Expand All @@ -215,7 +230,7 @@ func softDefault(substitution string, mapping Mapping) (string, bool, error) {
// Hard default (fall back if-and-only-if empty)
func hardDefault(substitution string, mapping Mapping) (string, bool, error) {
sep := "-"
if !strings.Contains(substitution, sep) {
if operatorSep(substitution) != sep {
return "", false, nil
}
name, defaultValue := partition(substitution, sep)
Expand All @@ -235,7 +250,7 @@ func required(substitution string, mapping Mapping) (string, bool, error) {
}

func withRequired(substitution string, mapping Mapping, sep string, valid func(string) bool) (string, bool, error) {
if !strings.Contains(substitution, sep) {
if operatorSep(substitution) != sep {
return "", false, nil
}
name, errorMessage := partition(substitution, sep)
Expand Down
36 changes: 36 additions & 0 deletions cli/compose/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ func TestMandatoryVariableErrors(t *testing.T) {
}
}

func TestRequiredMessageMayContainHyphen(t *testing.T) {
testCases := []struct {
template string
expectedError string
}{
{
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",
},
{
template: "not ok ${BAR:?must be set - hyphen in this message}",
expectedError: "required variable BAR is missing a value: must be set - hyphen in this message",
},
}
for _, tc := range testCases {
_, err := Substitute(tc.template, defaultMapping)
assert.Check(t, is.ErrorContains(err, tc.expectedError))
}

result, err := Substitute("ok ${FOO:?must be set - hyphen in this message}", defaultMapping)
assert.NilError(t, err)
assert.Check(t, is.Equal("ok first", result))

result, err = Substitute("ok ${missing-foo?bar}", defaultMapping)
assert.NilError(t, err)
assert.Check(t, is.Equal("ok foo?bar", result))

result, err = Substitute("ok ${missing:-foo?bar}", defaultMapping)
assert.NilError(t, err)
assert.Check(t, is.Equal("ok foo?bar", result))
}

func TestDefaultsForMandatoryVariables(t *testing.T) {
testCases := []struct {
template string
Expand Down
Loading