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
Changes from 1 commit
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
28 changes: 23 additions & 5 deletions stdlib.sh
Expand Up @@ -418,14 +418,32 @@ watch_dir() {

# 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"
local envrc file=${1:-.envrc}
envrc=$(cd .. && (find_up "$file" || true))
if [[ -n $envrc ]]; then
source_env "$envrc"
else
log_error "No ancestor $file found"
exit 1
jfly marked this conversation as resolved.
Show resolved Hide resolved
fi
}

# 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() {
local envrc file=${1:-.envrc}
envrc=$(cd .. && (find_up "$file" || true))
if [[ -n $envrc ]]; then
source_env "$envrc"
fi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
local envrc file=${1:-.envrc}
envrc=$(cd .. && (find_up "$file" || true))
if [[ -n $envrc ]]; then
source_env "$envrc"
fi
source_up "${1:-}" || true

With the above change it becomes possible to deduplicate the logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works, but with my change to source_up, it means you get a red "No ancestor .envrc found" message even though this function still succeeds.

I've tweaked things around a little bit to try to DRY this up. I'm honestly not sure if it's better or worse. What do you think, @zimbatm?

}

Expand Down