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
# 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
# hook function _autoenv_chpwd is added.
# The secret sauce can be found at the bottom of this file, where the `chpwd`
# hook function `_autoenv_chpwd` is added.
# The autoenv command provides a convenient way to create, edit, and remove
# enter and exit scripts in the current directory.
@ -29,12 +29,19 @@ commands:
fi
# Create then edit enter and exit scripts.
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.
[ -f $PWD/.enter ] && _autoenv_authorized $PWD/.enter yes
[ -f $PWD/.enter ] && \
authorized[$PWD/.enter]=`zstat +mtime $PWD/.enter`
# 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.
_autoenv_enter $PWD ;;
_autoenv_enter $PWD
;;
edit) # Edit enter and exit scripts in current directory.
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
@ -54,10 +61,10 @@ commands:
read "answer?Are you sure [y/N]? "
case "$answer" in
y|Y|yes)
# Remove enter and exit scripts if they exist.
# Remove enter and exit scripts.
[ -f $PWD/.enter ] && rm $PWD/.enter
[ -f $PWD/.exit ] && rm $PWD/.exit
break ;;
;;
*) break ;;
esac
done
@ -69,46 +76,41 @@ commands:
echo "invalid arguments: $@"
autoenv --help
return 1 ;;
esac
}
# Global entered directories array.
_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,
# ignore, or view the file. Authorized files and their modified times are
# stored in the ~/.cache/autoenv/authorized file to make authorization
# persistent.
_autoenv_authorized() {
local file=$1 yes=$2
# 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
local file=$1
# Load the authorized file into a map of authorized key value pairs.
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
# held in the authorized file, return.
local modified_time=`zstat +mtime $file`
[ "$authorized[$file]" = "$modified_time" ] && return
# If yes, don't prompt for user confirmation.
if [ "$yes" != "yes" ]; then
# Prompt to authorize file.
while true; do
read "answer?Authorize $file [Y/n/v]? "
case "$answer" in
y|Y|yes|'') break ;; # Authorize the file.
n|N|no) return 1 ;; # Do not authorize the file.
v|V|view) cat $file ;; # View the file.
esac
done
fi
# Add file to the authorized map.
authorized[$file]=$modified_time
# Prompt to authorize file.
while true; do
read "answer?Authorize $file [Y/n/v]? "
case "$answer" in
y|Y|yes|'') # Authorize the file.
authorized[$file]=$modified_time; break ;;
n|N|no) return 1 ;; # Do not authorize the file.
v|V|view) cat $file ;; # View the file.
esac
done
# 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