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 : ${AUTOENV_FILE_ENTER:=.env}
9 # Name of file to look for when leaving directories.
10 # Requires AUTOENV_HANDLE_LEAVE=1.
11 : ${AUTOENV_FILE_LEAVE:=.env.leave}
13 # Look for .env in parent dirs?
14 : ${AUTOENV_LOOK_UPWARDS:=1}
16 # Handle leave events when changing away from a subtree, where an "enter"
18 : ${AUTOENV_HANDLE_LEAVE:=1}
20 # Enable debugging. Multiple levels are supported (max 2).
23 # Public helper functions, which can be used from your .env files:
25 # Source the next .env file from parent directories.
26 # This is useful if you want to use a base .env file for a directory subtree.
27 autoenv_source_parent() {
28 local parent_env_file=$(_autoenv_get_file_upwards $PWD)
30 if [[ -n $parent_env_file ]] \
31 && _autoenv_check_authorized_env_file $parent_env_file; then
32 _autoenv_debug "Calling autoenv_source_parent: parent_env_file:$parent_env_file"
34 local parent_env_dir=${parent_env_file:A:h}
36 _autoenv_stack_entered_add $parent_env_file
38 _autoenv_source $parent_env_file enter $parent_env_dir
42 # Internal functions. {{{
43 # Internal: stack of entered (and handled) directories. {{{
44 _autoenv_stack_entered=()
45 typeset -A _autoenv_stack_entered_mtime
46 _autoenv_stack_entered_mtime=()
48 # Add an entry to the stack, and remember its mtime.
49 _autoenv_stack_entered_add() {
52 _autoenv_debug "[stack] adding: $env_file" 2
54 # Remove any existing entry.
55 _autoenv_stack_entered_remove $env_file
57 # Append it to the stack, and remember its mtime.
58 _autoenv_stack_entered+=($env_file)
59 _autoenv_stack_entered_mtime[$env_file]=$(_autoenv_get_file_mtime $env_file)
62 _autoenv_get_file_mtime() {
70 # Remove an entry from the stack.
71 _autoenv_stack_entered_remove() {
73 _autoenv_debug "[stack] removing: $env_file" 2
74 _autoenv_stack_entered[$_autoenv_stack_entered[(i)$env_file]]=()
75 _autoenv_stack_entered_mtime[$env_file]=
78 # Is the given entry already in the stack?
79 _autoenv_stack_entered_contains() {
81 if (( ${+_autoenv_stack_entered[(r)${env_file}]} )); then
83 if [[ $_autoenv_stack_entered_mtime[$env_file] == $(_autoenv_get_file_mtime $env_file) ]]; then
84 # Entry has the expected mtime.
92 # Internal function for debug output. {{{
96 if [[ $AUTOENV_DEBUG -lt $level ]]; then
99 # Load zsh color support.
100 if [[ -z $colors ]]; then
104 # Build $indent prefix.
106 if [[ $_autoenv_debug_indent -gt 0 ]]; then
107 for i in {1..${_autoenv_debug_indent}}; do
112 # Split $msg by \n (not newline).
113 lines=(${(ps:\\n:)msg})
114 for line in $lines; do
115 echo -n "${fg_bold[blue]}[autoenv]${fg_no_bold[default]} " >&2
116 echo ${indent}${line} >&2
121 # Load zstat module, but only its builtin `zstat`.
122 zmodload -F zsh/stat b:zstat
125 # Generate hash pair for a given file ($1).
126 # A fixed hash value can be given as 2nd arg, but is used with tests only.
127 _autoenv_hash_pair() {
128 local env_file=${1:A}
133 if ! [[ -e $env_file ]]; then
134 echo "Missing file argument for _autoenv_hash_pair!" >&2
137 env_shasum=$(shasum $env_file | cut -d' ' -f1)
139 echo "$env_file:$env_shasum:1"
142 _autoenv_authorized_env_file() {
144 local pair=$(_autoenv_hash_pair $env_file)
145 test -f $AUTOENV_ENV_FILENAME \
146 && \grep -qF $pair $AUTOENV_ENV_FILENAME
149 _autoenv_authorize() {
151 _autoenv_deauthorize $env_file
152 _autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
155 _autoenv_deauthorize() {
157 if [[ -s $AUTOENV_ENV_FILENAME ]]; then
158 echo "$(\grep -vF $env_file $AUTOENV_ENV_FILENAME)" > $AUTOENV_ENV_FILENAME
162 # This function can be mocked in tests
163 _autoenv_ask_for_yes() {
166 if [[ $answer == "yes" ]]; then
173 # Args: 1: absolute path to env file (resolved symlinks).
174 _autoenv_check_authorized_env_file() {
175 if ! [[ -f $1 ]]; then
178 if ! _autoenv_authorized_env_file $1; then
179 echo "Attempting to load unauthorized env file!"
182 echo "**********************************************"
186 echo "**********************************************"
188 echo -n "Would you like to authorize it? (type 'yes') "
190 if ! _autoenv_ask_for_yes; then
194 _autoenv_authorize $1
199 # Get directory of this file (absolute, with resolved symlinks).
200 _autoenv_source_dir=${0:A:h}
205 local _autoenv_envfile_dir=${3:-${1:A:h}}
207 autoenv_from_dir=$_autoenv_chpwd_prev_dir
210 # Source varstash library once.
211 if [[ -z "$functions[(I)autostash]" ]]; then
212 source $_autoenv_source_dir/lib/varstash
213 # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
217 # Change to directory of env file, source it and cd back.
219 builtin cd -q $_autoenv_envfile_dir
220 _autoenv_debug "== SOURCE: ${bold_color}$env_file${reset_color}\n PWD: $PWD"
221 (( _autoenv_debug_indent++ ))
223 (( _autoenv_debug_indent-- ))
224 _autoenv_debug "== END SOURCE =="
225 builtin cd -q $new_dir
227 # Unset vars set for enter/leave scripts.
228 # This should not get done for recursion (via autoenv_source_parent),
229 # and can be useful to have in general after autoenv was used.
230 # unset autoenv_event autoenv_from_dir autoenv_to_dir
233 _autoenv_get_file_upwards() {
234 local look_from=${1:-$PWD}
235 local look_for=${2:-$AUTOENV_FILE_ENTER}
237 # Manually look in parent dirs. An extended Zsh glob should use Y1 for
238 # performance reasons, which is only available in zsh-5.0.5-146-g9381bb6.
240 local parent_dir="$look_from/.."
242 parent_dir=${parent_dir:A}
243 if [[ $parent_dir == $last ]]; then
246 parent_file="${parent_dir}/${look_for}"
248 if [[ -f $parent_file ]]; then
254 parent_dir="${parent_dir}/.."
259 _autoenv_chpwd_prev_dir=$PWD
260 _autoenv_chpwd_handler() {
261 local env_file="$PWD/$AUTOENV_FILE_ENTER"
263 _autoenv_debug "Calling chpwd handler: PWD=$PWD"
265 # Handle leave event for previously sourced env files.
266 if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
267 local prev_file prev_dir
268 for prev_file in ${_autoenv_stack_entered}; do
269 prev_dir=${prev_file:A:h}
270 if ! [[ ${PWD:A}/ == ${prev_dir}/* ]]; then
271 local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
272 if _autoenv_check_authorized_env_file $env_file_leave; then
273 _autoenv_source $env_file_leave leave $prev_dir
275 _autoenv_stack_entered_remove $prev_file
280 if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
281 env_file=$(_autoenv_get_file_upwards $PWD)
282 if [[ -z $env_file ]]; then
283 _autoenv_chpwd_prev_dir=$PWD
288 # Load the env file only once: check if $env_file is in the stack of entered
290 if _autoenv_stack_entered_contains $env_file; then
291 _autoenv_debug "Already in stack: $env_file"
292 _autoenv_chpwd_prev_dir=$PWD
296 if ! _autoenv_check_authorized_env_file $env_file; then
297 _autoenv_chpwd_prev_dir=$PWD
301 _autoenv_stack_entered_add $env_file
303 # Source the enter env file.
304 _autoenv_debug "Sourcing from chpwd handler: $env_file"
305 _autoenv_source $env_file enter
307 _autoenv_chpwd_prev_dir=$PWD
309 (( _autoenv_debug_indent++ ))
313 autoload -U add-zsh-hook
314 add-zsh-hook chpwd _autoenv_chpwd_handler
316 # Look in current directory already.
317 _autoenv_chpwd_handler