]> Sergey Matveev's repositories - zsh-autoenv.git/blob - autoenv.zsh
Use AUTOENV prefix
[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 # Internal: stack of entered (and handled) directories.
22 _autoenv_stack_entered=()
23
24
25 _autoenv_hash_pair() {
26   local env_file=$1
27   if (( $+2 )); then
28     env_shasum=$2
29   else
30     env_shasum=$(shasum $env_file | cut -d' ' -f1)
31   fi
32   echo "$env_file:$env_shasum:1"
33 }
34
35 _autoenv_authorized_env_file() {
36   local env_file=$1
37   local pair=$(_autoenv_hash_pair $env_file)
38   test -f $AUTOENV_ENV_FILENAME \
39     && \grep -qF $pair $AUTOENV_ENV_FILENAME
40 }
41
42 _autoenv_authorize() {
43   local env_file=$1
44   _autoenv_deauthorize $env_file
45   _autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
46 }
47
48 _autoenv_deauthorize() {
49   local env_file=$1
50   if [[ -f $AUTOENV_ENV_FILENAME ]]; then
51     echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
52   fi
53 }
54
55 # This function can be mocked in tests
56 _autoenv_read_answer() {
57   local answer
58   read $=_AUTOENV_TEST_READ_ARGS -q answer
59   echo $answer
60 }
61
62 # Args: 1: absolute path to env file (resolved symlinks).
63 _autoenv_check_authorized_env_file() {
64   if ! [[ -f $1 ]]; then
65     return 1
66   fi
67   if ! _autoenv_authorized_env_file $1; then
68     echo "Attempting to load unauthorized env file: $1"
69     echo ""
70     echo "**********************************************"
71     echo ""
72     cat $1
73     echo ""
74     echo "**********************************************"
75     echo ""
76     echo -n "Would you like to authorize it? [y/N] "
77
78     local answer=$(_autoenv_read_answer)
79     echo
80     if [[ $answer != 'y' ]]; then
81       return 1
82     fi
83
84     _autoenv_authorize $1
85   fi
86   return 0
87 }
88
89 # Initialize $_autoenv_sourced_varstash, but do not overwrite an existing one
90 # from e.g. `exec zsh` (to reload your shell config).
91 : ${_autoenv_sourced_varstash:=0}
92
93 # Get directory of this file (absolute, with resolved symlinks).
94 _autoenv_this_dir=${0:A:h}
95
96 _autoenv_source() {
97   local env_file=$1
98   _autoenv_event=$2
99   _autoenv_envfile_dir=$3
100   _autoenv_from_dir=$_autoenv_chpwd_prev_dir
101   _autoenv_to_dir=$PWD
102
103   # Source varstash library once.
104   if [[ $_autoenv_sourced_varstash == 0 ]]; then
105     source $_autoenv_this_dir/lib/varstash
106     export _autoenv_sourced_varstash=1
107     # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
108     # ${env_file:h}.
109   fi
110
111   # Change to directory of env file, source it and cd back.
112   local new_dir=$PWD
113   builtin cd -q $_autoenv_envfile_dir
114   source $env_file
115   builtin cd -q $new_dir
116
117   unset _autoenv_event _autoenv_from_dir
118 }
119
120 _autoenv_chpwd_prev_dir=$PWD
121 _autoenv_chpwd_handler() {
122   local env_file="$PWD/$AUTOENV_FILE_ENTER"
123
124   # Handle leave event for previously sourced env files.
125   if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
126     for prev_dir in ${_autoenv_stack_entered}; do
127       if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
128         local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
129         if _autoenv_check_authorized_env_file $env_file_leave; then
130           _autoenv_source $env_file_leave leave $prev_dir
131         fi
132         # Remove this entry from the stack.
133         _autoenv_stack_entered=(${_autoenv_stack_entered#$prev_dir})
134       fi
135     done
136   fi
137
138   if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
139     # Look for files in parent dirs, using an extended Zsh glob.
140     setopt localoptions extendedglob
141     local m
142     m=((../)#${AUTOENV_FILE_ENTER}(N))
143     if (( $#m )); then
144       env_file=${${m[1]}:A}
145     else
146       _autoenv_chpwd_prev_dir=$PWD
147       return
148     fi
149   fi
150
151   if ! _autoenv_check_authorized_env_file $env_file; then
152     _autoenv_chpwd_prev_dir=$PWD
153     return
154   fi
155
156   # Load the env file only once: check if $env_file's parent
157   # is in $_autoenv_stack_entered.
158   local env_file_dir=${env_file:A:h}
159   if (( ${+_autoenv_stack_entered[(r)${env_file_dir}]} )); then
160     _autoenv_chpwd_prev_dir=$PWD
161     return
162   fi
163
164   _autoenv_stack_entered+=(${env_file_dir})
165
166   _autoenv_source $env_file enter $PWD
167
168   _autoenv_chpwd_prev_dir=$PWD
169 }
170
171 autoload -U add-zsh-hook
172 add-zsh-hook chpwd _autoenv_chpwd_handler
173
174 # Look in current directory already.
175 _autoenv_chpwd_handler