mirror of
https://github.com/Tarrasch/zsh-autoenv.git
synced 2024-11-29 09:50:59 +02:00
Use AUTOENV prefix
- s/DOTENV_/AUTOENV_/ - s/dotenv_/autoenv_/ Ref: https://github.com/Tarrasch/zsh-autoenv/issues/6
This commit is contained in:
parent
e088e83ff9
commit
bcec00d2dd
106
autoenv.zsh
106
autoenv.zsh
@ -4,25 +4,25 @@
|
|||||||
export AUTOENV_ENV_FILENAME=$HOME/.env_auth
|
export AUTOENV_ENV_FILENAME=$HOME/.env_auth
|
||||||
|
|
||||||
# Name of file to look for when entering directories.
|
# Name of file to look for when entering directories.
|
||||||
: ${DOTENV_FILE_ENTER:=.env}
|
: ${AUTOENV_FILE_ENTER:=.env}
|
||||||
|
|
||||||
# Name of file to look for when leaving directories.
|
# Name of file to look for when leaving directories.
|
||||||
# Requires DOTENV_HANDLE_LEAVE=1.
|
# Requires AUTOENV_HANDLE_LEAVE=1.
|
||||||
: ${DOTENV_FILE_LEAVE:=.env.leave}
|
: ${AUTOENV_FILE_LEAVE:=.env.leave}
|
||||||
|
|
||||||
# Look for .env in parent dirs?
|
# Look for .env in parent dirs?
|
||||||
: ${DOTENV_LOOK_UPWARDS:=1}
|
: ${AUTOENV_LOOK_UPWARDS:=1}
|
||||||
|
|
||||||
# Handle leave events when changing away from a subtree, where an "enter"
|
# Handle leave events when changing away from a subtree, where an "enter"
|
||||||
# event was handled?
|
# event was handled?
|
||||||
: ${DOTENV_HANDLE_LEAVE:=1}
|
: ${AUTOENV_HANDLE_LEAVE:=1}
|
||||||
|
|
||||||
|
|
||||||
# Internal: stack of entered (and handled) directories.
|
# Internal: stack of entered (and handled) directories.
|
||||||
_dotenv_stack_entered=()
|
_autoenv_stack_entered=()
|
||||||
|
|
||||||
|
|
||||||
_dotenv_hash_pair() {
|
_autoenv_hash_pair() {
|
||||||
local env_file=$1
|
local env_file=$1
|
||||||
if (( $+2 )); then
|
if (( $+2 )); then
|
||||||
env_shasum=$2
|
env_shasum=$2
|
||||||
@ -32,20 +32,20 @@ _dotenv_hash_pair() {
|
|||||||
echo "$env_file:$env_shasum:1"
|
echo "$env_file:$env_shasum:1"
|
||||||
}
|
}
|
||||||
|
|
||||||
_dotenv_authorized_env_file() {
|
_autoenv_authorized_env_file() {
|
||||||
local env_file=$1
|
local env_file=$1
|
||||||
local pair=$(_dotenv_hash_pair $env_file)
|
local pair=$(_autoenv_hash_pair $env_file)
|
||||||
test -f $AUTOENV_ENV_FILENAME \
|
test -f $AUTOENV_ENV_FILENAME \
|
||||||
&& \grep -qF $pair $AUTOENV_ENV_FILENAME
|
&& \grep -qF $pair $AUTOENV_ENV_FILENAME
|
||||||
}
|
}
|
||||||
|
|
||||||
_dotenv_authorize() {
|
_autoenv_authorize() {
|
||||||
local env_file=$1
|
local env_file=$1
|
||||||
_dotenv_deauthorize $env_file
|
_autoenv_deauthorize $env_file
|
||||||
_dotenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
|
_autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
|
||||||
}
|
}
|
||||||
|
|
||||||
_dotenv_deauthorize() {
|
_autoenv_deauthorize() {
|
||||||
local env_file=$1
|
local env_file=$1
|
||||||
if [[ -f $AUTOENV_ENV_FILENAME ]]; then
|
if [[ -f $AUTOENV_ENV_FILENAME ]]; then
|
||||||
echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
|
echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
|
||||||
@ -53,18 +53,18 @@ _dotenv_deauthorize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# This function can be mocked in tests
|
# This function can be mocked in tests
|
||||||
_dotenv_read_answer() {
|
_autoenv_read_answer() {
|
||||||
local answer
|
local answer
|
||||||
read $=_AUTOENV_TEST_READ_ARGS -q answer
|
read $=_AUTOENV_TEST_READ_ARGS -q answer
|
||||||
echo $answer
|
echo $answer
|
||||||
}
|
}
|
||||||
|
|
||||||
# Args: 1: absolute path to env file (resolved symlinks).
|
# Args: 1: absolute path to env file (resolved symlinks).
|
||||||
_dotenv_check_authorized_env_file() {
|
_autoenv_check_authorized_env_file() {
|
||||||
if ! [[ -f $1 ]]; then
|
if ! [[ -f $1 ]]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
if ! _dotenv_authorized_env_file $1; then
|
if ! _autoenv_authorized_env_file $1; then
|
||||||
echo "Attempting to load unauthorized env file: $1"
|
echo "Attempting to load unauthorized env file: $1"
|
||||||
echo ""
|
echo ""
|
||||||
echo "**********************************************"
|
echo "**********************************************"
|
||||||
@ -75,101 +75,101 @@ _dotenv_check_authorized_env_file() {
|
|||||||
echo ""
|
echo ""
|
||||||
echo -n "Would you like to authorize it? [y/N] "
|
echo -n "Would you like to authorize it? [y/N] "
|
||||||
|
|
||||||
local answer=$(_dotenv_read_answer)
|
local answer=$(_autoenv_read_answer)
|
||||||
echo
|
echo
|
||||||
if [[ $answer != 'y' ]]; then
|
if [[ $answer != 'y' ]]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_dotenv_authorize $1
|
_autoenv_authorize $1
|
||||||
fi
|
fi
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# Initialize $_dotenv_sourced_varstash, but do not overwrite an existing one
|
# Initialize $_autoenv_sourced_varstash, but do not overwrite an existing one
|
||||||
# from e.g. `exec zsh` (to reload your shell config).
|
# from e.g. `exec zsh` (to reload your shell config).
|
||||||
: ${_dotenv_sourced_varstash:=0}
|
: ${_autoenv_sourced_varstash:=0}
|
||||||
|
|
||||||
# Get directory of this file (absolute, with resolved symlinks).
|
# Get directory of this file (absolute, with resolved symlinks).
|
||||||
_dotenv_this_dir=${0:A:h}
|
_autoenv_this_dir=${0:A:h}
|
||||||
|
|
||||||
_dotenv_source() {
|
_autoenv_source() {
|
||||||
local env_file=$1
|
local env_file=$1
|
||||||
_dotenv_event=$2
|
_autoenv_event=$2
|
||||||
_dotenv_envfile_dir=$3
|
_autoenv_envfile_dir=$3
|
||||||
_dotenv_from_dir=$_dotenv_chpwd_prev_dir
|
_autoenv_from_dir=$_autoenv_chpwd_prev_dir
|
||||||
_dotenv_to_dir=$PWD
|
_autoenv_to_dir=$PWD
|
||||||
|
|
||||||
# Source varstash library once.
|
# Source varstash library once.
|
||||||
if [[ $_dotenv_sourced_varstash == 0 ]]; then
|
if [[ $_autoenv_sourced_varstash == 0 ]]; then
|
||||||
source $_dotenv_this_dir/lib/varstash
|
source $_autoenv_this_dir/lib/varstash
|
||||||
export _dotenv_sourced_varstash=1
|
export _autoenv_sourced_varstash=1
|
||||||
# NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
|
# NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
|
||||||
# ${env_file:h}.
|
# ${env_file:h}.
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Change to directory of env file, source it and cd back.
|
# Change to directory of env file, source it and cd back.
|
||||||
local new_dir=$PWD
|
local new_dir=$PWD
|
||||||
builtin cd -q $_dotenv_envfile_dir
|
builtin cd -q $_autoenv_envfile_dir
|
||||||
source $env_file
|
source $env_file
|
||||||
builtin cd -q $new_dir
|
builtin cd -q $new_dir
|
||||||
|
|
||||||
unset _dotenv_event _dotenv_from_dir
|
unset _autoenv_event _autoenv_from_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
_dotenv_chpwd_prev_dir=$PWD
|
_autoenv_chpwd_prev_dir=$PWD
|
||||||
_dotenv_chpwd_handler() {
|
_autoenv_chpwd_handler() {
|
||||||
local env_file="$PWD/$DOTENV_FILE_ENTER"
|
local env_file="$PWD/$AUTOENV_FILE_ENTER"
|
||||||
|
|
||||||
# Handle leave event for previously sourced env files.
|
# Handle leave event for previously sourced env files.
|
||||||
if [[ $DOTENV_HANDLE_LEAVE == 1 ]] && (( $#_dotenv_stack_entered )); then
|
if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
|
||||||
for prev_dir in ${_dotenv_stack_entered}; do
|
for prev_dir in ${_autoenv_stack_entered}; do
|
||||||
if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
|
if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
|
||||||
local env_file_leave=$prev_dir/$DOTENV_FILE_LEAVE
|
local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
|
||||||
if _dotenv_check_authorized_env_file $env_file_leave; then
|
if _autoenv_check_authorized_env_file $env_file_leave; then
|
||||||
_dotenv_source $env_file_leave leave $prev_dir
|
_autoenv_source $env_file_leave leave $prev_dir
|
||||||
fi
|
fi
|
||||||
# Remove this entry from the stack.
|
# Remove this entry from the stack.
|
||||||
_dotenv_stack_entered=(${_dotenv_stack_entered#$prev_dir})
|
_autoenv_stack_entered=(${_autoenv_stack_entered#$prev_dir})
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! [[ -f $env_file ]] && [[ $DOTENV_LOOK_UPWARDS == 1 ]]; then
|
if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
|
||||||
# Look for files in parent dirs, using an extended Zsh glob.
|
# Look for files in parent dirs, using an extended Zsh glob.
|
||||||
setopt localoptions extendedglob
|
setopt localoptions extendedglob
|
||||||
local m
|
local m
|
||||||
m=((../)#${DOTENV_FILE_ENTER}(N))
|
m=((../)#${AUTOENV_FILE_ENTER}(N))
|
||||||
if (( $#m )); then
|
if (( $#m )); then
|
||||||
env_file=${${m[1]}:A}
|
env_file=${${m[1]}:A}
|
||||||
else
|
else
|
||||||
_dotenv_chpwd_prev_dir=$PWD
|
_autoenv_chpwd_prev_dir=$PWD
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! _dotenv_check_authorized_env_file $env_file; then
|
if ! _autoenv_check_authorized_env_file $env_file; then
|
||||||
_dotenv_chpwd_prev_dir=$PWD
|
_autoenv_chpwd_prev_dir=$PWD
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Load the env file only once: check if $env_file's parent
|
# Load the env file only once: check if $env_file's parent
|
||||||
# is in $_dotenv_stack_entered.
|
# is in $_autoenv_stack_entered.
|
||||||
local env_file_dir=${env_file:A:h}
|
local env_file_dir=${env_file:A:h}
|
||||||
if (( ${+_dotenv_stack_entered[(r)${env_file_dir}]} )); then
|
if (( ${+_autoenv_stack_entered[(r)${env_file_dir}]} )); then
|
||||||
_dotenv_chpwd_prev_dir=$PWD
|
_autoenv_chpwd_prev_dir=$PWD
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_dotenv_stack_entered+=(${env_file_dir})
|
_autoenv_stack_entered+=(${env_file_dir})
|
||||||
|
|
||||||
_dotenv_source $env_file enter $PWD
|
_autoenv_source $env_file enter $PWD
|
||||||
|
|
||||||
_dotenv_chpwd_prev_dir=$PWD
|
_autoenv_chpwd_prev_dir=$PWD
|
||||||
}
|
}
|
||||||
|
|
||||||
autoload -U add-zsh-hook
|
autoload -U add-zsh-hook
|
||||||
add-zsh-hook chpwd _dotenv_chpwd_handler
|
add-zsh-hook chpwd _autoenv_chpwd_handler
|
||||||
|
|
||||||
# Look in current directory already.
|
# Look in current directory already.
|
||||||
_dotenv_chpwd_handler
|
_autoenv_chpwd_handler
|
||||||
|
@ -12,9 +12,9 @@ Manually create auth file
|
|||||||
|
|
||||||
Now try to make it accept it
|
Now try to make it accept it
|
||||||
|
|
||||||
$ unset _dotenv_stack_entered
|
$ unset _autoenv_stack_entered
|
||||||
$ rm $AUTOENV_ENV_FILENAME
|
$ rm $AUTOENV_ENV_FILENAME
|
||||||
$ _dotenv_read_answer() { echo 'y' }
|
$ _autoenv_read_answer() { echo 'y' }
|
||||||
$ cd .
|
$ cd .
|
||||||
Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob)
|
Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob)
|
||||||
|
|
||||||
@ -35,11 +35,11 @@ The last "ENTERED" is because it executed the command
|
|||||||
|
|
||||||
Now lets see that it actually checks the shasum value
|
Now lets see that it actually checks the shasum value
|
||||||
|
|
||||||
$ unset _dotenv_stack_entered
|
$ unset _autoenv_stack_entered
|
||||||
$ cd .
|
$ cd .
|
||||||
ENTERED
|
ENTERED
|
||||||
|
|
||||||
$ unset _dotenv_stack_entered
|
$ unset _autoenv_stack_entered
|
||||||
$ rm $AUTOENV_ENV_FILENAME
|
$ rm $AUTOENV_ENV_FILENAME
|
||||||
$ test_autoenv_add_to_env $PWD/.env mischief
|
$ test_autoenv_add_to_env $PWD/.env mischief
|
||||||
$ cd .
|
$ cd .
|
||||||
@ -60,9 +60,9 @@ Now lets see that it actually checks the shasum value
|
|||||||
|
|
||||||
Now, will it take no for an answer?
|
Now, will it take no for an answer?
|
||||||
|
|
||||||
$ unset _dotenv_stack_entered
|
$ unset _autoenv_stack_entered
|
||||||
$ rm $AUTOENV_ENV_FILENAME
|
$ rm $AUTOENV_ENV_FILENAME
|
||||||
$ _dotenv_read_answer() { echo 'n' }
|
$ _autoenv_read_answer() { echo 'n' }
|
||||||
$ cd .
|
$ cd .
|
||||||
Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob)
|
Attempting to load unauthorized env file: /tmp/cramtests-??????/autoenv.t/.env (glob)
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
Test $PWD, $_dotenv_from_dir and _dotenv_to_dir.
|
Test $PWD, $_autoenv_from_dir and _autoenv_to_dir.
|
||||||
|
|
||||||
$ source $TESTDIR/setup.sh
|
$ source $TESTDIR/setup.sh
|
||||||
|
|
||||||
Setup env actions / output.
|
Setup env actions / output.
|
||||||
|
|
||||||
$ DOTENV_LOOK_UPWARDS=1
|
$ AUTOENV_LOOK_UPWARDS=1
|
||||||
$ mkdir -p sub/sub2
|
$ mkdir -p sub/sub2
|
||||||
$ cd sub
|
$ cd sub
|
||||||
$ echo 'echo ENTERED: PWD:${PWD:t} from:${_dotenv_from_dir:t} to:${_dotenv_to_dir:t}' > .env
|
$ echo 'echo ENTERED: PWD:${PWD:t} from:${_autoenv_from_dir:t} to:${_autoenv_to_dir:t}' > .env
|
||||||
$ echo 'echo LEFT: PWD:${PWD:t} from:${_dotenv_from_dir:t} to:${_dotenv_to_dir:t}' > .env.leave
|
$ echo 'echo LEFT: PWD:${PWD:t} from:${_autoenv_from_dir:t} to:${_autoenv_to_dir:t}' > .env.leave
|
||||||
|
|
||||||
Manually create auth files.
|
Manually create auth files.
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ Lets set a simple .env action
|
|||||||
|
|
||||||
Change to the directory.
|
Change to the directory.
|
||||||
|
|
||||||
$ _dotenv_read_answer() { echo 'y' }
|
$ _autoenv_read_answer() { echo 'y' }
|
||||||
$ cd .
|
$ cd .
|
||||||
Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env (glob)
|
Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env (glob)
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ Change to the directory.
|
|||||||
|
|
||||||
Leave the directory and answer "no".
|
Leave the directory and answer "no".
|
||||||
|
|
||||||
$ _dotenv_read_answer() { echo 'n' }
|
$ _autoenv_read_answer() { echo 'n' }
|
||||||
$ cd ..
|
$ cd ..
|
||||||
Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env.leave (glob)
|
Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env.leave (glob)
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ Leave the directory and answer "no".
|
|||||||
|
|
||||||
$ cd sub
|
$ cd sub
|
||||||
ENTERED
|
ENTERED
|
||||||
$ _dotenv_read_answer() { echo 'y' }
|
$ _autoenv_read_answer() { echo 'y' }
|
||||||
$ cd ..
|
$ cd ..
|
||||||
Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env.leave (glob)
|
Attempting to load unauthorized env file: /tmp/cramtests-??????/leave.t/sub/.env.leave (glob)
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ Leave the directory and answer "no".
|
|||||||
|
|
||||||
Now check with subdirs, looking upwards.
|
Now check with subdirs, looking upwards.
|
||||||
|
|
||||||
$ DOTENV_LOOK_UPWARDS=1
|
$ AUTOENV_LOOK_UPWARDS=1
|
||||||
$ mkdir sub/child
|
$ mkdir sub/child
|
||||||
$ cd sub/child
|
$ cd sub/child
|
||||||
ENTERED
|
ENTERED
|
||||||
@ -68,7 +68,7 @@ Now check with subdirs, looking upwards.
|
|||||||
|
|
||||||
Now check with subdirs, not looking at parent dirs.
|
Now check with subdirs, not looking at parent dirs.
|
||||||
|
|
||||||
$ DOTENV_LOOK_UPWARDS=0
|
$ AUTOENV_LOOK_UPWARDS=0
|
||||||
$ cd sub/child
|
$ cd sub/child
|
||||||
$ cd ..
|
$ cd ..
|
||||||
ENTERED
|
ENTERED
|
||||||
@ -77,10 +77,10 @@ Now check with subdirs, not looking at parent dirs.
|
|||||||
LEFT
|
LEFT
|
||||||
|
|
||||||
|
|
||||||
Test that .env is sourced only once with DOTENV_HANDLE_LEAVE=0.
|
Test that .env is sourced only once with AUTOENV_HANDLE_LEAVE=0.
|
||||||
|
|
||||||
$ unset _dotenv_stack_entered
|
$ unset _autoenv_stack_entered
|
||||||
$ DOTENV_HANDLE_LEAVE=0
|
$ AUTOENV_HANDLE_LEAVE=0
|
||||||
$ cd sub
|
$ cd sub
|
||||||
ENTERED
|
ENTERED
|
||||||
$ cd ..
|
$ cd ..
|
||||||
|
@ -7,12 +7,12 @@
|
|||||||
_AUTOENV_TEST_READ_ARGS='-t 1'
|
_AUTOENV_TEST_READ_ARGS='-t 1'
|
||||||
|
|
||||||
test_autoenv_add_to_env() {
|
test_autoenv_add_to_env() {
|
||||||
_dotenv_hash_pair $1 $2 >> $AUTOENV_ENV_FILENAME
|
_autoenv_hash_pair $1 $2 >> $AUTOENV_ENV_FILENAME
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add enter and leave env files to authentication file.
|
# Add enter and leave env files to authentication file.
|
||||||
test_autoenv_auth_env_files() {
|
test_autoenv_auth_env_files() {
|
||||||
echo -n > $AUTOENV_ENV_FILENAME
|
echo -n > $AUTOENV_ENV_FILENAME
|
||||||
test_autoenv_add_to_env $PWD/$DOTENV_FILE_ENTER
|
test_autoenv_add_to_env $PWD/$AUTOENV_FILE_ENTER
|
||||||
test_autoenv_add_to_env $PWD/$DOTENV_FILE_LEAVE
|
test_autoenv_add_to_env $PWD/$AUTOENV_FILE_LEAVE
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ Setup test environment.
|
|||||||
|
|
||||||
$ mkdir sub
|
$ mkdir sub
|
||||||
$ cd sub
|
$ cd sub
|
||||||
$ echo "autostash FOO=baz" > $DOTENV_FILE_ENTER
|
$ echo "autostash FOO=baz" > $AUTOENV_FILE_ENTER
|
||||||
$ echo "autounstash" > $DOTENV_FILE_LEAVE
|
$ echo "autounstash" > $AUTOENV_FILE_LEAVE
|
||||||
|
|
||||||
Manually create auth file
|
Manually create auth file
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user