1
0
mirror of https://github.com/Tarrasch/zsh-autoenv.git synced 2024-11-26 08:50:59 +02:00

Remember non-yes answer for the current shell session

This basically caches $_autoenv_check_authorized_env_file in
$_autoenv_asked_already.  This should also improve the case for
authorized files.

Ref: https://github.com/Tarrasch/zsh-autoenv/issues/29
This commit is contained in:
Daniel Hahler 2015-05-09 18:22:28 +02:00
parent 12cfed6d3f
commit 63740e8ba1
4 changed files with 60 additions and 20 deletions

View File

@ -162,13 +162,6 @@ _autoenv_hash_pair() {
echo ":${env_file}:${env_shasum}:1" echo ":${env_file}:${env_shasum}:1"
} }
_autoenv_authorized_env_file() {
local env_file=$1
local pair=$(_autoenv_hash_pair $env_file)
test -f $AUTOENV_ENV_FILENAME \
&& \grep -qF $pair $AUTOENV_ENV_FILENAME
}
_autoenv_authorize() { _autoenv_authorize() {
local env_file=${1:A} local env_file=${1:A}
_autoenv_deauthorize $env_file _autoenv_deauthorize $env_file
@ -196,30 +189,46 @@ _autoenv_ask_for_yes() {
fi fi
} }
typeset -g -A _autoenv_asked_already
_autoenv_asked_already=()
# Args: 1: absolute path to env file (resolved symlinks). # Args: 1: absolute path to env file (resolved symlinks).
_autoenv_check_authorized_env_file() { _autoenv_check_authorized_env_file() {
if ! [[ -f $1 ]]; then local env_file=${1:A}
if ! [[ -f $env_file ]]; then
return 1 return 1
fi fi
if ! _autoenv_authorized_env_file $1; then
local pair=$(_autoenv_hash_pair $env_file)
if [[ -n ${_autoenv_asked_already[$pair]} ]]; then
_autoenv_debug "Asked already: ${_autoenv_asked_already[$pair]}"
return ${_autoenv_asked_already[$pair]}
fi
if [[ -f $AUTOENV_ENV_FILENAME ]] && \grep -qF $pair $AUTOENV_ENV_FILENAME; then
_autoenv_asked_already[$pair]=0
else
echo "Attempting to load unauthorized env file!" echo "Attempting to load unauthorized env file!"
command ls -l $1 command ls -l $env_file
echo "" echo ""
echo "**********************************************" echo "**********************************************"
echo "" echo ""
cat $1 cat $env_file
echo "" echo ""
echo "**********************************************" echo "**********************************************"
echo "" echo ""
echo -n "Would you like to authorize it? (type 'yes') " echo -n "Would you like to authorize it? (type 'yes') "
if ! _autoenv_ask_for_yes; then if _autoenv_ask_for_yes; then
return 1 _autoenv_authorize $env_file
_autoenv_asked_already[$pair]=0
else
_autoenv_asked_already[$pair]=1
fi fi
_autoenv_authorize $1
fi fi
return 0 return ${_autoenv_asked_already[$pair]}
} }
# Get directory of this file (absolute, with resolved symlinks). # Get directory of this file (absolute, with resolved symlinks).

View File

@ -14,6 +14,7 @@ Now try to make it accept it
$ _autoenv_stack_entered=() $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_ENV_FILENAME
$ _autoenv_asked_already=()
$ _autoenv_ask_for_yes() { echo "yes" } $ _autoenv_ask_for_yes() { echo "yes" }
$ cd . $ cd .
Attempting to load unauthorized env file! Attempting to load unauthorized env file!
@ -37,8 +38,9 @@ Now lets see that it actually checks the shasum value.
$ cd . $ cd .
ENTERED ENTERED
$ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_ENV_FILENAME
$ _autoenv_stack_entered=()
$ _autoenv_asked_already=()
$ test_autoenv_add_to_env $PWD/.env mischief $ test_autoenv_add_to_env $PWD/.env mischief
$ cd . $ cd .
Attempting to load unauthorized env file! Attempting to load unauthorized env file!
@ -56,8 +58,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?
$ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_ENV_FILENAME
$ _autoenv_stack_entered=()
$ _autoenv_asked_already=()
$ _autoenv_ask_for_yes() { echo "no"; return 1 } $ _autoenv_ask_for_yes() { echo "no"; return 1 }
$ cd . $ cd .
Attempting to load unauthorized env file! Attempting to load unauthorized env file!
@ -74,7 +77,8 @@ Now, will it take no for an answer?
Lets also try one more time to ensure it didn't add it. Lets also try one more time to ensure it didn't add it.
$ _autoenv_ask_for_yes() { echo "yes"; return 0 } $ _autoenv_asked_already=()
$ _autoenv_ask_for_yes() { echo "no"; return 1 }
$ cd . $ cd .
Attempting to load unauthorized env file! Attempting to load unauthorized env file!
-* /tmp/cramtests-*/autoenv.t/.env (glob) -* /tmp/cramtests-*/autoenv.t/.env (glob)
@ -85,11 +89,35 @@ Lets also try one more time to ensure it didn't add it.
********************************************** **********************************************
Would you like to authorize it? (type 'yes') no
And now see if we're not being asked again after not allowing it.
$ _autoenv_ask_for_yes() { echo "should_not_be_used"; return 1 }
$ cd .
Reloading the script should keep the current state, e.g. when reloading your
~/.zshrc.
But it should re-ask for unauthorized files.
$ cd ..
$ $TEST_SOURCE_AUTOENV
$ _autoenv_ask_for_yes() { echo "yes"; return 0 }
$ cd -
Attempting to load unauthorized env file!
-* /tmp/cramtests-*/autoenv.t/.env (glob)
**********************************************
echo ENTERED
**********************************************
Would you like to authorize it? (type 'yes') yes Would you like to authorize it? (type 'yes') yes
ENTERED ENTERED
Reloading the script should keep the current state, e.g. when reloading your With an authorized file, it should not re-ask you.
~/.zshrc.
$ $TEST_SOURCE_AUTOENV $ $TEST_SOURCE_AUTOENV
$ cd . $ cd .

View File

@ -42,6 +42,7 @@ Leave the directory and answer "no".
$ cd sub $ cd sub
ENTERED ENTERED
$ _autoenv_asked_already=()
$ _autoenv_ask_for_yes() { echo "yes"; return 0 } $ _autoenv_ask_for_yes() { echo "yes"; return 0 }
$ cd .. $ cd ..
Attempting to load unauthorized env file! Attempting to load unauthorized env file!

View File

@ -110,6 +110,7 @@ Changing the root .env should trigger re-authentication via autoenv_source_paren
First, let's answer "no". First, let's answer "no".
$ echo "echo NEW" >| .env $ echo "echo NEW" >| .env
$ _autoenv_asked_already=()
$ _autoenv_ask_for_yes() { echo "no"; return 1 } $ _autoenv_ask_for_yes() { echo "no"; return 1 }
$ cd sub $ cd sub
autoenv_source_parent_from_sub: autoenv_source_parent_from_sub:
@ -135,6 +136,7 @@ This currently does not trigger re-execution of the .env file.
Touching the .env file will now source the parent env file. Touching the .env file will now source the parent env file.
$ _autoenv_asked_already=()
$ touch -t 201401010104 .env $ touch -t 201401010104 .env
$ cd . $ cd .
autoenv_source_parent_from_sub: autoenv_source_parent_from_sub: