Last active
December 4, 2021 00:50
-
-
Save kakarukeys/c513a5af9b125cf60a89aeded79b58ef to your computer and use it in GitHub Desktop.
shell script to enable Poetry .env loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function poetry() { | |
dot_env_path=$(pwd) | |
while [[ "$dot_env_path" != "" && ! -e "$dot_env_path/.env" ]]; do | |
dot_env_path=${dot_env_path%/*} | |
done | |
# if POETRY_DONT_LOAD_ENV is *not* set, then load .env if it exists | |
if [[ -z "$POETRY_DONT_LOAD_ENV" && -f "$dot_env_path/.env" ]]; then | |
>&2 echo 'Loading .env environment variables…' | |
export $(grep -v '^#' "$dot_env_path/.env" | tr -d ' ' | xargs) | |
command poetry "$@" | |
if [[ -f "$dot_env_path/.env" ]]; then | |
unset $(grep -v '^#' "$dot_env_path/.env" | sed -E 's/([^=]*)=.*/\1/' | xargs) | |
fi | |
else | |
command poetry "$@" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment