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
Prev Previous commit
Next Next commit
Use grep and clean up allowed list check logic
  • Loading branch information
mcornella committed Mar 22, 2020
commit 5eb248d4a2916036259a25051c263894cfd4ca72
35 changes: 16 additions & 19 deletions plugins/dotenv/dotenv.plugin.zsh
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
source_env() {
if [[ -f $ZSH_DOTENV_FILE ]]; then
if [ "$ZSH_DOTENV_PROMPT" != "false" ]; then
# agree list
touch "${ZSH_DOTENV_AGREE_LIST_FILE}"
local dotenv_absolutepath="${ZSH_DOTENV_FILE:A}"
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
if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
local confirmation dirpath="$PWD"

# make sure there is an allowed file
touch "$ZSH_DOTENV_ALLOWED_LIST"

# check if current directory's .env file is allowed or ask for confirmation
if ! grep -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
# 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) "
echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways) "
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

# check input
case "$confirmation" in
[nN]) return ;;
[aA]) echo "$dirpath" >> "$ZSH_DOTENV_ALLOWED_LIST" ;;
*) ;; # interpret anything else as a yes
esac
fi
fi

Expand Down