]> Sergey Matveev's repositories - zsh-autoenv.git/blob - autoenv.zsh
s/ENV_AUTHORIZATION_FILE/AUTOENV_ENV_FILENAME/
[zsh-autoenv.git] / autoenv.zsh
1 # Initially based on
2 # https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
3
4 export AUTOENV_ENV_FILENAME=$HOME/.env_auth
5
6 # Name of file to look for when entering directories.
7 : ${DOTENV_FILE_ENTER:=.env}
8
9 # Name of file to look for when leaving directories.
10 # Requires DOTENV_HANDLE_LEAVE=1.
11 : ${DOTENV_FILE_LEAVE:=.env.leave}
12
13 # Look for .env in parent dirs?
14 : ${DOTENV_LOOK_UPWARDS:=1}
15
16 # Handle leave events when changing away from a subtree, where an "enter"
17 # event was handled?
18 : ${DOTENV_HANDLE_LEAVE:=1}
19
20
21 # Internal: stack of entered (and handled) directories.
22 _dotenv_stack_entered=()
23
24
25 _dotenv_hash_pair() {
26   local env_file=$1
27   echo "$env_file:$env_shasum:1"
28 }
29
30 _dotenv_authorized_env_file() {
31   local env_file=$1
32   local pair=$(_dotenv_hash_pair $env_file)
33   test -f $AUTOENV_ENV_FILENAME \
34     && \grep -qF $pair $AUTOENV_ENV_FILENAME
35 }
36
37 _dotenv_authorize() {
38   local env_file=$1
39   _dotenv_deauthorize $env_file
40   _dotenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
41 }
42
43 _dotenv_deauthorize() {
44   local env_file=$1
45   if [[ -f $AUTOENV_ENV_FILENAME ]]; then
46     echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
47   fi
48 }
49
50 # This function can be mocked in tests
51 _dotenv_read_answer() {
52   local answer
53   read $=_AUTOENV_TEST_READ_ARGS -q answer
54   echo $answer
55 }
56
57 # Args: 1: absolute path to env file (resolved symlinks).
58 _dotenv_check_authorized_env_file() {
59   if ! [[ -f $1 ]]; then
60     return 1
61   fi
62   if ! _dotenv_authorized_env_file $1; then
63     echo "Attempting to load unauthorized env file: $1"
64     echo ""
65     echo "**********************************************"
66     echo ""
67     cat $1
68     echo ""
69     echo "**********************************************"
70     echo ""
71     echo -n "Would you like to authorize it? [y/N] "
72
73     local answer=$(_dotenv_read_answer)
74     echo
75     if [[ $answer != 'y' ]]; then
76       return 1
77     fi
78
79     _dotenv_authorize $1
80   fi
81   return 0
82 }
83
84 # Initialize $_dotenv_sourced_varstash, but do not overwrite an existing one
85 # from e.g. `exec zsh` (to reload your shell config).
86 : ${_dotenv_sourced_varstash:=0}
87
88 # Get directory of this file (absolute, with resolved symlinks).
89 _dotenv_this_dir=${0:A:h}
90
91 _dotenv_source() {
92   local env_file=$1
93   _dotenv_event=$2
94   _dotenv_cwd=$3
95
96   # Source varstash library once.
97   if [[ $_dotenv_sourced_varstash == 0 ]]; then
98     source $_dotenv_this_dir/lib/varstash
99     export _dotenv_sourced_varstash=1
100     # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
101     # ${env_file:h}.
102   fi
103
104   # Change to directory of env file, source it and cd back.
105   local new_dir=$PWD
106   builtin cd -q $_dotenv_cwd
107   source $env_file
108   builtin cd -q $new_dir
109
110   unset _dotenv_event _dotenv_cwd
111 }
112
113 _dotenv_chpwd_handler() {
114   local env_file="$PWD/$DOTENV_FILE_ENTER"
115
116   # Handle leave event for previously sourced env files.
117   if [[ $DOTENV_HANDLE_LEAVE == 1 ]] && (( $#_dotenv_stack_entered )); then
118     for prev_dir in ${_dotenv_stack_entered}; do
119       if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
120         local env_file_leave=$prev_dir/$DOTENV_FILE_LEAVE
121         if _dotenv_check_authorized_env_file $env_file_leave; then
122           _dotenv_source $env_file_leave leave $prev_dir
123         fi
124         # Remove this entry from the stack.
125         _dotenv_stack_entered=(${_dotenv_stack_entered#$prev_dir})
126       fi
127     done
128   fi
129
130   if ! [[ -f $env_file ]] && [[ $DOTENV_LOOK_UPWARDS == 1 ]]; then
131     # Look for files in parent dirs, using an extended Zsh glob.
132     setopt localoptions extendedglob
133     local m
134     m=((../)#${DOTENV_FILE_ENTER}(N))
135     if (( $#m )); then
136       env_file=${${m[1]}:A}
137     else
138       return
139     fi
140   fi
141
142   if ! _dotenv_check_authorized_env_file $env_file; then
143     return
144   fi
145
146   # Load the env file only once: check if $env_file's parent
147   # is in $_dotenv_stack_entered.
148   local env_file_dir=${env_file:A:h}
149   if (( ${+_dotenv_stack_entered[(r)${env_file_dir}]} )); then
150     return
151   fi
152
153   _dotenv_stack_entered+=(${env_file_dir})
154
155   _dotenv_source $env_file enter $PWD
156 }
157
158 autoload -U add-zsh-hook
159 add-zsh-hook chpwd _dotenv_chpwd_handler
160
161 # Look in current directory already.
162 _dotenv_chpwd_handler