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

stdlib: add env_vars_required #872

Merged
merged 1 commit into from Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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