Skip to content
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 support for mandatory variables to stack deploy #893

Conversation

adshmh
Copy link
Contributor

@adshmh adshmh commented Feb 21, 2018

This PR adds support for mandatory variables syntax to stack deploy. Fixes #805

Signed-off-by: Arash Deshmeh adeshmeh@ca.ibm.com

- What I did
Added support for mandatory variables syntax, i.e. ${VAR?error message} and ${VAR:?error message}, to stack deploy command.

- How I did it
Added the code required to handle the syntax in cli/compose/template/template.go, and the corresponding tests in cli/compose/template/template_test.go

- How to verify it
Run stack deploy command on a compose file that contains mandatory variable(s).

- Description for the changelog
Added support for mandatory variables to stack deploy command.

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah
Copy link
Member

Linter is complaining;

cli/compose/template/template.go:36::warning: cyclomatic complexity 17 of function Substitute() is high (> 16) (gocyclo)

@thaJeztah
Copy link
Member

Otherwise, change LGTM; tested this, and looks to work as expected

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm preparing a patch to fix the gocyclo issue.

name, errorMessage := partition(substitution, ":?")
value, ok := mapping(name)
if !ok || value == "" {
err = &InvalidTemplateError{Template: errorMessage}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message should include something about "missing value" or "value required"

for _, tc := range testCases {
_, err := Substitute(tc.template, defaultMapping)
assert.Error(t, err)
assert.IsType(t, &InvalidTemplateError{tc.expectedError}, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check the error message as well as the type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. I have updated the PR. Also removed the tc.expectedError from the InvalidTemplateError as it seemed unnecessary.

@codecov-io
Copy link

codecov-io commented Feb 21, 2018

Codecov Report

Merging #893 into master will increase coverage by 0.06%.
The diff coverage is 97.43%.

@@            Coverage Diff             @@
##           master     #893      +/-   ##
==========================================
+ Coverage   53.26%   53.32%   +0.06%     
==========================================
  Files         258      258              
  Lines       16357    16374      +17     
==========================================
+ Hits         8712     8731      +19     
+ Misses       7081     7079       -2     
  Partials      564      564

@dnephin
Copy link
Contributor

dnephin commented Feb 21, 2018

The commit I pushed should fix the lint error

}{
{
template: "not ok ${UNSET_VAR:?Mandatory Variable Unset}",
expectedError: "Mandatory Variable Unset",
Copy link
Contributor

@dnephin dnephin Feb 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this test case. Why would anyone put a default if the value is required?
Edit: Oh I see, apparently that's how it's supposed to work.

The error message should contain something like "required variable UNSET_VAR is missing a value: Mandatory Variable Unset"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference; this is what it looks like for /bin/sh and Bash:

echo ${UNSET_VAR:?Mandatory Variable Unset}
sh: UNSET_VAR: Mandatory Variable Unset

echo ${UNSET_VAR:?Mandatory Variable Unset}
bash: UNSET_VAR: Mandatory Variable Unset

},
{
template: "not ok ${UNSET_VAR:?}",
expectedError: "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error here should be something like "required variable UNSET_VAR is missing a value"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I am assuming you are referring to the error message in the test cases here, not changing the Substitute function, is that correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think the error from Substitute should be something like this:

err = &InvalidTemplateError{
    Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
}

For reference, this is the error in docker-compose: https://github.com/docker/compose/pull/5531/files#diff-b2372f2a81cc5911e04cc7df7d68675bR65

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. I updated the PR (the reg. exp. also needed to be fixed to cover empty error messages)

Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as @dnephin, otherwise looks good 😉

adshmh and others added 4 commits February 26, 2018 15:29
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
Signed-off-by: Daniel Nephin <dnephin@docker.com>
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
@adshmh adshmh force-pushed the add-support-for-mandatory-variables-syntax-to-stack-deploy branch from ac2331f to 5d8ce59 Compare February 26, 2018 20:39
Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🐸

@vdemeester vdemeester merged commit 750b038 into docker:master Feb 27, 2018
@GordonTheTurtle GordonTheTurtle added this to the 18.04.0 milestone Feb 27, 2018
@thaJeztah
Copy link
Member

Docs were added in docker/docs#5826

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants