2024-12-17 09:49:51 +03:00

49 lines
781 B
Bash

# vim: ft=zsh
_dir_authorized() {
if [ -z "${1}" ]; then
return 1
fi
if [ ! -d "${1}" ]; then
return 1
fi
grep "^${1}$" "${HOME}/.autoenv_authorized_dirs" &>/dev/null
}
_autoenv() {
if ! _dir_authorized "${PWD}"; then
return
fi
if [ -f ./.env ]; then
source ./.env
fi
}
_autoenv_leave() {
if ! _dir_authorized "${OLDPWD}"; then
return
fi
if [ -f "${OLDPWD}/.env.leave" ]; then
source "${OLDPWD}/.env.leave"
fi
}
alias authorize_dir='echo "\n${PWD}" >> "${HOME}/.autoenv_authorized_dirs"'
autoload -U add-zsh-hook
add-zsh-hook chpwd _autoenv
add-zsh-hook chpwd _autoenv_leave
if [ -f ./.env ]; then
if _dir_authorized "${PWD}"; then
source ./.env
fi
fi