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: add never option to confirmation prompt #9102

Merged
merged 3 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add never option no confirmation prompt
  • Loading branch information
jakobhellermann committed Jul 11, 2020
commit 28a8f69b61d9a2a39a2729656b2d7ed031270fad
19 changes: 10 additions & 9 deletions plugins/dotenv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,25 @@ Set `ZSH_DOTENV_PROMPT=false` in your zshrc file if you don't want the confirmat
You can also choose the `Always` option when prompted to always allow sourcing the .env file
in that directory. See the next section for more details.

### ZSH_DOTENV_ALLOWED_LIST
### ZSH_DOTENV_{DIS,}ALLOWED_LIST

The default behavior of the plugin is to always ask whether to source a dotenv file. There's
a **Y**es, **N**o, and **A**lways option. If you choose Always, the directory of the .env file
will be added to an allowed list. If a directory is found in this list, the plugin won't ask
for confirmation and will instead source the .env file directly.
a **Y**es, **N**o, **A**lways and N**e**ver option. If you choose Always, the directory of the .env file
will be added to an allowed list, for Never it get's added to a disallowed list respectively. If a directory is found in those lists, the plugin won't ask
for confirmation and will instead source the .env file or proceed without action directly.

This allowed list is saved by default in `$ZSH_CACHE_DIR/dotenv-allowed.list`. If you want
to change that location, change the `$ZSH_DOTENV_ALLOWED_LIST` variable, like so:
This (dis-)allowed list is saved by default in `$ZSH_CACHE_DIR/dotenv-{dis,}allowed.list`. If you want
to change that location, change the `$ZSH_DOTENV_{DIS,}ALLOWED_LIST` variable, like so:

```zsh
# in ~/.zshrc, before Oh My Zsh is sourced:
ZSH_DOTENV_ALLOWED_LIST=/path/to/dotenv/allowed/list
ZSH_DOTENV_DISALLOWED_LIST=/path/to/dotenv/disallowed/list
```

This file is just a list of directories allowed, separated by a newline character. If you want
to disallow a directory, just edit this file and remove the line for the directory you want to
disallow.
The file is just a list of directories, separated by a newline character. If you want
to change your decistion, just edit the file and remove the line for the directory you want to
change.

## Version Control

Expand Down
12 changes: 10 additions & 2 deletions plugins/dotenv/dotenv.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Path to the file containing allowed paths
: ${ZSH_DOTENV_ALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-allowed.list"}
: ${ZSH_DOTENV_DISALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-disallowed.list"}


## Functions
Expand All @@ -14,19 +15,26 @@ source_env() {
if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
local confirmation dirpath="${PWD:A}"

# make sure there is an allowed file
# make sure there is an (dis-)allowed file
touch "$ZSH_DOTENV_ALLOWED_LIST"
touch "$ZSH_DOTENV_DISALLOWED_LIST"

# early return if disallowed
if grep -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then
return;
fi

# 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: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways) "
echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
read -k 1 confirmation; [[ "$confirmation" != $'\n' ]] && echo

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