6fbfc4c78bc00f1bb6f583c250f4f89509782957

Author
Jędrzej Lewandowski <jedrzejblew@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

dotenv: add agree-once improvement to confirmation prompt (#8729)

* dotenv: add possibility to agree once for a given .env file

* refactor: fix code style

* Use :A modifier instead of readlink. Thanks Aloxaf

* Use grep and clean up allowed list check logic

* Simplify and reorder file; change default allowed list path

* Add new feature to README

* Make sure ZSH_CACHE_DIR is defined

* Resolve symlinks in $PWD before storing in allowed list

Co-authored-by: Aloxaf <bailong104@gmail.com>
Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

This diff is truncated to protect this page.

 1diff --git a/plugins/dotenv/README.md b/plugins/dotenv/README.md
 2index dbc02bf61b58fd7baf9e2298f40340a33afee809..f6612473123b9715b017f3feae7672173ed7df52 100644
 3--- a/plugins/dotenv/README.md
 4+++ b/plugins/dotenv/README.md
 5@@ -4,9 +4,7 @@ Automatically load your project ENV variables from `.env` file when you `cd` int
 6 
 7diff --git a/plugins/dotenv/dotenv.plugin.zsh b/plugins/dotenv/dotenv.plugin.zsh
 8index 54036bee383d794e621fd733f622fdcc1a288192..ac3210d7fdd7454ceb5a9bab365362a1393834c6 100644
 9--- a/plugins/dotenv/dotenv.plugin.zsh
10+++ b/plugins/dotenv/dotenv.plugin.zsh
11@@ -1,35 +1,46 @@
12+## Settings
13+
14+# Filename of the dotenv file to look for
15+: ${ZSH_DOTENV_FILE:=.env}
16+
17+# Path to the file containing allowed paths
18+: ${ZSH_DOTENV_ALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-allowed.list"}
19+
20+
21+## Functions
22+
23 source_env() {
24   if [[ -f $ZSH_DOTENV_FILE ]]; then
25-    if [ "$ZSH_DOTENV_PROMPT" != "false" ]; then
26-      # confirm before sourcing file
27-      local confirmation
28-      # print same-line prompt and output newline character if necessary
29-      echo -n "dotenv: source '$ZSH_DOTENV_FILE' file in the directory? (Y/n) "
30-      read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
31-      # only bail out if confirmation character is n
32-      if [[ "$confirmation" = [nN] ]]; then
33-        return
34+    if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
35+      local confirmation dirpath="${PWD:A}"
36+
37+      # make sure there is an allowed file
38+      touch "$ZSH_DOTENV_ALLOWED_LIST"
39+
40+      # check if current directory's .env file is allowed or ask for confirmation
41+      if ! grep -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
42+        # print same-line prompt and output newline character if necessary
43+        echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways) "
44+        read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo
45+
46+        # check input
47+        case "$confirmation" in
48+          [nN]) return ;;
49+          [aA]) echo "$dirpath" >> "$ZSH_DOTENV_ALLOWED_LIST" ;;
50+          *) ;; # interpret anything else as a yes
51+        esac
52       fi
53     fi
54 
55     # test .env syntax
56     zsh -fn $ZSH_DOTENV_FILE || echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
57 
58-    if [[ -o a ]]; then
59-      source $ZSH_DOTENV_FILE
60-    else
61-      set -a
62-      source $ZSH_DOTENV_FILE
63-      set +a
64-    fi
65+    setopt localoptions allexport
66+    source $ZSH_DOTENV_FILE
67   fi
68 }
69 
70 autoload -U add-zsh-hook
71 add-zsh-hook chpwd source_env
72 
73-if [[ -z $ZSH_DOTENV_FILE ]]; then
74-    ZSH_DOTENV_FILE=.env
75-fi
76-
77 source_env