zsh-autoenv/autoenv.zsh

71 lines
1.5 KiB
Bash
Raw Normal View History

2013-09-08 18:06:26 +03:00
# Stolen from
# https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
export ENV_AUTHORIZATION_FILE=$HOME/.env_auth
_dotenv_hash_pair() {
2014-11-15 15:49:48 +02:00
local env_file=$1
2013-09-08 18:06:26 +03:00
env_shasum=$(shasum $env_file | cut -d' ' -f1)
echo "$env_file:$env_shasum"
}
_dotenv_authorized_env_file() {
2014-11-15 15:49:48 +02:00
local env_file=$1
local pair=$(_dotenv_hash_pair $env_file)
2013-09-08 18:06:26 +03:00
touch $ENV_AUTHORIZATION_FILE
\grep -Gq $pair $ENV_AUTHORIZATION_FILE
}
_dotenv_authorize() {
2014-11-15 15:49:48 +02:00
local env_file=$1
2013-09-08 18:06:26 +03:00
_dotenv_deauthorize $env_file
_dotenv_hash_pair $env_file >> $ENV_AUTHORIZATION_FILE
}
_dotenv_deauthorize() {
2014-11-15 15:49:48 +02:00
local env_file=$1
echo $(\grep -Gv $env_file $ENV_AUTHORIZATION_FILE) > $ENV_AUTHORIZATION_FILE
2013-09-08 18:06:26 +03:00
}
_dotenv_print_unauthorized_message() {
echo "Attempting to load unauthorized env: $1"
echo ""
echo "**********************************************"
echo ""
cat $1
echo ""
echo "**********************************************"
echo ""
echo "Would you like to authorize it? (y/n)"
}
2013-09-08 19:32:16 +03:00
# This function can be mocked in tests
_dotenv_read_answer() {
read answer
}
2013-09-08 18:06:26 +03:00
_dotenv_source_env() {
local env_file="$PWD/.env"
if [[ -f $env_file ]]
then
if _dotenv_authorized_env_file $env_file
then
source $env_file
return 0
fi
_dotenv_print_unauthorized_message $env_file
2013-09-08 19:32:16 +03:00
_dotenv_read_answer
2013-09-08 18:06:26 +03:00
if [[ $answer == 'y' ]]
then
_dotenv_authorize $env_file
source $env_file
fi
fi
}
chpwd_functions=($chpwd_functions _dotenv_source_env)