From 4ea4da352f31ff1854569e9069254bf81cf069b0 Mon Sep 17 00:00:00 2001 From: thek4n Date: Wed, 18 Dec 2024 15:07:56 +0300 Subject: [PATCH] autoenv refactor --- home/user/.config/zsh/autoenv | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/home/user/.config/zsh/autoenv b/home/user/.config/zsh/autoenv index b1d580e..a3af17f 100644 --- a/home/user/.config/zsh/autoenv +++ b/home/user/.config/zsh/autoenv @@ -31,24 +31,26 @@ _autoenv_envfile_authorize() { return 1 } -_autoenv() { - if ! _autoenv_envfile_authorize "${PWD}/${AUTOENV_FILE_ENTER}"; then - return +_autoenv_source_if_authorized() { + local file_to_source="${1}" + + if [ ! -s "${file_to_source}" ]; then + return 1 fi - if [ -f "${PWD}/${AUTOENV_FILE_ENTER}" ]; then - source "${PWD}/${AUTOENV_FILE_ENTER}" + if ! _autoenv_envfile_authorize "${file_to_source}"; then + return 1 fi + + source "${file_to_source}" +} + +_autoenv() { + _autoenv_source_if_authorized "${PWD}/${AUTOENV_FILE_ENTER}" } _autoenv_leave() { - if ! _autoenv_envfile_authorize "${OLDPWD}/${AUTOENV_FILE_LEAVE}"; then - return - fi - - if [ -f "${OLDPWD}/${AUTOENV_FILE_LEAVE}" ]; then - source "${OLDPWD}/${AUTOENV_FILE_LEAVE}" - fi + _autoenv_source_if_authorized "${OLDPWD}/${AUTOENV_FILE_LEAVE}" } if [[ "${AUTOENV_DISABLED}" != 1 ]]; then @@ -59,12 +61,7 @@ if [[ "${AUTOENV_DISABLED}" != 1 ]]; then add-zsh-hook chpwd _autoenv_leave fi - if [ -f "${PWD}/${AUTOENV_FILE_ENTER}" ]; then - if _autoenv_envfile_authorize "${PWD}/${AUTOENV_FILE_ENTER}"; then - source "${PWD}/${AUTOENV_FILE_ENTER}" - fi - fi - + _autoenv_source_if_authorized "${PWD}/${AUTOENV_FILE_ENTER}" fi