-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add options to the compose loader #1128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add options to the compose loader #1128
Conversation
f57d7df to
53f3114
Compare
silvin-lubecki
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think some unit tests would help here 😄
cli/compose/template/template.go
Outdated
| Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage), | ||
| } | ||
| for _, f := range subsFuncs { | ||
| var value string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
var(
value string
applied bool
)
cli/compose/template/template.go
Outdated
|
|
||
| // Soft default (fall back if unset or empty) | ||
| func softDefault(substitution string, mapping Mapping) (string, bool, error) { | ||
| if strings.Contains(substitution, ":-") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
if !strings.Contains(substitution, ":-"){
return "", false, nil
}
...
cli/compose/template/template.go
Outdated
|
|
||
| // Hard default (fall back if-and-only-if empty) | ||
| func hardDefault(substitution string, mapping Mapping) (string, bool, error) { | ||
| if strings.Contains(substitution, "-") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
cli/compose/template/template.go
Outdated
| } | ||
|
|
||
| func requiredNonEmpty(substitution string, mapping Mapping) (string, bool, error) { | ||
| if strings.Contains(substitution, ":?") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
cli/compose/template/template.go
Outdated
| } | ||
|
|
||
| func required(substitution string, mapping Mapping) (string, bool, error) { | ||
| if strings.Contains(substitution, "?") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
|
Linter is complaining: |
06c6486 to
de77f86
Compare
de77f86 to
3367be8
Compare
cli/compose/template/template.go
Outdated
|
|
||
| // Substitute variables in the string with their values | ||
| func Substitute(template string, mapping Mapping) (string, error) { | ||
| // SubstituteFunc is a user-supplied function that apply substition. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/substition/substitution/
cli/compose/template/template.go
Outdated
| func Substitute(template string, mapping Mapping) (string, error) { | ||
| // SubstituteFunc is a user-supplied function that apply substition. | ||
| // Returns the value as a string, a bool indicating if the function could apply | ||
| // the substition and an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/substition/substitution/
f05c771 to
f8ffc8e
Compare
cli/compose/loader/loader.go
Outdated
|
|
||
| // Load reads a ConfigDetails and returns a fully loaded configuration | ||
| func Load(configDetails types.ConfigDetails) (*types.Config, error) { | ||
| func Load(configDetails types.ConfigDetails, ops ...func(*Options)) (*types.Config, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps s/ops/options/ but that's a nit
| } | ||
|
|
||
| for _, op := range ops { | ||
| op(opts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be the standard pattern, but was just thinking why functional options cannot produce an error 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thaJeztah they could 😉 I just though they would not here 😝
cli/compose/loader/loader.go
Outdated
| if err != nil { | ||
| return nil, err | ||
| if !opts.SkipInterpolation { | ||
| var err error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this var err .. ? There's more err variables in this function, so having this here will be confusing.
|
|
||
| if err := schema.Validate(configDict, configDetails.Version); err != nil { | ||
| return nil, err | ||
| if !opts.SkipValidation { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the reason to allow skipping "validation" is that if "interpolation" is skipped, there's values that are invalid (because, e.g. $port is not a valid port-number)?
Is there a use-case to disable validation or interpolation separately?
Also, trying to think of situations where skipping validation could skip important checks (although, I guess in the end that's the daemon/API's responsibility)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't interpolate, for sure we don't want to validate (otherwise the composefile could be invalid). The other way could be happens, i.e. doing some interpolation but not validating (for whatever reason, like if it's later on used with something else).
Also, trying to think of situations where skipping validation could skip important checks (although, I guess in the end that's the daemon/API's responsibility)
Yes. And also, this is only in the library part of cli/compose — i.e. with this PR it is not possible to not validate or interpolate (and we shouldn't allow that with the docker cli) 😉
cli/compose/template/template.go
Outdated
| type SubstituteFunc func(string, Mapping) (string, bool, error) | ||
|
|
||
| // SubstituteWith subsitute variables in the string with their values. | ||
| // It accepts additionnal substitute function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/additionnal/additional/
cli/compose/template/template.go
Outdated
| // SubstituteWith subsitute variables in the string with their values. | ||
| // It accepts additionnal substitute function. | ||
| func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) { | ||
| subsFuncs = append([]SubstituteFunc{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we always append the default functions, or only do so if no custom functions are provided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I did that to not export the default function, but looking back at it, this function should probably just take the list and use them (letting user decide which one to apply)
| applied bool | ||
| ) | ||
| value, applied, err = f(substitution, mapping) | ||
| if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the error itself discarded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thaJeztah it's not if it's the last one to be applied (i.e. if one function doesn't apply, or apply with error, it doesn't necessarly mean others wont).
f8ffc8e to
468e96d
Compare
- Add the possibility to skip interpolation - Add the possibility to skip schema validation - Allow customizing the substitution function, to add special cases. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
468e96d to
9fdd14f
Compare
thaJeztah
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Example of use.
Again, this is gonna be very useful for
docker/app👼Signed-off-by: Vincent Demeester vincent@sbr.pm