From fce22b3c416ce9863024c6a04e7077a57ecbd927 Mon Sep 17 00:00:00 2001 From: Arash Rouhani Date: Sun, 8 Sep 2013 17:06:26 +0200 Subject: [PATCH] Add source file --- autoenv.zsh | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 autoenv.zsh diff --git a/autoenv.zsh b/autoenv.zsh new file mode 100644 index 0000000..287be05 --- /dev/null +++ b/autoenv.zsh @@ -0,0 +1,65 @@ +# Stolen from +# https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh + +export ENV_AUTHORIZATION_FILE=$HOME/.env_auth + +_dotenv_hash_pair() { + env_file=$1 + env_shasum=$(shasum $env_file | cut -d' ' -f1) + echo "$env_file:$env_shasum" +} + +_dotenv_authorized_env_file() { + env_file=$1 + pair=$(_dotenv_hash_pair $env_file) + touch $ENV_AUTHORIZATION_FILE + \grep -Gq $pair $ENV_AUTHORIZATION_FILE +} + +_dotenv_authorize() { + env_file=$1 + _dotenv_deauthorize $env_file + _dotenv_hash_pair $env_file >> $ENV_AUTHORIZATION_FILE +} + +_dotenv_deauthorize() { + env_file=$1 + echo $(grep -Gv $env_file $ENV_AUTHORIZATION_FILE) > $ENV_AUTHORIZATION_FILE +} + +_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)" +} + +_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 + + read answer + + if [[ $answer == 'y' ]] + then + _dotenv_authorize $env_file + source $env_file + fi + fi +} + +chpwd_functions=($chpwd_functions _dotenv_source_env)