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