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

Bash style variable expension in env var supports defaults using variables #2223

Merged
merged 1 commit into from
Aug 30, 2019
Merged

Bash style variable expension in env var supports defaults using variables #2223

merged 1 commit into from
Aug 30, 2019

Conversation

wilmardo
Copy link
Contributor

@wilmardo wilmardo commented Aug 13, 2019

Closes: #2250

PR Type

  • Feature Pull Request

Expand the interpolation so expands default defined variables. Based on the implementation in bash:

# export TEST=test
# echo ${NONE:-$TEST}
test
# echo ${NONE:-$NONE}

When the default variable is not found it returns an empty string.

I use this to postfix the platform names so no conflicts happen, in the CI it uses a predefined Gitlab variable, for local testing it takes the user running molecule.

platforms:
  - name: Docker-CE-${CI_COMMIT_REF_SLUG:-$VMWARE_USER}

Changes:

  • Expanded the existing test so it tests both the curly and non curly braced syntax
  • Added a testcase for the existing default string interpolation
  • Implemented default variable interpolation and added a testcase

@wilmardo
Copy link
Contributor Author

Same unrelated PR fail as in #2193

Copy link
Member

@ssbarnea ssbarnea left a comment

Choose a reason for hiding this comment

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

I like the feature but the implicit expansion to empty string worries me. I know that bash has this behavior when not run in strict mode.

Still, I am not very keen on replicating bash bugs, especially as one like this would easily get ignored.

I am not +1/-1 because I want to see what other think about desired behavior when variable is not defined.

I personally prefer getting an error and asking user to provide a default if they want, like: $FOO would give error but if you put ${FOO:-} it will not be an error as you mention explicitly that undefined goes as empty string.

@wilmardo
Copy link
Contributor Author

@ssbarnea Thanks for the feedback! I am open for changes and I am keen to what other have to say!

My reasoning is that this behavior lays in the extend of the syntax and is expected for the endusers (Bash is default non strict).

@wilmardo
Copy link
Contributor Author

@decentral1se Any input on this PR?

Copy link
Contributor

@decentral1se decentral1se left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@fabianvf fabianvf left a comment

Choose a reason for hiding this comment

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

Big +1 from me, I've definitely wanted this at least a couple times. My only comment is that it might make sense to make the interpolation recursive (so you could do ${UNDEFINED:-${STILL_UNDEFINED:-hello}}), but I've never really needed more than two levels anyway so it's probably more complex than it's worth.

@wilmardo
Copy link
Contributor Author

@fabianvf Thanks for the opinion! I think the recursive option is a little overkill, at some point you should reconsider your approach ;)

Will fix the conflicts first thing tomorrow :)

Signed-off-by: wilmardo <info@wilmardenouden.nl>
@ssbarnea ssbarnea changed the title feat(config): bash style variable expension in env var defaults Bash style variable expension in env var supports defaults using variables Aug 30, 2019
@ssbarnea ssbarnea merged commit aa438bb into ansible:master Aug 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recursive evaluation of shell variables does not work in molecule.yml
4 participants