]> Sergey Matveev's repositories - zsh-autoenv.git/blob - autoenv.zsh
Set DOTENV_LOOK_UPWARDS=1 by default
[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   env_shasum=$(shasum $env_file | cut -d' ' -f1)
29   echo "$env_file:$env_shasum"
30 }
31
32 _dotenv_authorized_env_file() {
33   local env_file=$1
34   local pair=$(_dotenv_hash_pair $env_file)
35   test -f $ENV_AUTHORIZATION_FILE \
36     && \grep -qF $pair $ENV_AUTHORIZATION_FILE
37 }
38
39 _dotenv_authorize() {
40   local env_file=$1
41   _dotenv_deauthorize $env_file
42   _dotenv_hash_pair $env_file >> $ENV_AUTHORIZATION_FILE
43 }
44
45 _dotenv_deauthorize() {
46   local env_file=$1
47   if [[ -f $ENV_AUTHORIZATION_FILE ]]; then
48     echo $(\grep -vF $env_file $ENV_AUTHORIZATION_FILE) > $ENV_AUTHORIZATION_FILE
49   fi
50 }
51
52 # This function can be mocked in tests
53 _dotenv_read_answer() {
54   local answer
55   read -q answer
56   echo $answer
57 }
58
59 # Args: 1: absolute path to env file (resolved symlinks).
60 _dotenv_check_authorized_env_file() {
61   if ! [[ -f $1 ]]; then
62     return 1
63   fi
64   if ! _dotenv_authorized_env_file $1; then
65     echo "Attempting to load unauthorized env file: $1"
66     echo ""
67     echo "**********************************************"
68     echo ""
69     cat $1
70     echo ""
71     echo "**********************************************"
72     echo ""
73     echo -n "Would you like to authorize it? [y/N] "
74
75     local answer=$(_dotenv_read_answer)
76     echo
77     if [[ $answer != 'y' ]]; then
78       return 1
79     fi
80
81     _dotenv_authorize $1
82   fi
83   return 0
84 }
85
86 _dotenv_source() {
87   local env_file=$1
88   _dotenv_event=$2
89   _dotenv_cwd=$PWD
90
91   builtin cd -q ${env_file:h}
92   source $env_file
93   builtin cd -q $_dotenv_cwd
94
95   unset _dotenv_event _dotenv_cwd
96 }
97
98 _dotenv_chpwd_handler() {
99   local env_file="$PWD/$DOTENV_FILE_ENTER"
100
101   # Handle leave event for previously sourced env files.
102   if [[ $DOTENV_HANDLE_LEAVE == 1 ]] && (( $#_dotenv_stack_entered )); then
103     for prev_dir in ${_dotenv_stack_entered}; do
104       if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
105         local env_file_leave=$prev_dir/$DOTENV_FILE_LEAVE
106         if _dotenv_check_authorized_env_file $env_file_leave; then
107           _dotenv_source $env_file_leave leave
108         fi
109         # Remove this entry from the stack.
110         _dotenv_stack_entered=(${_dotenv_stack_entered#$prev_dir})
111       fi
112     done
113   fi
114
115   if ! [[ -f $env_file ]] && [[ $DOTENV_LOOK_UPWARDS == 1 ]]; then
116     # Look for files in parent dirs, using an extended Zsh glob.
117     setopt localoptions extendedglob
118     local m
119     m=((../)#${DOTENV_FILE_ENTER}(N))
120     if (( $#m )); then
121       env_file=${${m[1]}:A}
122     else
123       return
124     fi
125   fi
126
127   if ! _dotenv_check_authorized_env_file $env_file; then
128     return
129   fi
130
131   # Load the env file only once: check if $env_file's parent
132   # is in $_dotenv_stack_entered.
133   local env_file_dir=${env_file:A:h}
134   if (( ${+_dotenv_stack_entered[(r)${env_file_dir}]} )); then
135     return
136   fi
137
138   _dotenv_stack_entered+=(${env_file_dir})
139
140   _dotenv_source $env_file enter
141 }
142
143 autoload -U add-zsh-hook
144 add-zsh-hook chpwd _dotenv_chpwd_handler
145
146 # Look in current directory already.
147 _dotenv_chpwd_handler