Compare commits

..

1 Commits

Author SHA1 Message Date
51f3f7fab4 Add autoenv command to manage autoenv's
usage: autoenv [-h] {init,edit,deinit}

options:
        -h, --help  show this help message and exit

commands:
        init        add .enter and .exit scripts in current directory
        edit        edit .enter and .exit scripts in current directory
        deinit      remove .enter and .exit scripts in current directory
2018-04-25 20:47:05 +01:00

View File

@ -2,8 +2,8 @@
# changes, this is a reimplementation of the ideas found in the repository # changes, this is a reimplementation of the ideas found in the repository
# https://github.com/Tarrasch/zsh-autoenv stripped down to bare essentials. # https://github.com/Tarrasch/zsh-autoenv stripped down to bare essentials.
# #
# The secret sauce can be found at the bottom of this file, where the chpwd # The secret sauce can be found at the bottom of this file, where the `chpwd`
# hook function _autoenv_chpwd is added. # hook function `_autoenv_chpwd` is added.
# The autoenv command provides a convenient way to create, edit, and remove # The autoenv command provides a convenient way to create, edit, and remove
# enter and exit scripts in the current directory. # enter and exit scripts in the current directory.
@ -29,12 +29,19 @@ commands:
fi fi
# Create then edit enter and exit scripts. # Create then edit enter and exit scripts.
touch .enter .exit && autoenv edit touch .enter .exit && autoenv edit
# Load the authorized file into a map of authorized key value pairs.
typeset -A authorized=(`cat ~/.cache/autoenv/authorized`)
# If enter script exists, authorize it. # If enter script exists, authorize it.
[ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes [ -f $PWD/.enter ] && \
authorized[$PWD/.enter]=`zstat +mtime $PWD/.enter`
# If exit script exists, authorize it. # If exit script exists, authorize it.
[ -f $PWD/.exit ] && _autoenv_authorized $PWD/.exit yes [ -f $PWD/.exit ] && \
authorized[$PWD/.exit]=`zstat +mtime $PWD/.exit`
# Store authorized map in authorized file.
echo ${(kv)authorized} > ~/.cache/autoenv/authorized
# Enter the new autoenv. # Enter the new autoenv.
_autoenv_enter $PWD ;; _autoenv_enter $PWD
;;
edit) # Edit enter and exit scripts in current directory. edit) # Edit enter and exit scripts in current directory.
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
@ -54,10 +61,10 @@ commands:
read "answer?Are you sure [y/N]? " read "answer?Are you sure [y/N]? "
case "$answer" in case "$answer" in
y|Y|yes) y|Y|yes)
# Remove enter and exit scripts if they exist. # Remove enter and exit scripts.
[ -f $PWD/.enter ] && rm $PWD/.enter [ -f $PWD/.enter ] && rm $PWD/.enter
[ -f $PWD/.exit ] && rm $PWD/.exit [ -f $PWD/.exit ] && rm $PWD/.exit
break ;; ;;
*) break ;; *) break ;;
esac esac
done done
@ -69,46 +76,41 @@ commands:
echo "invalid arguments: $@" echo "invalid arguments: $@"
autoenv --help autoenv --help
return 1 ;; return 1 ;;
esac esac
} }
# Global entered directories array. # Global entered directories array.
_autoenv_entered=() _autoenv_entered=()
# If autoenv cache directory does not exist, create it.
! [ -d ~/.cache/autoenv ] && mkdir -p ~/.cache/autoenv
# If the authorized file does not exist, create it.
! [ -f ~/.cache/autoenv/authorized ] && touch ~/.cache/autoenv/authorized
# Check if the given file is authorized, if not prompt the user to authorize, # Check if the given file is authorized, if not prompt the user to authorize,
# ignore, or view the file. Authorized files and their modified times are # ignore, or view the file. Authorized files and their modified times are
# stored in the ~/.cache/autoenv/authorized file to make authorization # stored in the ~/.cache/autoenv/authorized file to make authorization
# persistent. # persistent.
_autoenv_authorized() { _autoenv_authorized() {
local file=$1 yes=$2 local file=$1
# If autoenv cache directory does not exist, create it.
! [ -d ~/.cache/autoenv ] && mkdir -p ~/.cache/autoenv
# If the authorized file does not exist, create it.
! [ -f ~/.cache/autoenv/authorized ] && touch ~/.cache/autoenv/authorized
# Load the authorized file into a map of authorized key value pairs. # Load the authorized file into a map of authorized key value pairs.
typeset -A authorized=(`cat ~/.cache/autoenv/authorized`) typeset -A authorized=(`cat ~/.cache/autoenv/authorized`)
# If the file has been removed, return.
! [ -f $file ] && return 1
# If the given file has been authorized, i.e. the modified time matches that # If the given file has been authorized, i.e. the modified time matches that
# held in the authorized file, return. # held in the authorized file, return.
local modified_time=`zstat +mtime $file` local modified_time=`zstat +mtime $file`
[ "$authorized[$file]" = "$modified_time" ] && return [ "$authorized[$file]" = "$modified_time" ] && return
# If yes, don't prompt for user confirmation. # Prompt to authorize file.
if [ "$yes" != "yes" ]; then while true; do
# Prompt to authorize file. read "answer?Authorize $file [Y/n/v]? "
while true; do case "$answer" in
read "answer?Authorize $file [Y/n/v]? " y|Y|yes|'') # Authorize the file.
case "$answer" in authorized[$file]=$modified_time; break ;;
y|Y|yes|'') break ;; # Authorize the file. n|N|no) return 1 ;; # Do not authorize the file.
n|N|no) return 1 ;; # Do not authorize the file. v|V|view) cat $file ;; # View the file.
v|V|view) cat $file ;; # View the file. esac
esac done
done
fi
# Add file to the authorized map.
authorized[$file]=$modified_time
# Store authorized map in authorized file. # Store authorized map in authorized file.
echo ${(kv)authorized} > ~/.cache/autoenv/authorized echo ${(kv)authorized} > $authorized_file
} }
# Source an enter script and add its directory to the global entered # Source an enter script and add its directory to the global entered