Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Merge pull request #651 from jcsirot/composefile-syntax-improved-erro…
Browse files Browse the repository at this point in the history
…r-messages

Better error messages when using unsupported compose file syntax
  • Loading branch information
rumpl committed Oct 7, 2019
2 parents c6102cc + 994bc0d commit e0122a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
19 changes: 13 additions & 6 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ const (
// variable name must start with at least one of the the following: a-z, A-Z or _
substitution = `[a-zA-Z_]+([a-zA-Z0-9_]*(([.]{1}[0-9a-zA-Z_]+)|([0-9a-zA-Z_])))*`
// compose files may contain variable names followed by default values/error messages with separators ':-', '-', ':?' and '?'.
defaultValuePattern = `[a-zA-Z_]+[a-zA-Z0-9_.]*((:-)|(\-)|(:\?)|(\?)){1}(.*)`
defaultValuePattern = `[a-zA-Z_]+[a-zA-Z0-9_.]*((:-)|(\-)){1}(.*)`
// compose files may contain variable names followed by default values/error messages with separators ':-', '-', ':?' and '?'.
errorMessagePattern = `[a-zA-Z_]+[a-zA-Z0-9_.]*((:\?)|(\?)){1}(.*)`
)

var (
patternString = fmt.Sprintf(
`%s(?i:(?P<named>%s)|(?P<skip>%s{1,})|\{(?P<braced>%s)\}|\{(?P<fail>%s)\})`,
delimiter, substitution, delimiter, substitution, defaultValuePattern,
`%s(?i:(?P<named>%s)|(?P<skip>%s{1,})|\{(?P<braced>%s)\}|\{(?P<defvals>%s)\}|\{(?P<errormsg>%s)\})`,
delimiter, substitution, delimiter, substitution, defaultValuePattern, errorMessagePattern,
)
rePattern = regexp.MustCompile(patternString)
)
Expand Down Expand Up @@ -74,8 +76,13 @@ func substituteParams(allParameters map[string]string, composeContent string) (s
groups[name] = match[i+1]
}
//fail on default values enclosed within {}
if fail := groups["fail"]; fail != "" {
return "", errors.New(fmt.Sprintf("Parameters must not have default values set in compose file. Invalid parameter: %s.", match[0]))
if fail := groups["defvals"]; fail != "" {
return "", errors.Errorf("The default value syntax of Compose files is not supported in Docker App. "+
"The characters ':' and '-' are not allowed in parameter names. Invalid parameter: %s.", match[0])
}
if fail := groups["errormsg"]; fail != "" {
return "", errors.Errorf("The custom error message syntax of Compose files is not supported in Docker App. "+
"The characters ':' and '?' are not allowed in parameter names. Invalid parameter: %s.", match[0])
}
if skip := groups["skip"]; skip != "" {
continue
Expand All @@ -88,7 +95,7 @@ func substituteParams(allParameters map[string]string, composeContent string) (s
if value, ok := allParameters[val]; ok {
composeContent = strings.ReplaceAll(composeContent, varString, value)
} else {
return "", errors.New(fmt.Sprintf("Failed to set value for %s. Value not found in parameters.", val))
return "", errors.Errorf("Failed to set value for %s. Value not found in parameters.", val)
}
}
return composeContent, nil
Expand Down
12 changes: 8 additions & 4 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ services:
userParameters := map[string]string{
"front.port": "4242",
}
checkRenderError(t, userParameters, composeFile, "Parameters must not have default values set in compose file. Invalid parameter: ${front.port:-9090}.")
checkRenderError(t, userParameters, composeFile, "The default value syntax of Compose files is not supported in Docker App. "+
"The characters ':' and '-' are not allowed in parameter names. Invalid parameter: ${front.port:-9090}.")

composeFile = `
version: "3.6"
Expand All @@ -92,15 +93,17 @@ services:
ports:
- "${front.port-9090}:80"
`
checkRenderError(t, userParameters, composeFile, "Parameters must not have default values set in compose file. Invalid parameter: ${front.port-9090}.")
checkRenderError(t, userParameters, composeFile, "The default value syntax of Compose files is not supported in Docker App. "+
"The characters ':' and '-' are not allowed in parameter names. Invalid parameter: ${front.port-9090}.")
composeFile = `
version: "3.6"
services:
front:
ports:
- "${front.port:?Error}:80"
`
checkRenderError(t, userParameters, composeFile, "Parameters must not have default values set in compose file. Invalid parameter: ${front.port:?Error}.")
checkRenderError(t, userParameters, composeFile, "The custom error message syntax of Compose files is not supported in Docker App. "+
"The characters ':' and '?' are not allowed in parameter names. Invalid parameter: ${front.port:?Error}.")

composeFile = `
version: "3.6"
Expand All @@ -109,7 +112,8 @@ services:
ports:
- "${front.port?Error:unset variable}:80"
`
checkRenderError(t, userParameters, composeFile, "Parameters must not have default values set in compose file. Invalid parameter: ${front.port?Error:unset variable}.")
checkRenderError(t, userParameters, composeFile, "The custom error message syntax of Compose files is not supported in Docker App. "+
"The characters ':' and '?' are not allowed in parameter names. Invalid parameter: ${front.port?Error:unset variable}.")

}
func TestSubstituteMixedParams(t *testing.T) {
Expand Down

0 comments on commit e0122a7

Please sign in to comment.