diff --git a/man/direnv-stdlib.1 b/man/direnv-stdlib.1 index 674d9e9c4..801c82727 100644 --- a/man/direnv-stdlib.1 +++ b/man/direnv-stdlib.1 @@ -126,6 +126,27 @@ source_env_if_exists .envrc.private .fi .RE +.SS \fB\fCenv_vars_required [ ...]\fR +.PP +Logs error for every variable not present in the environment or having an empty value. +.br +Typically this is used in combination with source_env and source_env_if_exists. + +.PP +Example: + +.PP +.RS + +.nf +# expect .envrc.private to provide tokens +source_env .envrc.private +# check presence of tokens +env_vars_required GITHUB_TOKEN OTHER_TOKEN + +.fi +.RE + .SS \fB\fCsource_up []\fR .PP Loads another \fB\fC\&.envrc\fR if found when searching from the parent directory up to /. diff --git a/man/direnv-stdlib.1.md b/man/direnv-stdlib.1.md index a2d491dd0..c7d28f61b 100644 --- a/man/direnv-stdlib.1.md +++ b/man/direnv-stdlib.1.md @@ -90,6 +90,19 @@ Example: source_env_if_exists .envrc.private +### `env_vars_required [ ...]` + +Logs error for every variable not present in the environment or having an empty value. +Typically this is used in combination with source_env and source_env_if_exists. + +Example: + + # expect .envrc.private to provide tokens + source_env .envrc.private + # check presence of tokens + env_vars_required GITHUB_TOKEN OTHER_TOKEN + + ### `source_up []` Loads another `.envrc` if found when searching from the parent directory up to /. diff --git a/stdlib.sh b/stdlib.sh index a5e1610b9..372e76d5f 100755 --- a/stdlib.sh +++ b/stdlib.sh @@ -372,6 +372,33 @@ source_env_if_exists() { if [[ -f "$1" ]]; then source_env "$1"; fi } +# Usage: env_vars_required [ ...] +# +# Logs error for every variable not present in the environment or having an empty value. +# Typically this is used in combination with source_env and source_env_if_exists. +# +# Example: +# +# # expect .envrc.private to provide tokens +# source_env .envrc.private +# # check presence of tokens +# env_vars_required GITHUB_TOKEN OTHER_TOKEN +# +env_vars_required() { + local environment + local -i ret + environment=$(env) + ret=0 + + for var in "$@"; do + if [[ "$environment" != *"$var="* || -z ${!var:-} ]]; then + log_error "env var $var is required but missing/empty" + ret=1 + fi + done + return "$ret" +} + # Usage: watch_file [ ...] # # Adds each to the list of files that direnv will watch for changes - diff --git a/test/stdlib.bash b/test/stdlib.bash index b2207dc7c..55786501e 100755 --- a/test/stdlib.bash +++ b/test/stdlib.bash @@ -192,6 +192,26 @@ test_name source_env_if_exists [[ $FOO = bar ]] ) +test_name env_vars_required +( + load_stdlib + + export FOO=1 + env_vars_required FOO + + # these should all fail + # shellcheck disable=SC2034 + BAR=1 + export BAZ= + output="$(env_vars_required BAR BAZ MISSING 2>&1 > /dev/null || echo "--- result: $?")" + + [[ "${output#*'--- result: 1'}" != "$output" ]] + [[ "${output#*'BAR is required'}" != "$output" ]] + [[ "${output#*'BAZ is required'}" != "$output" ]] + [[ "${output#*'MISSING is required'}" != "$output" ]] +) + + # test strict_env and unstrict_env ./strict_env_test.bash