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

source_up_if_exists: A strict_env compatible version of source_up #921

Merged
merged 4 commits into from Apr 14, 2022
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
11 changes: 10 additions & 1 deletion man/direnv-stdlib.1
Expand Up @@ -149,7 +149,16 @@ env_vars_required GITHUB_TOKEN OTHER_TOKEN

.SS \fB\fCsource_up [<filename>]\fR
.PP
Loads another \fB\fC\&.envrc\fR if found when searching from the parent directory up to /.
Loads another \fB\fC\&.envrc\fR if found with the find_up command. Returns 1 if no file
is found.

.PP
NOTE: the other \fB\fC\&.envrc\fR is not checked by the security framework.

.SS \fB\fCsource_up_if_exists [<filename>]\fR
.PP
Loads another \fB\fC\&.envrc\fR if found with the find_up command. If one is not
found, nothing happens.

.PP
NOTE: the other \fB\fC\&.envrc\fR is not checked by the security framework.
Expand Down
38 changes: 32 additions & 6 deletions stdlib.sh
Expand Up @@ -416,17 +416,43 @@ watch_dir() {
eval "$("$direnv" watch-dir bash "$1")"
}

# Usage: _source_up [<filename>] [true|false]
#
# Private helper function for source_up and source_up_if_exists. The second
# parameter determines if it's an error for the file we're searching for to
# not exist.
_source_up() {
local envrc file=${1:-.envrc}
local ok_if_not_exist=${2}
envrc=$(cd .. && (find_up "$file" || true))
if [[ -n $envrc ]]; then
source_env "$envrc"
elif $ok_if_not_exist; then
return 0
else
log_error "No ancestor $file found"
return 1
fi
}

# Usage: source_up [<filename>]
#
# Loads another ".envrc" if found with the find_up command.
# Loads another ".envrc" if found with the find_up command. Returns 1 if no
# file is found.
#
# NOTE: the other ".envrc" is not checked by the security framework.
source_up() {
local dir file=${1:-.envrc}
dir=$(cd .. && find_up "$file")
if [[ -n $dir ]]; then
source_env "$dir"
fi
_source_up "${1:-}" false
}

# Usage: source_up_if_exists [<filename>]
#
# Loads another ".envrc" if found with the find_up command. If one is not
# found, nothing happens.
#
# NOTE: the other ".envrc" is not checked by the security framework.
source_up_if_exists() {
_source_up "${1:-}" true
}

# Usage: fetchurl <url> [<integrity-hash>]
Expand Down