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

Rename AUTOENV_ENV_FILENAME to AUTOENV_AUTH_FILE; use XDG

This makes the name of the auth file location more fitting, and uses the
[XDG spec](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables)
for the default location (~/.local/share/autoenv_auth).

When the old setting is used, or the previous default exists, a warning
is given with instructions.
This commit is contained in:
Daniel Hahler 2015-05-22 00:16:53 +02:00
parent 18201debd6
commit 9279fcc441
4 changed files with 40 additions and 22 deletions

View File

@ -2,7 +2,25 @@
# https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh # https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
# File to store confirmed authentication into. # File to store confirmed authentication into.
: ${AUTOENV_ENV_FILENAME:=~/.autoenv_auth} # This handles the deprecated, old location(s).
if [[ -z $AUTOENV_AUTH_FILE ]]; then
if [[ -n $AUTOENV_ENV_FILENAME ]]; then
echo "zsh-autoenv: using deprecated setting for AUTOENV_AUTH_FILE from AUTOENV_ENV_FILENAME." >&2
echo "Please set AUTOENV_AUTH_FILE instead." >&2
AUTOENV_AUTH_FILE=$AUTOENV_ENV_FILENAME
else
if [[ -n $XDG_DATA_HOME ]]; then
AUTOENV_AUTH_FILE=$XDG_DATA_HOME/autoenv_auth
else
AUTOENV_AUTH_FILE=~/.local/share/autoenv_auth
fi
if [[ -f ~/.env_auth ]]; then
echo "zsh-autoenv: using deprecated location for AUTOENV_AUTH_FILE." >&2
echo "Please move it: mv ~/.env_auth ${(D)AUTOENV_AUTH_FILE}." >&2
AUTOENV_AUTH_FILE=~/.env_auth
fi
fi
fi
# Name of the file to look for when entering directories. # Name of the file to look for when entering directories.
: ${AUTOENV_FILE_ENTER:=.autoenv.zsh} : ${AUTOENV_FILE_ENTER:=.autoenv.zsh}
@ -167,14 +185,14 @@ _autoenv_hash_pair() {
_autoenv_authorized_env_file() { _autoenv_authorized_env_file() {
local env_file=$1 local env_file=$1
local pair=$(_autoenv_hash_pair $env_file) local pair=$(_autoenv_hash_pair $env_file)
test -f $AUTOENV_ENV_FILENAME \ test -f $AUTOENV_AUTH_FILE \
&& \grep -qF $pair $AUTOENV_ENV_FILENAME && \grep -qF $pair $AUTOENV_AUTH_FILE
} }
_autoenv_authorize() { _autoenv_authorize() {
local env_file=${1:A} local env_file=${1:A}
_autoenv_deauthorize $env_file _autoenv_deauthorize $env_file
_autoenv_hash_pair $env_file >>| $AUTOENV_ENV_FILENAME _autoenv_hash_pair $env_file >>| $AUTOENV_AUTH_FILE
} }
# Deauthorize a given filename, by removing it from the auth file. # Deauthorize a given filename, by removing it from the auth file.
@ -182,8 +200,8 @@ _autoenv_authorize() {
# allow for writing to the same file again. # allow for writing to the same file again.
_autoenv_deauthorize() { _autoenv_deauthorize() {
local env_file=${1:A} local env_file=${1:A}
if [[ -s $AUTOENV_ENV_FILENAME ]]; then if [[ -s $AUTOENV_AUTH_FILE ]]; then
echo "$(\grep -vF :${env_file}: $AUTOENV_ENV_FILENAME)" >| $AUTOENV_ENV_FILENAME echo "$(\grep -vF :${env_file}: $AUTOENV_AUTH_FILE)" >| $AUTOENV_AUTH_FILE
fi fi
} }

View File

@ -21,27 +21,27 @@ Tests for _autoenv_authorize. {{{
Auth file is empty. Auth file is empty.
$ cd ../.. $ cd ../..
$ cat $AUTOENV_ENV_FILENAME $ cat $AUTOENV_AUTH_FILE
Failed authorization should keep the auth file empty. Failed authorization should keep the auth file empty.
$ _autoenv_authorize does-not-exist $ _autoenv_authorize does-not-exist
Missing file argument for _autoenv_hash_pair! Missing file argument for _autoenv_hash_pair!
[1] [1]
$ cat $AUTOENV_ENV_FILENAME $ cat $AUTOENV_AUTH_FILE
Now adding some auth pair. Now adding some auth pair.
$ echo first > first $ echo first > first
$ _autoenv_authorize first $ _autoenv_authorize first
$ cat $AUTOENV_ENV_FILENAME $ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob)
And a second one. And a second one.
$ echo second > second $ echo second > second
$ _autoenv_authorize second $ _autoenv_authorize second
$ cat $AUTOENV_ENV_FILENAME $ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob)
@ -49,7 +49,7 @@ And a third.
$ echo third > third $ echo third > third
$ _autoenv_authorize third $ _autoenv_authorize third
$ cat $AUTOENV_ENV_FILENAME $ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/third:ad180453bf8a374a15df3e90a78c180230146a7c:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/third:ad180453bf8a374a15df3e90a78c180230146a7c:1 (glob)
@ -57,7 +57,7 @@ And a third.
Re-add the second one, with the same hash. Re-add the second one, with the same hash.
$ _autoenv_authorize second $ _autoenv_authorize second
$ cat $AUTOENV_ENV_FILENAME $ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/first:271ac93c44ac198d92e706c6d6f1d84aefcfa337:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/third:ad180453bf8a374a15df3e90a78c180230146a7c:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/third:ad180453bf8a374a15df3e90a78c180230146a7c:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob)
@ -66,7 +66,7 @@ Re-add the first one, with a new hash.
$ echo one more line >> first $ echo one more line >> first
$ _autoenv_authorize first $ _autoenv_authorize first
$ cat $AUTOENV_ENV_FILENAME $ cat $AUTOENV_AUTH_FILE
:/tmp/cramtests-*/_autoenv_utils.t/third:ad180453bf8a374a15df3e90a78c180230146a7c:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/third:ad180453bf8a374a15df3e90a78c180230146a7c:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/second:7bee8f3b184e1e141ff76efe369c3b8bfc50e64c:1 (glob)
:/tmp/cramtests-*/_autoenv_utils.t/first:65eb010197b73ddc109b7210080f97a87f53451e:1 (glob) :/tmp/cramtests-*/_autoenv_utils.t/first:65eb010197b73ddc109b7210080f97a87f53451e:1 (glob)

View File

@ -13,7 +13,7 @@ Manually create auth file
Now try to make it accept it Now try to make it accept it
$ _autoenv_stack_entered=() $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_AUTH_FILE
$ _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!
@ -38,7 +38,7 @@ Now lets see that it actually checks the shasum value.
ENTERED ENTERED
$ _autoenv_stack_entered=() $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_AUTH_FILE
$ test_autoenv_add_to_env $PWD/.autoenv.zsh mischief $ test_autoenv_add_to_env $PWD/.autoenv.zsh mischief
$ cd . $ cd .
Attempting to load unauthorized env file! Attempting to load unauthorized env file!
@ -57,7 +57,7 @@ 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=() $ _autoenv_stack_entered=()
$ rm $AUTOENV_ENV_FILENAME $ rm $AUTOENV_AUTH_FILE
$ _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!

View File

@ -2,17 +2,17 @@
# #
# It returns 1 in case of errors, and no tests should be run then! # It returns 1 in case of errors, and no tests should be run then!
# #
# Ensure we have our mocked out AUTOENV_ENV_FILENAME # Ensure we have our mocked out AUTOENV_AUTH_FILE
# (via .zshenv). # (via .zshenv).
# Treat unset variables as errors. # Treat unset variables as errors.
# Not handled in varstash yet. # Not handled in varstash yet.
# setopt nounset # setopt nounset
export AUTOENV_ENV_FILENAME="$CRAMTMP/.autoenv_auth" export AUTOENV_AUTH_FILE="$CRAMTMP/.autoenv_auth"
if [[ $AUTOENV_ENV_FILENAME[0,4] != '/tmp' ]]; then if [[ $AUTOENV_AUTH_FILE[0,4] != '/tmp' ]]; then
echo "AUTOENV_ENV_FILENAME is not in /tmp. Aborting." echo "AUTOENV_AUTH_FILE is not in /tmp. Aborting."
return 1 return 1
fi fi
@ -24,11 +24,11 @@ set -e
$TEST_SOURCE_AUTOENV $TEST_SOURCE_AUTOENV
# Reset any authentication. # Reset any authentication.
echo -n >| $AUTOENV_ENV_FILENAME echo -n >| $AUTOENV_AUTH_FILE
# Add file $1 (with optional hash $2) to authentication file. # Add file $1 (with optional hash $2) to authentication file.
test_autoenv_add_to_env() { test_autoenv_add_to_env() {
_autoenv_hash_pair $1 ${2:-} >>| $AUTOENV_ENV_FILENAME _autoenv_hash_pair $1 ${2:-} >>| $AUTOENV_AUTH_FILE
} }
# Add enter and leave env files to authentication file. # Add enter and leave env files to authentication file.