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 presence of env-vars #843

Closed
eval opened this issue Nov 23, 2021 · 3 comments · Fixed by #872
Closed

Check presence of env-vars #843

eval opened this issue Nov 23, 2021 · 3 comments · Fixed by #872
Labels

Comments

@eval
Copy link
Contributor

eval commented Nov 23, 2021

Is your feature request related to a problem? Please describe.

I often use the following setup when working with teams:

  • .envrc.local git-ignored (originating from .envrc.local.example) and .envrc in git containing:
# .envrc
export DATABASE_URL=postgres://localhost:5432/project_development

source_env_if_exists .envrc.local
# Beyond this point we need SOME_TOKEN to be present

So DATABASE_URL is a variable that is straightforward: we can provide a sane default value and if needed it can be locally overridden.

There are also variables that our project requires but 1) for which we cannot provide plausible defaults (vary too much per developer) or 2) that are secrets which we won't include in .envrc.

Describe the solution you'd like

For these types of variables it would be nice to have the possibility to check their presence and warn if absent:

# at end of .envrc
env_vars_required GITHUB_NAME, SOME_TOKEN
@eval eval added the Feature label Nov 23, 2021
@zimbatm
Copy link
Member

zimbatm commented Nov 23, 2021

Sounds good. It would something like this:

env_vars_required() {
  for var in "$@"; do
    if [[ -z ${!var} ]]; then
      echo "env var $var is required and missing" >&2
      return 1
    fi
  done
}

@eval
Copy link
Contributor Author

eval commented Nov 29, 2021

Thanks - I reworked it slightly to 1) catch non-exported/empty variables (which we had a few of 😳 ) and 2) to not exit and log all missing variables.

env_vars_required() {
  for var in "$@"; do
    if ! env | grep -q '^'"$var"'=\w'; then
      log_error "env var $var is required but missing/empty"
    fi
  done
}

@zimbatm Would it be something to include in the stdlib? If so, I'll happily submit a PR.

@zimbatm
Copy link
Member

zimbatm commented Dec 23, 2021

Yeah, something like that could be nice to have. Here is an even better version:

  • avoid spawning env | bash on each var
  • print all the missing envs, and return with error at the end
env_vars_required() {
  ret=0
  for var in "$@"; do
    if [[ -z ${!var:-} ]]; then
      log_error "env var $var is required but missing/empty"
      ret=1
    fi
  done
  return "$ret"
}

eval added a commit to eval/direnv that referenced this issue Dec 29, 2021
Check presence of env vars:

    env_vars_required SOME_TOKEN

Fixes direnv#843
eval added a commit to eval/direnv that referenced this issue Dec 29, 2021
Check presence of env vars:

    env_vars_required SOME_TOKEN

Fixes direnv#843
eval added a commit to eval/direnv that referenced this issue Dec 29, 2021
Check presence of env vars:

    env_vars_required SOME_TOKEN

Fixes direnv#843
zimbatm pushed a commit that referenced this issue Dec 29, 2021
Check presence of env vars:

    env_vars_required SOME_TOKEN

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

Successfully merging a pull request may close this issue.

2 participants