reworked and modernized the script...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-07-13 18:48:37 +03:00
parent ce22eb7339
commit 8839fa6d31
2 changed files with 185 additions and 142 deletions

View File

@ -1,2 +1,12 @@
# bashctrl # bashctrl
bash config management system bash config management system
## History
This was the backbone of my `.bashrc` file since the late 1990s, but for
some reason I never figured out a logical way to either package this nor
a way to add the project to a VCS, so here is attempt N, in 2022 =)

317
bashctrl
View File

@ -1,45 +1,92 @@
#!/bin/bash #!/usr/bin/bash
#----------------------------------------------------------------------
#
# Bash control script...
#
# Originally created in the late 90s to early 2Ks by Alex A. Naanou.
#
#
#----------------------------------------------------------------------
# base configuration...
# NOTE: these are overloadable...
# NOTE: spaces in file/dir names are not so good here, avoid if you can =)
[ -z $BASH_DIR ] \
&& BASH_DIR=~/.bash/
[ -z $BIN_DIR ] \
&& BIN_DIR=~/bin/
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
DEFAULT_PLUGINS=(
"alias 50 login"
"alias 50 start"
"alias 50 end"
"alias 50 logout"
"prompt 50 start"
)
DISABLED_PLUGINS=(
"bindings 50 login"
"bindings 50 start"
"ssh-agent 50 login"
"bash_completion 99 login"
"bash_completion 99 start"
"env 50 start"
"bin 90 start"
)
#----------------------------------------------------------------------
SCRIPT=`basename $0` SCRIPT=`basename $0`
ORIGPATH=`pwd`
printhelp () { function printhelp(){ cat << EOF
echo 'Bash Control script' Bash control script
echo 'format:'
echo ' bashctrl COMMAND [ARGS]' Usage::
echo $SCRIPT COMMAND [ARGS]
echo 'supported commands:'
echo ' help - print this message.' supported commands:
echo ' list - list events and installed plugins.' -h|--help|help - print this message and exit.
echo ' script - list the plugins in a runnable bash script format.'
echo ' plugins - list available plugins.' -l|list - list events and installed plugins.
echo ' events - list supported events.' -e|events - list supported events.
echo ' add PLUGIN PRIO EVT' -p|plugins - list available plugins.
echo ' - install PLUGIN to EVT with PRIO.' -s|script - list the plugins in a runnable bash script format.
echo ' if a plugin is already installed, change its settings.'
echo ' NOTE: use a two digit priority (arbitary digit prios' add PLUGIN PRIO EVT
echo ' are not supported by some commands).' - install PLUGIN to EVT with PRIO.
echo ' del PLUGIN EVT|all' if a plugin is already installed, change its settings.
echo ' - remove PLUGIN from EVT.' NOTE: use a two digit priority (arbitary digit prios
echo ' if "all" is given, remove from all events.' are not supported by some commands).
# XXX del PLUGIN EVT|all
#echo ' setup - setup bashctrl directories.' - remove PLUGIN from EVT.
#echo ' update - update pligins' if "all" is given, remove from all events.
#echo ' run EVENT'
#echo ' - run event scripts' setup - setup bashctrl directories and defaults.
echo
echo 'other commands:' run EVENT - run event scripts
echo ' drop-all-plugin-bindings'
echo ' - removes all installed plugin bindings.' EOF
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# handle arguments...
if [ -z $1 ]; then if [ -z $1 ]; then
printhelp printhelp
exit exit
fi fi
cd ~/.bash/
while true; do while true; do
if [ -z $1 ]; then if [ -z $1 ]; then
break break
@ -49,152 +96,138 @@ while true; do
printhelp printhelp
break break
;; ;;
--add-plugin|add) -l|list)
echo installed plugins:
for event in ${BASH_DIR}/events/* ; do
[ -f $event ] \
&& continue
echo " `basename ${event}`"
for plugin in ${event}/* ; do
plugin=`basename $plugin`
( [ "$plugin" == "*" ] \
|| [ $plugin == "README" ] ) \
&& continue
echo " $plugin"
done
done
break
;;
-e|events)
echo supported events:
for event in ${BASH_DIR}/events/* ; do
[ -f $event ] \
&& continue
echo " `basename ${event}`"
[ -e ${event}/README ] \
&& cat ${event}/README \
&& echo
done
break
;;
-p|plugins)
echo available plugins:
for plugin in ${BASH_DIR}/plugins/* ; do
plugin=`basename $plugin`
[ $plugin == "README" ] \
&& continue
echo " ${plugin}"
done
break
;;
-s|script)
echo '#!/bin/bash'
echo '# this script will setup a bash plugin configuration.'
echo '# generated by: bashctrl script'
echo '#'
for event in ${BASH_DIR}/events/* ; do
[ -f $event ] \
&& continue
echo
echo "# event: `basename ${event}`"
for plugin in ${event}/* ; do
plugin=`basename $plugin`
( [ "$plugin" == "*" ] \
|| [ $plugin == "README" ] ) \
&& continue
echo "bashctrl add ${plugin/??_/} ${plugin/_*/} `basename ${event}`"
done
done
echo
break
;;
add)
if [[ $# < 4 ]] ; then if [[ $# < 4 ]] ; then
echo "$SCRIPT: Error: wrong argument format." echo "$SCRIPT: Error: wrong argument format."
exit 1 exit 1
fi fi
# TODO check if such a plugin exists... file=${BASH_DIR}/events/${4}/${3}_${2}
file=events/${4}/${3}_${2}
# remove old version if it exists... # remove old version if it exists...
# TODO make this silent... plugin=${BASH_DIR}/plugins/${2}
plugin=plugins/${2}
if [ -r $plugin ]; then if [ -r $plugin ]; then
evt=`echo events/${4}/*_${2}` evt=`echo ${BASH_DIR}/events/${4}/*_${2}`
for p in $evt; do for p in $evt; do
if [ $p == $evt ] && ! [ -e $p ]; then if [ $p == $evt ] && ! [ -e $p ]; then
break break
fi fi
rm $p rm $p
done done
# TODO test this... ln -s $plugin $file
ln -s ../../$plugin $file
else else
echo "$SCRIPT: Error: plugin \"$2\" does not exist or is not readable." echo "$SCRIPT: Error: plugin \"$2\" does not exist or is not readable."
fi fi
break break
;; ;;
del|delete)
--del-plugins|del) # remove all...
if [ ${3} = all ] || [ ${3} = '' ] ; then if [ ${3} = all ] || [ ${3} = '' ] ; then
files=`echo events/*/*_${2}` files=`echo ${BASH_DIR}/events/*/*_${2}`
echo "rmoving \"$files\"..." echo "rmoving \"$files\"..."
rm $files rm $files
# remove one...
else else
files=`echo events/${3}/*_${2}` files=`echo ${BASH_DIR}/events/${3}/*_${2}`
echo "rmoving \"$files\"..." echo "rmoving \"$files\"..."
rm $files rm $files
fi fi
break break
;; ;;
--events|events)
echo supported events:
for event in events/* ; do
if [ $event = "events/run" ]; then
continue
fi
echo " ${event/events\//}"
if [ -e ${event}/DOC ] ; then
cat ${event}/DOC
echo
fi
done
break
;;
--list|list)
echo installed plugins:
for event in events/* ; do
if [ $event = "events/run" ]; then
continue
fi
echo " ${event/events\//}"
for plugin in ${event}/* ; do
plugin=`basename $plugin`
if [[ $plugin == 'DOC' ]] ; then
continue
fi
echo " $plugin"
done
done
break
;;
--script|script)
echo '#!/bin/bash'
echo '# this script will setup a bash plugin configuration.'
echo '# generated by: bashctrl script'
echo '#'
echo '# if you want to do a clean install uncomment the next line...'
echo '##bashctrl drop-all-plugin-bindings'
for event in events/* ; do
if [ $event = "events/run" ]; then
continue
fi
echo
echo "# event: ${event/events\//}"
for plugin in ${event}/* ; do
plugin=`basename $plugin`
if [[ $plugin == 'DOC' ]] ; then
continue
fi
echo "bashctrl add ${plugin/??_/} ${plugin/_*/} ${event/events\//}"
done
done
echo
break
;;
--drop-all-plugin-bindings|drop-all-plugin-bindings)
read -p "are you sure you want to remove all current bash plugin bindings? (yes/No) " ans
if [[ $ans == 'yes' ]] ; then
rm -f ~/.bash/events/*/*
else
echo "not doing anything..."
fi
break
;;
--list-plugins|plugins)
echo available plugins:
for plugin in plugins/* ; do
plugin=`basename $plugin`
if [ $plugin = "README" ]; then
continue
fi
echo " ${plugin}"
done
break
;;
setup)
mkdir -p \
~/.bash/{commands,dat,local,plugins}/ \
~/.bash/events/{start,login,logout,end}/
break
;;
update)
break
;;
run) run)
if [ -z $2 ] ; then if [ -z $2 ] ; then
echo 'bashctrl: no EVENT given.' >&2 echo 'bashctrl: no EVENT given.' >&2
exit 1 exit 1
fi fi
for plugin in events/$2/* ; do for plugin in ${BASH_DIR}/events/$2/* ; do
[ $plugin == "README" ] \
&& continue
source $plugin $2 source $plugin $2
done done
shift
break
;;
setup)
# dirs...
mkdir -p \
${BASH_DIR}/{commands,dat,local,plugins}/ \
${BASH_DIR}/events/{start,login,logout,end}/ \
${BIN_DIR}/
# script...
cp $0 ${BASH_DIR}/
ln -sf ${BASH_DIR}/$0 ~/bin/bashctrl
# plugins...
for plugin in "${DEFAULT_PLUGINS[@]}" ; do
bashctrl add $plugin
done
break break
;; ;;
# XXX
##update)
## break
## ;;
esac esac
shift shift
done done
cd $ORIGPATH
#----------------------------------------------------------------------
# vim:set ts=4 sw=4 nowrap spell :