]> Sergey Matveev's repositories - zsh-autoenv.git/blob - autoenv.zsh
Cleanup, and fixes (absolute path for env_file)
[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:=0}
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 _dotenv_check_authorized_env_file() {
60   if ! _dotenv_authorized_env_file $1; then
61     echo "Attempting to load unauthorized env file: $1"
62     echo ""
63     echo "**********************************************"
64     echo ""
65     cat $1
66     echo ""
67     echo "**********************************************"
68     echo ""
69     echo -n "Would you like to authorize it? [y/N] "
70
71     local answer=$(_dotenv_read_answer)
72     echo
73     if [[ $answer != 'y' ]]; then
74       return 1
75     fi
76
77     _dotenv_authorize $1
78   fi
79   return 0
80 }
81
82 _dotenv_chpwd_handler() {
83   local env_file="$PWD/$DOTENV_FILE_ENTER"
84
85   # Handle leave event for previously sourced env files.
86   if [[ $DOTENV_HANDLE_LEAVE == 1 ]] && (( $#_dotenv_stack_entered )); then
87     for prev_dir in ${_dotenv_stack_entered}; do
88       if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
89         local env_file_leave=$prev_dir/$DOTENV_FILE_LEAVE
90         if _dotenv_check_authorized_env_file $env_file_leave; then
91           _dotenv_event=leave
92           source $env_file_leave
93           unset _dotenv_event
94         fi
95         # Remove this entry from the stack.
96         _dotenv_stack_entered=(${_dotenv_stack_entered#$prev_dir})
97       fi
98     done
99   fi
100
101   if ! [[ -f $env_file ]] && [[ $DOTENV_LOOK_UPWARDS == 1 ]]; then
102     setopt localoptions extendedglob
103     local m
104     m=((../)#${DOTENV_FILE_ENTER}(N))
105     if (( $#m )); then
106       env_file=${${m[1]}:A}
107     else
108       return
109     fi
110   fi
111
112   if ! [[ -f $env_file ]] || ! _dotenv_check_authorized_env_file $env_file; then
113     return
114   fi
115
116   # Load the env file only once.
117   if (( ${+_dotenv_stack_entered[(r)${env_file:A:h}]} )); then
118     return
119   fi
120
121   _dotenv_stack_entered+=(${env_file:A:h})
122
123   _dotenv_event=enter
124   source $env_file
125   unset _dotenv_event
126 }
127
128 autoload -U add-zsh-hook
129 add-zsh-hook chpwd _dotenv_chpwd_handler
130
131 # Look in current directory already.
132 _dotenv_chpwd_handler