mirror of
https://github.com/flynx/bashctrl.git
synced 2025-10-28 02:10:10 +00:00
reworked and modernized the script...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
ce22eb7339
commit
8839fa6d31
10
README.md
10
README.md
@ -1,2 +1,12 @@
|
||||
# bashctrl
|
||||
|
||||
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
317
bashctrl
@ -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`
|
||||
ORIGPATH=`pwd`
|
||||
|
||||
printhelp () {
|
||||
echo 'Bash Control script'
|
||||
echo 'format:'
|
||||
echo ' bashctrl COMMAND [ARGS]'
|
||||
echo
|
||||
echo 'supported commands:'
|
||||
echo ' help - print this message.'
|
||||
echo ' list - list events and installed plugins.'
|
||||
echo ' script - list the plugins in a runnable bash script format.'
|
||||
echo ' plugins - list available plugins.'
|
||||
echo ' events - list supported events.'
|
||||
echo ' add PLUGIN PRIO EVT'
|
||||
echo ' - install PLUGIN to EVT with PRIO.'
|
||||
echo ' if a plugin is already installed, change its settings.'
|
||||
echo ' NOTE: use a two digit priority (arbitary digit prios'
|
||||
echo ' are not supported by some commands).'
|
||||
echo ' del PLUGIN EVT|all'
|
||||
echo ' - remove PLUGIN from EVT.'
|
||||
echo ' if "all" is given, remove from all events.'
|
||||
# XXX
|
||||
#echo ' setup - setup bashctrl directories.'
|
||||
#echo ' update - update pligins'
|
||||
#echo ' run EVENT'
|
||||
#echo ' - run event scripts'
|
||||
echo
|
||||
echo 'other commands:'
|
||||
echo ' drop-all-plugin-bindings'
|
||||
echo ' - removes all installed plugin bindings.'
|
||||
function printhelp(){ cat << EOF
|
||||
Bash control script
|
||||
|
||||
Usage::
|
||||
$SCRIPT COMMAND [ARGS]
|
||||
|
||||
supported commands:
|
||||
-h|--help|help - print this message and exit.
|
||||
|
||||
-l|list - list events and installed plugins.
|
||||
-e|events - list supported events.
|
||||
-p|plugins - list available plugins.
|
||||
-s|script - list the plugins in a runnable bash script format.
|
||||
|
||||
add PLUGIN PRIO EVT
|
||||
- install PLUGIN to EVT with PRIO.
|
||||
if a plugin is already installed, change its settings.
|
||||
NOTE: use a two digit priority (arbitary digit prios
|
||||
are not supported by some commands).
|
||||
del PLUGIN EVT|all
|
||||
- remove PLUGIN from EVT.
|
||||
if "all" is given, remove from all events.
|
||||
|
||||
setup - setup bashctrl directories and defaults.
|
||||
|
||||
run EVENT - run event scripts
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
# handle arguments...
|
||||
|
||||
if [ -z $1 ]; then
|
||||
printhelp
|
||||
exit
|
||||
fi
|
||||
|
||||
cd ~/.bash/
|
||||
|
||||
while true; do
|
||||
if [ -z $1 ]; then
|
||||
break
|
||||
@ -49,152 +96,138 @@ while true; do
|
||||
printhelp
|
||||
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
|
||||
echo "$SCRIPT: Error: wrong argument format."
|
||||
exit 1
|
||||
fi
|
||||
# TODO check if such a plugin exists...
|
||||
file=events/${4}/${3}_${2}
|
||||
file=${BASH_DIR}/events/${4}/${3}_${2}
|
||||
# remove old version if it exists...
|
||||
# TODO make this silent...
|
||||
plugin=plugins/${2}
|
||||
plugin=${BASH_DIR}/plugins/${2}
|
||||
if [ -r $plugin ]; then
|
||||
evt=`echo events/${4}/*_${2}`
|
||||
evt=`echo ${BASH_DIR}/events/${4}/*_${2}`
|
||||
for p in $evt; do
|
||||
if [ $p == $evt ] && ! [ -e $p ]; then
|
||||
break
|
||||
fi
|
||||
rm $p
|
||||
done
|
||||
# TODO test this...
|
||||
ln -s ../../$plugin $file
|
||||
ln -s $plugin $file
|
||||
else
|
||||
echo "$SCRIPT: Error: plugin \"$2\" does not exist or is not readable."
|
||||
fi
|
||||
break
|
||||
;;
|
||||
|
||||
--del-plugins|del)
|
||||
del|delete)
|
||||
# remove all...
|
||||
if [ ${3} = all ] || [ ${3} = '' ] ; then
|
||||
files=`echo events/*/*_${2}`
|
||||
files=`echo ${BASH_DIR}/events/*/*_${2}`
|
||||
echo "rmoving \"$files\"..."
|
||||
rm $files
|
||||
# remove one...
|
||||
else
|
||||
files=`echo events/${3}/*_${2}`
|
||||
files=`echo ${BASH_DIR}/events/${3}/*_${2}`
|
||||
echo "rmoving \"$files\"..."
|
||||
rm $files
|
||||
fi
|
||||
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)
|
||||
if [ -z $2 ] ; then
|
||||
echo 'bashctrl: no EVENT given.' >&2
|
||||
exit 1
|
||||
fi
|
||||
for plugin in events/$2/* ; do
|
||||
for plugin in ${BASH_DIR}/events/$2/* ; do
|
||||
[ $plugin == "README" ] \
|
||||
&& continue
|
||||
source $plugin $2
|
||||
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
|
||||
;;
|
||||
# XXX
|
||||
##update)
|
||||
## break
|
||||
## ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
cd $ORIGPATH
|
||||
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
# vim:set ts=4 sw=4 nowrap spell :
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user