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 a envOrDefault helper #829

Closed
nicolasbernard opened this issue Dec 22, 2016 · 7 comments
Closed

Add a envOrDefault helper #829

nicolasbernard opened this issue Dec 22, 2016 · 7 comments
Milestone

Comments

@nicolasbernard
Copy link

nicolasbernard commented Dec 22, 2016

Feature Request

For a new project we use a bench of {{ if env "TIMEOUT_CONNECT" }}{{ env "TIMEOUT_CONNECT" }}{{ else }}5s{{ end }} which is a bit verbose and redundant.

It would be very conveniant to use a syntax like {{envOrDefault "TIMEOUT_CONNECT" "5s"}}

It should be trivial to implement, I can make a pull request if you are ok with it.

@sethvargo
Copy link
Contributor

Hi @nikkau

You should be able to use the built-in or operator like this:

{{ or (env "TIMEOUT_CONNECT") "5s" }} 

Does that work?

@nicolasbernard
Copy link
Author

Hi,

It works, but {{ env }} rely on os.Getenv which always return a string, an empty one when the var doesn't exists, it works only because {{ or }} consider "" falsy which is not the case in all language, so not intuitive imho.

{{ envOrDefault }} could use os.LookupEnv to return default value only when the var doesn't exists in environment.

I don't know if someone need this stricter behavior (today I don't, the "or pattern" does the trick).

I also think explicit helper have value for the sake of explicitness, for coherence with the existence of keyOrDefault

@sethvargo
Copy link
Contributor

Hi @nikkau

Thank you for your response. I am going to close this out until someone has a stricter use case. Thanks!

@dfredell
Copy link
Contributor

dfredell commented Dec 12, 2017

Hello,

I was searching high and low for this answer. {{ or (env "TIMEOUT_CONNECT") "5s" }} does exactly what I want it to do. It was just hard to find.

I would like to make it easier for the next person to find. Can I make a PR to update the docs and add this example?

The another option would be to make the envOrDefault function like @nikkau suggested. It would be very intuitive and easy to read. {{envOrDefault "TIMEOUT_CONNECT" "5s"}}

@slackpad
Copy link
Contributor

slackpad commented Jan 5, 2018

Can I make a PR to update the docs and add this example?

Sure @dfredell we'd welcome that!

@jtrinklein
Copy link
Contributor

I also had a very hard time finding the solution to this. I'd like to submit a PR to add the envOrDefault helper.

@eikenb
Copy link
Contributor

eikenb commented Mar 29, 2021

@jtrinklein, if you'd like to submit a PR with this feature I'll happily review it for possible inclusion. Please remember tests. Thanks.

eikenb added a commit that referenced this issue May 21, 2021
…helper

Fix/master: add envOrDefault helper

Fixes #829
@eikenb eikenb added this to the 0.26.0 milestone May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants