zsh autoenv refactor

This commit is contained in:
thek4n 2024-12-17 12:01:51 +03:00
parent 695522d088
commit 1d136f1f64
2 changed files with 35 additions and 18 deletions

View File

@ -19,7 +19,6 @@ __pycache__/
*.log *.log
*.bak *.bak
*~ *~
.env.leave
# db # db
*.db *.db
@ -43,3 +42,6 @@ build/
dist/ dist/
.tns .tns
.autoenv.zsh
.autoenv_leave.zsh

View File

@ -1,16 +1,26 @@
# vim: ft=zsh # vim: ft=zsh
: ${AUTOENV_AUTH_FILE:=~/.autoenv_auth}
: ${AUTOENV_FILE_ENTER:=.autoenv.zsh}
: ${AUTOENV_FILE_LEAVE:=.autoenv_leave.zsh}
: ${AUTOENV_DISABLED:=0}
: ${AUTOENV_HANDLE_LEAVE:=1}
_dir_authorized() { _dir_authorized() {
if [ -z "${1}" ]; then local dir="${1}"
if [ -z "${dir}" ]; then
return 1 return 1
fi fi
if [ ! -d "${1}" ]; then if [ ! -d "${dir}" ]; then
return 1 return 1
fi fi
grep "^${1}$" "${HOME}/.autoenv_authorized_dirs" &>/dev/null grep "^${dir}$" "${AUTOENV_AUTH_FILE}" &>/dev/null
} }
_autoenv() { _autoenv() {
@ -18,8 +28,8 @@ _autoenv() {
return return
fi fi
if [ -f ./.env ]; then if [ -f "${PWD}/${AUTOENV_FILE_ENTER}" ]; then
source ./.env source "${PWD}/${AUTOENV_FILE_ENTER}"
fi fi
} }
@ -28,22 +38,27 @@ _autoenv_leave() {
return return
fi fi
if [ -f "${OLDPWD}/.env.leave" ]; then if [ -f "${OLDPWD}/${AUTOENV_FILE_LEAVE}" ]; then
source "${OLDPWD}/.env.leave" source "${OLDPWD}/${AUTOENV_FILE_LEAVE}"
fi fi
} }
alias authorize_dir='echo "\n${PWD}" >> "${HOME}/.autoenv_authorized_dirs"' alias autoenv-auth='echo "\n${PWD}" >> "${AUTOENV_AUTH_FILE}"'
autoload -U add-zsh-hook
add-zsh-hook chpwd _autoenv
add-zsh-hook chpwd _autoenv_leave
if [ -f ./.env ]; then if [[ "${AUTOENV_DISABLED}" != 1 ]]; then
if _dir_authorized "${PWD}"; then autoload -U add-zsh-hook
source ./.env add-zsh-hook chpwd _autoenv
if [[ "${AUTOENV_HANDLE_LEAVE}" == 1 ]]; then
add-zsh-hook chpwd _autoenv_leave
fi fi
if [ -f "${PWD}/${AUTOENV_FILE_ENTER}" ]; then
if _dir_authorized "${PWD}"; then
source "${PWD}/${AUTOENV_FILE_ENTER}"
fi
fi
fi fi