3 # Filename of the dotenv file to look for
4 : ${ZSH_DOTENV_FILE:=.env}
6 # Path to the file containing allowed paths
7 : ${ZSH_DOTENV_ALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-allowed.list"}
8 : ${ZSH_DOTENV_DISALLOWED_LIST:="${ZSH_CACHE_DIR:-$ZSH/cache}/dotenv-disallowed.list"}
14 if [[ ! -f "$ZSH_DOTENV_FILE" ]]; then
18 if [[ "$ZSH_DOTENV_PROMPT" != false ]]; then
19 local confirmation dirpath="${PWD:A}"
21 # make sure there is an (dis-)allowed file
22 touch "$ZSH_DOTENV_ALLOWED_LIST"
23 touch "$ZSH_DOTENV_DISALLOWED_LIST"
25 # early return if disallowed
26 if command grep -Fx -q "$dirpath" "$ZSH_DOTENV_DISALLOWED_LIST" &>/dev/null; then
30 # check if current directory's .env file is allowed or ask for confirmation
31 if ! command grep -Fx -q "$dirpath" "$ZSH_DOTENV_ALLOWED_LIST" &>/dev/null; then
32 # get cursor column and print new line before prompt if not at line beginning
34 echo -ne "\e[6n" > /dev/tty
35 read -t 1 -s -d R column < /dev/tty
36 column="${column##*\[*;}"
37 [[ $column -eq 1 ]] || echo
39 # print same-line prompt and output newline character if necessary
40 echo -n "dotenv: found '$ZSH_DOTENV_FILE' file. Source it? ([Y]es/[n]o/[a]lways/n[e]ver) "
41 read -k 1 confirmation
42 [[ "$confirmation" = $'\n' ]] || echo
45 case "$confirmation" in
47 [aA]) echo "$dirpath" >> "$ZSH_DOTENV_ALLOWED_LIST" ;;
48 [eE]) echo "$dirpath" >> "$ZSH_DOTENV_DISALLOWED_LIST"; return ;;
49 *) ;; # interpret anything else as a yes
55 zsh -fn $ZSH_DOTENV_FILE || {
56 echo "dotenv: error when sourcing '$ZSH_DOTENV_FILE' file" >&2
60 setopt localoptions allexport
61 source $ZSH_DOTENV_FILE
64 autoload -U add-zsh-hook
65 add-zsh-hook chpwd source_env