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
2 changed files with 64 additions and 41 deletions

View File

@ -1,5 +1,6 @@
#compdef autoenv
# Completion for the autoenv command.
_autoenv() {
local ret=1 context curcontext="$curcontext" state line
typeset -A opt_args

View File

@ -5,38 +5,11 @@
# 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.
autoenv() {
local cmd=$1
case "$cmd" in
init) # Create enter and exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
echo '.enter or .exit already exists'; return 1
fi
touch .enter .exit
autoenv edit ;;
edit) # Edit enter and exit scripts in current directory.
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
echo '.enter or .exit not found'; return 1
fi
if which vim &> /dev/null; then
vim -P .enter .exit
else
echo 'vim not found'; return 1
fi ;;
deinit) # Remove enter and exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
while true; do
read "answer?Are you sure [y/N]? "
case "$answer" in
y|Y|yes)
[ -f $PWD/.enter ] && rm $PWD/.enter
[ -f $PWD/.exit ] && rm $PWD/.exit ;;
*) break ;;
esac
done
else
echo '.enter and .exit not found'; return 1
fi ;;
-h|--help) # Display help.
echo "\
usage: autoenv [-h] {init,edit,deinit}
@ -47,17 +20,72 @@ options:
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
";;
*) # Invalid arguments.
deinit remove .enter and .exit scripts in current directory"
;;
init) # Create enter and exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
echo '.enter or .exit already exists'; return 1
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 ] && \
authorized[$PWD/.enter]=`zstat +mtime $PWD/.enter`
# If exit script exists, authorize it.
[ -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
;;
edit) # Edit enter and exit scripts in current directory.
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
echo '.enter or .exit not found'; return 1
fi
# If vim exists, edit enter and exit scripts.
if which vim &> /dev/null; then
vim -p .enter .exit
else
echo 'vim not found'; return 1
fi ;;
deinit) # Remove enter and exit scripts in current directory.
if [ -f $PWD/.enter ] || [ -f $PWD/.exit ]; then
# Prompt user to confirm removal of enter and exit scripts.
while true; do
read "answer?Are you sure [y/N]? "
case "$answer" in
y|Y|yes)
# Remove enter and exit scripts.
[ -f $PWD/.enter ] && rm $PWD/.enter
[ -f $PWD/.exit ] && rm $PWD/.exit
;;
*) break ;;
esac
done
else
echo '.enter and .exit not found'; return 1
fi ;;
*) # Invalid arguments, show help then error.
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
@ -65,14 +93,8 @@ _autoenv_entered=()
# persistent.
_autoenv_authorized() {
local file=$1
# If autoenv cache directory does not exist, create it.
local cache_dir=~/.cache/autoenv
! [ -d $cache_dir ] && mkdir -p $cache_dir
local authorized_file=$cache_dir/authorized
# Define a map to hold authorized key value pairs.
typeset -A authorized=()
# If the authorized file exists, load it into the map.
[ -f $authorized_file ] && typeset -A authorized=(`cat $authorized_file`)
# Load the authorized file into a map of authorized key value pairs.
typeset -A authorized=(`cat ~/.cache/autoenv/authorized`)
# 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`
@ -87,7 +109,7 @@ _autoenv_authorized() {
v|V|view) cat $file ;; # View the file.
esac
done
# Store authorized associative array in authorized file.
# Store authorized map in authorized file.
echo ${(kv)authorized} > $authorized_file
}