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

Check sourced file exports expected variables #424

Closed
Globegitter opened this issue Dec 6, 2018 · 8 comments
Closed

Check sourced file exports expected variables #424

Globegitter opened this issue Dec 6, 2018 · 8 comments
Labels

Comments

@Globegitter
Copy link

Issue description

We are testing direnv for some of our projects and what we ended up doing so far is have one .envrc checked in that has static env vars like the port of the app etc. but then we also have the following section:

if [[ -f .envrc.secret ]]; then
    source_env .envrc.secret
fi 

which, as the name suggests, is a file which exports secret env vars and that is on gitignore. To now make sure that everyone exports the secrets in the right way we add a secondary file .envrc.secret.template which just lists stuff like export MY_SECRET1=XXX. It would be nice that rather than having to specify this secondary file we could just add some validation to the main .envrc that the .envrc.secret exports the env vars we expect.

Steps to reproduce

Create an .envrc file with:

if [[ -f .envrc.secret ]]; then
    source_env .envrc.secret
fi 

add a .envrc.secret file and after adding some env vars you have no idea to know if you added the expected ones unless the application needing these env vars will in some ways error or not work as expected.

Technical details

I imagine this could somehow already bee done in bash but having something like a source_env_validate <filename> EXPECTED_ENV_VAR1 EXPECTED_ENV_VAR2 ... would be great, or if there is already an other easy way to do this that would probably also suffice.

  • direnv version: 2.17.0
  • OS release: elementary OS 5.0 (Ubuntu 18.04)
@zimbatm zimbatm added the Feature label Dec 6, 2018
@zimbatm
Copy link
Member

zimbatm commented Dec 6, 2018

Hi @Globegitter,

Sounds like a useful thing to have. I don't think that direnv necessarily needs to be extended though as you could add your own checking function in the .envrc.

Eg:

check_vars() {
  for key in "$@"; do
    if [[ -z "${!key}" ]]; then echo "Missing $key var"; return 1; fi
  done
}

if [[ -f .envrc.secret ]]; then
  source_env .envrc.secret
  check_vars EXPECTED_ENV_VAR1 EXPECTED_ENV_VAR2
fi

@Globegitter
Copy link
Author

@zimbatm it is quite straight-forward indeed - is it also possible to extend the stdlib for all subdirectories rather than just via a ~/.direnvrc? The idea being here that I could put a .direnvrc at the root of our monorepo that defines this function and then all projects that make use of the .envrc have access to the method without having to redefine or source it.

@zimbatm
Copy link
Member

zimbatm commented Dec 6, 2018

The best option is to source the lib. It would add a single line per .envrc.

@Globegitter
Copy link
Author

Globegitter commented Dec 6, 2018

Makes sense and this is not something you would accept in the default stdlib? (if not feel free to close as the problem has been resolved otherwise)

@zimbatm
Copy link
Member

zimbatm commented Dec 7, 2018

My default it to try and keep direnv simple. If I hear that many people need a feature then I would be inclined to revisit this.

Thanks for the thorough bug report, it's interesting to see how other people are using my tool :)

@zimbatm zimbatm closed this as completed Dec 7, 2018
@Globegitter
Copy link
Author

Thanks for the response.

@tanguyantoine
Copy link

I would love to have a mechanism that based on a template could check env variable presence

@zimbatm
Copy link
Member

zimbatm commented Jul 30, 2019

It's not that hard to do.
Add this to your ~/.config/direnv/direnvrc file:

check_env() { local ret=0; for var in "$@"; do [[ -v $var ]] || { echo "$var is missing"; ret=1; }; done; return $ret; }

Example usage:

$ check_env HOME FOO BAR
FOO is missing
BAR is missing
$ echo $?
1

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

No branches or pull requests

3 participants