Add autoenv add=local subcommand

The `add=local` subcommand creates `.local/bin` if it doesn't exit and
then updates the autoenv to add it to `PATH`.
This commit is contained in:
Kenneth Benzie 2022-10-26 20:29:56 +01:00
parent c691d335c0
commit 5a33d2b5ac
2 changed files with 23 additions and 0 deletions

View File

@ -17,6 +17,7 @@ _autoenv() {
edit:'edit .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' deinit:'remove .enter and .exit scripts in current directory'
reload:'reload the current environment' reload:'reload the current environment'
add=local:'add .local/bin to PATH'
add=py:'add Python virtualenv to the autoenv' add=py:'add Python virtualenv to the autoenv'
) )
_describe -t commands command commands && ret=0 ;; _describe -t commands command commands && ret=0 ;;

View File

@ -22,6 +22,7 @@ commands:
edit edit .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 deinit remove .enter and .exit scripts in current directory
reload reload the current environment reload reload the current environment
add=local add .local/bin to PATH
add=py add Python virtualenv to the autoenv" add=py add Python virtualenv to the autoenv"
;; ;;
@ -92,6 +93,27 @@ commands:
_autoenv_enter $PWD _autoenv_enter $PWD
;; ;;
add=local) # Add .local/bin to PATH
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
echo '.enter or .exit not found'; return 1
fi
_autoenv_exit $PWD
# Create .local/bin if not present
if ! [ -d $PWD/.local/bin ]; then
mkdir -p $PWD/.local/bin
fi
# On enter: store PATH and insert .local/bin
echo 'OLDPATH=$PATH' >> .enter
echo 'PATH=$PWD/.local/bin:$PATH' >> .enter
# On exit: reset PATH
echo 'PATH=$OLDPATH' >> .exit
echo 'unset OLDPATH' >> .exit
# Authorize modified autoenv
_autoenv_authorized $PWD/.enter yes
_autoenv_authorized $PWD/.exit yes
_autoenv_enter $PWD
;;
add=py) # Add Python virtualenv to the sandbox add=py) # Add Python virtualenv to the sandbox
if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then if ! [ -f $PWD/.enter ] || ! [ -f $PWD/.exit ]; then
echo '.enter or .exit not found'; return 1 echo '.enter or .exit not found'; return 1