2 # https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
4 export AUTOENV_ENV_FILENAME=$HOME/.env_auth
6 # Name of file to look for when entering directories.
7 : ${DOTENV_FILE_ENTER:=.env}
9 # Name of file to look for when leaving directories.
10 # Requires DOTENV_HANDLE_LEAVE=1.
11 : ${DOTENV_FILE_LEAVE:=.env.leave}
13 # Look for .env in parent dirs?
14 : ${DOTENV_LOOK_UPWARDS:=1}
16 # Handle leave events when changing away from a subtree, where an "enter"
18 : ${DOTENV_HANDLE_LEAVE:=1}
21 # Internal: stack of entered (and handled) directories.
22 _dotenv_stack_entered=()
30 env_shasum=$(shasum $env_file | cut -d' ' -f1)
32 echo "$env_file:$env_shasum:1"
35 _dotenv_authorized_env_file() {
37 local pair=$(_dotenv_hash_pair $env_file)
38 test -f $AUTOENV_ENV_FILENAME \
39 && \grep -qF $pair $AUTOENV_ENV_FILENAME
44 _dotenv_deauthorize $env_file
45 _dotenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
48 _dotenv_deauthorize() {
50 if [[ -f $AUTOENV_ENV_FILENAME ]]; then
51 echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
55 # This function can be mocked in tests
56 _dotenv_read_answer() {
58 read $=_AUTOENV_TEST_READ_ARGS -q answer
62 # Args: 1: absolute path to env file (resolved symlinks).
63 _dotenv_check_authorized_env_file() {
64 if ! [[ -f $1 ]]; then
67 if ! _dotenv_authorized_env_file $1; then
68 echo "Attempting to load unauthorized env file: $1"
70 echo "**********************************************"
74 echo "**********************************************"
76 echo -n "Would you like to authorize it? [y/N] "
78 local answer=$(_dotenv_read_answer)
80 if [[ $answer != 'y' ]]; then
89 # Initialize $_dotenv_sourced_varstash, but do not overwrite an existing one
90 # from e.g. `exec zsh` (to reload your shell config).
91 : ${_dotenv_sourced_varstash:=0}
93 # Get directory of this file (absolute, with resolved symlinks).
94 _dotenv_this_dir=${0:A:h}
101 # Source varstash library once.
102 if [[ $_dotenv_sourced_varstash == 0 ]]; then
103 source $_dotenv_this_dir/lib/varstash
104 export _dotenv_sourced_varstash=1
105 # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
109 # Change to directory of env file, source it and cd back.
111 builtin cd -q $_dotenv_cwd
113 builtin cd -q $new_dir
115 unset _dotenv_event _dotenv_cwd
118 _dotenv_chpwd_handler() {
119 local env_file="$PWD/$DOTENV_FILE_ENTER"
121 # Handle leave event for previously sourced env files.
122 if [[ $DOTENV_HANDLE_LEAVE == 1 ]] && (( $#_dotenv_stack_entered )); then
123 for prev_dir in ${_dotenv_stack_entered}; do
124 if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
125 local env_file_leave=$prev_dir/$DOTENV_FILE_LEAVE
126 if _dotenv_check_authorized_env_file $env_file_leave; then
127 _dotenv_source $env_file_leave leave $prev_dir
129 # Remove this entry from the stack.
130 _dotenv_stack_entered=(${_dotenv_stack_entered#$prev_dir})
135 if ! [[ -f $env_file ]] && [[ $DOTENV_LOOK_UPWARDS == 1 ]]; then
136 # Look for files in parent dirs, using an extended Zsh glob.
137 setopt localoptions extendedglob
139 m=((../)#${DOTENV_FILE_ENTER}(N))
141 env_file=${${m[1]}:A}
147 if ! _dotenv_check_authorized_env_file $env_file; then
151 # Load the env file only once: check if $env_file's parent
152 # is in $_dotenv_stack_entered.
153 local env_file_dir=${env_file:A:h}
154 if (( ${+_dotenv_stack_entered[(r)${env_file_dir}]} )); then
158 _dotenv_stack_entered+=(${env_file_dir})
160 _dotenv_source $env_file enter $PWD
163 autoload -U add-zsh-hook
164 add-zsh-hook chpwd _dotenv_chpwd_handler
166 # Look in current directory already.
167 _dotenv_chpwd_handler