]> Sergey Matveev's repositories - zsh-autoenv.git/blob - autoenv.zsh
Some more refactoring, adding support for `autoenv_source_parent`
[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 : ${AUTOENV_FILE_ENTER:=.env}
8
9 # Name of file to look for when leaving directories.
10 # Requires AUTOENV_HANDLE_LEAVE=1.
11 : ${AUTOENV_FILE_LEAVE:=.env.leave}
12
13 # Look for .env in parent dirs?
14 : ${AUTOENV_LOOK_UPWARDS:=1}
15
16 # Handle leave events when changing away from a subtree, where an "enter"
17 # event was handled?
18 : ${AUTOENV_HANDLE_LEAVE:=1}
19
20
21 # Public helper functions, which can be used from your .env files:
22 #
23 # Source the next .env file from parent directories.
24 # This is useful if you want to use a base .env file for a directory subtree.
25 autoenv_source_parent() {
26   local parent_env_file=$(_autoenv_get_file_upwards $PWD)
27
28   if [[ -n $parent_env_file ]] \
29     && _autoenv_check_authorized_env_file $parent_env_file; then
30
31     local parent_env_dir=${parent_env_file:A:h}
32     _autoenv_source $parent_env_file enter $parent_env_dir
33   fi
34 }
35
36
37 # Internal: stack of entered (and handled) directories. {{{
38 _autoenv_stack_entered=()
39 typeset -A _autoenv_stack_entered_mtime
40 _autoenv_stack_entered_mtime=()
41
42 # Add an entry to the stack, and remember its mtime.
43 _autoenv_stack_entered_add() {
44   local env_file=$1
45
46   # Remove any existing entry.
47   _autoenv_stack_entered[$_autoenv_stack_entered[(i)$1]]=()
48
49   # Append it to the stack, and remember its mtime.
50   _autoenv_stack_entered+=($env_file)
51   _autoenv_stack_entered_mtime[$env_file]=$(_autoenv_get_file_mtime $env_file)
52 }
53
54 _autoenv_get_file_mtime() {
55   if [[ -f $1 ]]; then
56     zstat +mtime $1
57   else
58     echo 0
59   fi
60 }
61
62 # Remove an entry from the stack.
63 _autoenv_stack_entered_remove() {
64   local env_file=$1
65   _autoenv_stack_entered=(${_autoenv_stack_entered#$env_file})
66   _autoenv_stack_entered_mtime[$env_file]=
67 }
68
69 # Is the given entry already in the stack?
70 _autoenv_stack_entered_contains() {
71   local env_file=$1
72   if (( ${+_autoenv_stack_entered[(r)${env_file}]} )); then
73     # Entry is in stack.
74     if [[ $_autoenv_stack_entered_mtime[$env_file] == $(_autoenv_get_file_mtime $env_file) ]]; then
75       # Entry has the expected mtime.
76       return
77     fi
78   fi
79   return 1
80 }
81 # }}}
82
83 # Load zstat module, but only its builtin `zstat`.
84 zmodload -F zsh/stat b:zstat
85
86
87 _autoenv_hash_pair() {
88   local env_file=${1:A}
89   if (( $+2 )); then
90     env_shasum=$2
91   else
92     env_shasum=$(shasum $env_file | cut -d' ' -f1)
93   fi
94   echo "$env_file:$env_shasum:1"
95 }
96
97 _autoenv_authorized_env_file() {
98   local env_file=$1
99   local pair=$(_autoenv_hash_pair $env_file)
100   test -f $AUTOENV_ENV_FILENAME \
101     && \grep -qF $pair $AUTOENV_ENV_FILENAME
102 }
103
104 _autoenv_authorize() {
105   local env_file=$1
106   _autoenv_deauthorize $env_file
107   _autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
108 }
109
110 _autoenv_deauthorize() {
111   local env_file=$1
112   if [[ -f $AUTOENV_ENV_FILENAME ]]; then
113     echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
114   fi
115 }
116
117 # This function can be mocked in tests
118 _autoenv_read_answer() {
119   local answer
120   read $=_AUTOENV_TEST_READ_ARGS -q answer
121   echo $answer
122 }
123
124 # Args: 1: absolute path to env file (resolved symlinks).
125 _autoenv_check_authorized_env_file() {
126   if ! [[ -f $1 ]]; then
127     return 1
128   fi
129   if ! _autoenv_authorized_env_file $1; then
130     echo "Attempting to load unauthorized env file: $1"
131     echo ""
132     echo "**********************************************"
133     echo ""
134     cat $1
135     echo ""
136     echo "**********************************************"
137     echo ""
138     echo -n "Would you like to authorize it? [y/N] "
139
140     local answer=$(_autoenv_read_answer)
141     echo
142     if [[ $answer != 'y' ]]; then
143       return 1
144     fi
145
146     _autoenv_authorize $1
147   fi
148   return 0
149 }
150
151 # Get directory of this file (absolute, with resolved symlinks).
152 _autoenv_this_dir=${0:A:h}
153
154 _autoenv_source() {
155   local env_file=$1
156   _autoenv_event=$2
157   local _autoenv_envfile_dir=$3
158
159   _autoenv_from_dir=$_autoenv_chpwd_prev_dir
160   _autoenv_to_dir=$PWD
161
162   # Source varstash library once.
163   if [[ -z "$functions[(I)autostash]" ]]; then
164     source $_autoenv_this_dir/lib/varstash
165     # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
166     # ${env_file:h}.
167   fi
168
169   # Change to directory of env file, source it and cd back.
170   local new_dir=$PWD
171   builtin cd -q $_autoenv_envfile_dir
172   source $env_file
173   builtin cd -q $new_dir
174
175   # Unset vars set for enter/leave scripts.
176   # This should not get done for recursion (via autoenv_source_parent),
177   # and can be useful to have in general after autoenv was used.
178   # unset _autoenv_event _autoenv_from_dir _autoenv_to_dir
179 }
180
181 _autoenv_get_file_upwards() {
182   local look_from=${1:-$PWD}
183   local look_for=${2:-$AUTOENV_FILE_ENTER}
184   # Look for files in parent dirs, using an extended Zsh glob.
185   setopt localoptions extendedglob
186   local m
187   # Y1: short-circuit: first match.
188   # :A: absolute path, resolving symlinks.
189   m=($look_from/(../)##${look_for}(NY1:A))
190   if (( $#m )); then
191     echo $m[1]
192   fi
193 }
194
195
196 _autoenv_chpwd_prev_dir=$PWD
197 _autoenv_chpwd_handler() {
198   local env_file="$PWD/$AUTOENV_FILE_ENTER"
199
200   # Handle leave event for previously sourced env files.
201   if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
202     local prev_file prev_dir
203     for prev_file in ${_autoenv_stack_entered}; do
204       prev_dir=${prev_file:A:h}
205       if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
206         local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
207         if _autoenv_check_authorized_env_file $env_file_leave; then
208           _autoenv_source $env_file_leave leave $prev_dir
209         fi
210         _autoenv_stack_entered_remove $prev_dir
211       fi
212     done
213   fi
214
215   if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
216     env_file=$(_autoenv_get_file_upwards $PWD)
217     if [[ -z $env_file ]]; then
218       _autoenv_chpwd_prev_dir=$PWD
219       return
220     fi
221   fi
222
223   # Load the env file only once: check if $env_file is in the stack of entered
224   # directories.
225   if _autoenv_stack_entered_contains $env_file; then
226     _autoenv_chpwd_prev_dir=$PWD
227     return
228   fi
229
230   if ! _autoenv_check_authorized_env_file $env_file; then
231     _autoenv_chpwd_prev_dir=$PWD
232     return
233   fi
234
235   _autoenv_stack_entered_add $env_file
236
237   # Source the enter env file.
238   _autoenv_source $env_file enter $PWD
239
240   _autoenv_chpwd_prev_dir=$PWD
241 }
242
243 autoload -U add-zsh-hook
244 add-zsh-hook chpwd _autoenv_chpwd_handler
245
246 # Look in current directory already.
247 _autoenv_chpwd_handler