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

Enable recursive .env sourcing #11920

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions plugins/dotenv/README.md
Expand Up @@ -78,6 +78,15 @@ change.
NOTE: if a directory is found in both the allowed and disallowed lists, the disallowed list
takes preference, _i.e._ the .env file will never be sourced.

### ZSH_DOTENV_RECURSIVE, ZSH_DOTENV_ROOT
Set `ZSH_DOTENV_RECURSIVE=true` in your zshrc file to enable recursive search for the `ZSH_DOTENV_FILE` up to
`ZSH_DOTENV_ROOT` (default `$HOME`) and source from `$ZSH_DOTENV_ROOT` to `$PWD`.

```zsh
# in ~/.zshrc, before Oh My Zsh is sourced:
ZSH_DOTENV_RECURSIVE=true
ZSH_DOTENV_ROOT=/path/to/root
```
## Version Control

**It's strongly recommended to add `.env` file to `.gitignore`**, because usually it contains sensitive information such as your credentials, secret keys, passwords etc. You don't want to commit this file, it's supposed to be local only.
Expand Down
36 changes: 28 additions & 8 deletions plugins/dotenv/dotenv.plugin.zsh
Expand Up @@ -7,11 +7,13 @@
: ${ZSH_DOTENV_ALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-allowed.list"}
: ${ZSH_DOTENV_DISALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-disallowed.list"}


: ${ZSH_DOTENV_ROOT:=$HOME}
: ${ZSH_DOTENV_RECURSIVE:=false}
## Functions

source_env() {
if [[ ! -f "$ZSH_DOTENV_FILE" ]]; then
base_dir=${1:=.}
if [[ ! -f "$base_dir/$ZSH_DOTENV_FILE" ]]; then
return
fi

Expand All @@ -37,7 +39,7 @@ source_env() {
[[ $column -eq 1 ]] || echo

# print same-line prompt and output newline character if necessary
echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
echo -n "dotenv: found '$base_dir/$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
read -k 1 confirmation
[[ "$confirmation" = $'\n' ]] || echo

Expand All @@ -52,16 +54,34 @@ source_env() {
fi

# test .env syntax
zsh -fn $ZSH_DOTENV_FILE || {
echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
zsh -fn $base_dir/$ZSH_DOTENV_FILE || {
echo "dotenv: error when sourcing '$base_dir/$ZSH_DOTENV_FILE' file" >&2
return 1
}

setopt localoptions allexport
source $ZSH_DOTENV_FILE
source $base_dir/$ZSH_DOTENV_FILE
}

source_recursive_env() {
curr_dir=$PWD
paths_to_load=()
while [[ "$curr_dir" = "$HOME"* ]]; do
if [[ -f "$curr_dir/$ZSH_DOTENV_FILE" ]]; then
paths_to_load+=("$curr_dir")
fi
curr_dir=${curr_dir%/*}
done
for path_to_load in "${paths_to_load[@]}"; do
source_env "$path_to_load"
done
}
autoload -U add-zsh-hook
add-zsh-hook chpwd source_env

source_env
if [[ "$ZSH_DOTENV_RECURSIVE" = true ]]; then
add-zsh-hook chpwd source_recursive_env
source_recursive_env
else
add-zsh-hook chpwd source_env
source_env
fi