Skip to content

Commit

Permalink
source_up_if_exists: A strict_env compatible version of source_up (#921)
Browse files Browse the repository at this point in the history
This fixes #913

While I was in here, I opted to make the existing `source_up` script log
explicitly when it can't find a file. I think that's useful both with
strict_env enabled and disabled.

Co-authored-by: zimbatm <zimbatm@zimbatm.com>
  • Loading branch information
jfly and zimbatm committed Apr 14, 2022
1 parent e168a1d commit c965ba0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
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

0 comments on commit c965ba0

Please sign in to comment.