Compare commits
1 Commits
173337d9a2
...
ac97157665
Author | SHA1 | Date | |
---|---|---|---|
ac97157665 |
@ -6,45 +6,14 @@
|
|||||||
# Global entered directories array.
|
# Global entered directories array.
|
||||||
_autoenv_entered=()
|
_autoenv_entered=()
|
||||||
|
|
||||||
# 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
|
|
||||||
# persistant.
|
|
||||||
_autoenv_authorized() {
|
|
||||||
local file=$1
|
|
||||||
# If autoenv cache directory does not exist, create it.
|
|
||||||
local cache_dir=~/.config/autoenv
|
|
||||||
! [ -d $cache_dir ] && mkdir -p $cache_dir
|
|
||||||
local authorized_file=$cache_dir/authorized
|
|
||||||
# Define the associative array to hold authorized key value pairs.
|
|
||||||
# If the authorized file exists, load it into an associative array.
|
|
||||||
typeset -A authorized=()
|
|
||||||
[ -f $authorized_file ] && typeset -A authorized=(`cat $authorized_file`)
|
|
||||||
# If the given file has been authorized, return.
|
|
||||||
local modified_time=`zstat +mtime $file`
|
|
||||||
[ "$authorized[$file]" = "$modified_time" ] && return
|
|
||||||
# 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 associative array in authorized file.
|
|
||||||
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
|
||||||
# directories array.
|
# directories array.
|
||||||
_autoenv_enter() {
|
_autoenv_enter() {
|
||||||
local entered=$1
|
local entered=$1
|
||||||
# If entered exists in the entered directories array, return.
|
# If entered has already been sourced, return.
|
||||||
(( ${+_autoenv_entered[${_autoenv_entered[(i)$entered]}]} )) && return
|
(( ${+_autoenv_entered[${_autoenv_entered[(i)$entered]}]} )) && return
|
||||||
# If the enter script is not authorized, return.
|
# If the enter script is not authorized, return.
|
||||||
_autoenv_authorized $entered/.enter || return
|
# TODO: _autoenv_authorized $entered/.enter || return
|
||||||
# Source the enter script.
|
# Source the enter script.
|
||||||
source $entered/.enter
|
source $entered/.enter
|
||||||
# Add the entered directory to the global entered array.
|
# Add the entered directory to the global entered array.
|
||||||
@ -56,7 +25,7 @@ _autoenv_enter() {
|
|||||||
_autoenv_exit() {
|
_autoenv_exit() {
|
||||||
local entered=$1
|
local entered=$1
|
||||||
# If the exit script is not authorized, return.
|
# If the exit script is not authorized, return.
|
||||||
_autoenv_authorized $entered/.exit || return
|
# TODO: _autoenv_authorized $entered/.exit || return
|
||||||
# Source the exit script.
|
# Source the exit script.
|
||||||
source $entered/.exit
|
source $entered/.exit
|
||||||
# Remove the entered directory from the global entered array.
|
# Remove the entered directory from the global entered array.
|
||||||
@ -71,7 +40,7 @@ _autoenv_find_enter() {
|
|||||||
[ -f "$current/.enter" ] && echo $current
|
[ -f "$current/.enter" ] && echo $current
|
||||||
# Loop until an enter script or the root directory is found.
|
# Loop until an enter script or the root directory is found.
|
||||||
while true; do
|
while true; do
|
||||||
# Go up one directory and make the path absolute.
|
# Walk up one directory and make the path absolute.
|
||||||
local next=$current/..; local next=${next:A}
|
local next=$current/..; local next=${next:A}
|
||||||
# If the current directory equals the next directory, return.
|
# If the current directory equals the next directory, return.
|
||||||
[[ $current == $next ]] && return || local current=$next
|
[[ $current == $next ]] && return || local current=$next
|
||||||
|
Loading…
x
Reference in New Issue
Block a user