autoenv refactor

This commit is contained in:
thek4n 2024-12-17 09:49:51 +03:00
parent c4a7194249
commit 695522d088

View File

@ -1,26 +1,30 @@
# vim: ft=zsh # 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() { _autoenv() {
if [ -z "${PWD}" ]; then if ! _dir_authorized "${PWD}"; then
return return
fi fi
if ! grep "${PWD}" ~/.autoenv_authorized_dirs &>/dev/null; then if [ -f ./.env ]; then
return
fi
if [ -f .env ]; then
source ./.env source ./.env
fi fi
} }
_autoenv_leave() { _autoenv_leave() {
if [ -z "${OLDPWD}" ]; then if ! _dir_authorized "${OLDPWD}"; then
return
fi
if ! grep "${OLDPWD}" ~/.autoenv_authorized_dirs &>/dev/null; then
return return
fi fi
@ -29,14 +33,17 @@ _autoenv_leave() {
fi fi
} }
alias authorize_dir='echo "\n${PWD}" >> "${HOME}/.autoenv_authorized_dirs"'
autoload -U add-zsh-hook autoload -U add-zsh-hook
add-zsh-hook chpwd _autoenv add-zsh-hook chpwd _autoenv
add-zsh-hook chpwd _autoenv_leave add-zsh-hook chpwd _autoenv_leave
if [ -f .env ]; then if [ -f ./.env ]; then
if grep "${PWD}" ~/.autoenv_authorized_dirs &>/dev/null; then if _dir_authorized "${PWD}"; then
. ./.env source ./.env
fi fi
fi fi