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

dotenv plugin: agree once improvement #8729

Merged
merged 8 commits into from
Mar 23, 2020
35 changes: 26 additions & 9 deletions plugins/dotenv/dotenv.plugin.zsh
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
source_env() {
if [[ -f $ZSH_DOTENV_FILE ]]; then
if [ "$ZSH_DOTENV_PROMPT" != "false" ]; then
# confirm before sourcing file
local confirmation
# print same-line prompt and output newline character if necessary
echo -n "dotenv: source '$ZSH_DOTENV_FILE' file in the directory? (Y/n) "
read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
# only bail out if confirmation character is n
if [[ "$confirmation" = [nN] ]]; then
return
# agree list
touch "${ZSH_DOTENV_AGREE_LIST_FILE}"
local dotenv_absolutepath="$(readlink -f $ZSH_DOTENV_FILE)"
Jblew marked this conversation as resolved.
Show resolved Hide resolved
local agree_list="${(@f)"$(<${ZSH_DOTENV_AGREE_LIST_FILE})"}"
[[ ${agree_list[(ie)$]} -le ${#agree_list} ]]; local alreadyagreed=$?

if ! [ "$alreadyagreed" = "1" ]; then
# confirm before sourcing file
local confirmation
# print same-line prompt and output newline character if necessary
echo -n "dotenv: source '$ZSH_DOTENV_FILE' file in the directory? ([Y]es/[a]lways/[n]n) "
read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
# only bail out if confirmation character is n
if [[ "$confirmation" = [nN] ]]; then
return
fi

if [[ "$confirmation" = [aA] ]]; then
echo "${dotenv_absolutepath}" >> "${ZSH_DOTENV_AGREE_LIST_FILE}"
fi
fi
fi

Expand All @@ -29,7 +41,12 @@ autoload -U add-zsh-hook
add-zsh-hook chpwd source_env

if [[ -z $ZSH_DOTENV_FILE ]]; then
ZSH_DOTENV_FILE=.env
ZSH_DOTENV_FILE=.env
fi

if [[ -z $ZSH_DOTENV_AGREE_LIST_FILE ]]; then
ZSH_DOTENV_AGREE_LIST_FILE="${ZSH}/dotenv-agree.list"
fi

source_env