Skip to content

Commit

Permalink
stdlib: add env_vars_required (#872)
Browse files Browse the repository at this point in the history
Check presence of env vars:

    env_vars_required SOME_TOKEN

Fixes #843
  • Loading branch information
eval committed Dec 29, 2021
1 parent af4363e commit d6ad475
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
21 changes: 21 additions & 0 deletions man/direnv-stdlib.1
Expand Up @@ -126,6 +126,27 @@ source_env_if_exists .envrc.private
.fi
.RE

.SS \fB\fCenv_vars_required <varname> [<varname> ...]\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 [<filename>]\fR
.PP
Loads another \fB\fC\&.envrc\fR if found when searching from the parent directory up to /.
Expand Down
13 changes: 13 additions & 0 deletions man/direnv-stdlib.1.md
Expand Up @@ -90,6 +90,19 @@ Example:

source_env_if_exists .envrc.private

### `env_vars_required <varname> [<varname> ...]`

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 [<filename>]`

Loads another `.envrc` if found when searching from the parent directory up to /.
Expand Down
27 changes: 27 additions & 0 deletions stdlib.sh
Expand Up @@ -372,6 +372,33 @@ source_env_if_exists() {
if [[ -f "$1" ]]; then source_env "$1"; fi
}

# Usage: env_vars_required <varname> [<varname> ...]
#
# 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 <filename> [<filename> ...]
#
# Adds each <filename> to the list of files that direnv will watch for changes -
Expand Down
20 changes: 20 additions & 0 deletions test/stdlib.bash
Expand Up @@ -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

Expand Down

0 comments on commit d6ad475

Please sign in to comment.