Module: check_mk
Branch: master
Commit: 48ee162e4263e28450f0c87a043fae50a7a79ad7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=48ee162e4263e2…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Sep 5 09:36:00 2017 +0200
5180 Add bash completion for OMD command
OMD is now installing a bash completion configuration for the OMD command.
Depending on the context (root or site user) the completion will show the
available commands.
This only works on platforms where the bash completion package is installed.
Change-Id: Id84947a279b3f24d2184131c74a7c66f0ab86458
---
.bugs/2928 | 6 +-
.werks/5180 | 14 ++++
omd/debian/postinst.in | 1 +
omd/omd.spec.in | 2 +
omd/packages/omd/Makefile | 2 +
omd/packages/omd/bash_completion | 139 +++++++++++++++++++++++++++++++++++++++
omd/packages/omd/skel/.bashrc | 6 ++
7 files changed, 168 insertions(+), 2 deletions(-)
diff --git a/.bugs/2928 b/.bugs/2928
index bcd4ecc..ffd7cb2 100644
--- a/.bugs/2928
+++ b/.bugs/2928
@@ -1,8 +1,10 @@
Title: Order of command boxes in views are language dependent
Component: multisite
-State: open
+Class: bug
+State: done
Date: 2017-07-18 11:29:17
Targetversion: 1.4.0
-Class: bug
The order in Englisch and German is different but should not.
+
+2017-09-05 09:15:07: changed state open -> done
diff --git a/.werks/5180 b/.werks/5180
new file mode 100644
index 0000000..70612bf
--- /dev/null
+++ b/.werks/5180
@@ -0,0 +1,14 @@
+Title: Add bash completion for OMD command
+Level: 1
+Component: omd
+Compatible: compat
+Edition: cre
+Version: 1.5.0i1
+Date: 1504596897
+Class: feature
+
+OMD is now installing a bash completion configuration for the OMD command.
+Depending on the context (root or site user) the completion will show the
+available commands.
+
+This only works on platforms where the bash completion package is installed.
diff --git a/omd/debian/postinst.in b/omd/debian/postinst.in
index f156993..45388d6 100755
--- a/omd/debian/postinst.in
+++ b/omd/debian/postinst.in
@@ -45,6 +45,7 @@ case "$1" in
update-alternatives --install /omd/versions/default \
omd /omd/versions/###OMD_VERSION### ###OMD_SERIAL### \
--slave /usr/bin/omd omd.bin /omd/versions/###OMD_VERSION###/bin/omd \
+ --slave /etc/bash_completion.d/omd omd.bash_completion
/omd/versions/###OMD_VERSION###/lib/omd/omd_bash_completion \
--slave /usr/share/man/man8/omd.8.gz omd.man8 \
/omd/versions/###OMD_VERSION###/share/man/man8/omd.8.gz
diff --git a/omd/omd.spec.in b/omd/omd.spec.in
index f1e87c3..eff9f5a 100644
--- a/omd/omd.spec.in
+++ b/omd/omd.spec.in
@@ -81,6 +81,7 @@ ln -sfn "%{version}" /omd/versions/default
ln -sfn /omd/versions/default/bin/omd /usr/bin/omd
ln -sfn /omd/versions/default/share/man/man8/omd.8.gz /usr/share/man/man8/omd.8.gz
ln -sfn /omd/versions/default/share/omd/apache.conf @APACHE_CONFDIR@/zzz_omd.conf
+ln -sfn /omd/versions/default/lib/omd/omd_bash_completion /etc/bash_completion.d/omd
IS_SYSTEMD=0
if [ -d /usr/lib/systemd ] && which systemctl >/dev/null 2>&1; then
@@ -187,6 +188,7 @@ else
echo "Removing global symbolic links"
rm -f /usr/bin/omd
rm -f /usr/share/man/man8/omd.8.gz
+ rm -f /etc/bash_completion.d/omd
# Can be either the symlink /etc/init.d/omd or the regular
# file /etc/systemd/system/omd.service.
diff --git a/omd/packages/omd/Makefile b/omd/packages/omd/Makefile
index bb06b8d..0db60a7 100644
--- a/omd/packages/omd/Makefile
+++ b/omd/packages/omd/Makefile
@@ -30,9 +30,11 @@ install:
mkdir -p $(DESTDIR)$(OMD_ROOT)/lib/omd
install -m 644 init_profile $(DESTDIR)$(OMD_ROOT)/lib/omd/
install -m 755 port_is_used $(DESTDIR)$(OMD_ROOT)/lib/omd/
+ install -m 644 omd_bash_completion $(DESTDIR)$(OMD_ROOT)/lib/omd/
mkdir -p $(DESTDIR)$(OMD_ROOT)/lib/omd/scripts/post-create
skel:
+ mkdir -p $(SKEL)/etc/bash_completion.d
clean:
diff --git a/omd/packages/omd/bash_completion b/omd/packages/omd/bash_completion
new file mode 100644
index 0000000..b8589df
--- /dev/null
+++ b/omd/packages/omd/bash_completion
@@ -0,0 +1,139 @@
+# Bash completion script for "omd"
+#
+
+_omd_cmd_options() {
+ cmd=$1
+ echo "-h --help"
+ omd $cmd -h | awk '{print $1}' | grep -v ^Possible | tr ',' '
'
+ return
+}
+
+_omd()
+{
+ local GLOBALCMDS="sites|version|versions|help"
+ local
SITECMDS="start|stop|restart|reload|status|update|config|backup|restore|diff"
+ local ROOTCMDS="create|setversion"
+ local ROOTSITECMDS="rm|mv|cp|disable|enable|init|su"
+
+ COMPREPLY=()
+
+ local subcmd site
+ local opts=""
+ local cur="${COMP_WORDS[COMP_CWORD]}"
+
+ # simply expland files and folders if current item starts with . or /
+ if [[ $cur =~ ^/|^\. ]]; then
+ COMPREPLY=( $(compgen -o default -- ${cur}) )
+ return 0
+ fi
+
+ # check if first arg is omd
+ if [[ ${COMP_WORDS[0]} != omd ]]; then
+ COMPREPLY=( $(compgen -o default -- ${cur}) )
+ return 0
+ fi
+
+ # parse args up to COMP_CWORD
+ subcmdargs=()
+ local i=1
+ while [ $i -lt $COMP_CWORD ]; do
+ local arg=${COMP_WORDS[$i]}
+ case "$arg" in
+ -V)
+ i=$((i + 1))
+ ;;
+ -*)
+ :
+ ;;
+ *)
+ if [ "x$subcmd" = "x" ]; then
+ subcmd=$arg
+ if [ $EUID -eq 0 ]; then
+ site="${COMP_WORDS[$i+1]}"
+ i=$((i + 1))
+ else
+ site="$OMD_SITE"
+ fi
+ else
+ subcmdargs+=($arg)
+ fi
+ ;;
+ esac
+ i=$((i + 1))
+ done
+
+ local prev="${COMP_WORDS[COMP_CWORD-1]}"
+
+ # omd -V|setversion ...
+ if [[ $prev =~ -V|setversion ]]; then
+ opts=$(cd /omd/versions && ls -1 | grep -v default)
+
+ # omd ...
+ elif [ "x$subcmd" = "x" ]; then
+ opts="-V -f --force -v "$(omd help | grep ^\ *omd | awk '{print $2
}' | grep -v COMMAND)
+
+ # omd versions|sites ...
+ elif [[ $subcmd =~ versions|sites ]]; then
+ opts="$(_omd_cmd_options $subcmd)"
+
+ # omd version ...
+ elif [[ $subcmd =~ version ]]; then
+ opts="$(_omd_cmd_options $subcmd) $(omd sites --bare)"
+
+ # omd <sitecmd> ... (as root)
+ elif _complete_as_root && [ $prev = $subcmd ] && [[ $subcmd =~
$ROOTSITECMDS|$SITECMDS ]]; then
+ # complete with site names
+ opts="-h --help $(omd sites --bare)"
+
+ # omd backup|restore ...
+ elif [[ $subcmd =~ backup|restore ]]; then
+ opts="$(_omd_cmd_options $subcmd)"
+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) $(compgen -o default --
${cur}) )
+ return 0
+
+ # omd diff ...
+ elif [[ $subcmd = diff ]]; then
+ if [ $EUID -eq 0 ]; then
+ opts="$(_omd_cmd_options $subcmd) $(omd diff -b $site | awk '{print
$2}')"
+ else
+ opts="$(_omd_cmd_options $subcmd) $(omd diff -b | awk '{print
$2}')"
+ fi
+
+ # omd start|stop|restart|reload|status ...
+ elif [[ $subcmd =~ start|stop|restart|reload|status ]]; then
+ opts="$(_omd_cmd_options $subcmd) $(env ls -1 /omd/sites/$site/etc/init.d/
2>/dev/null)"
+
+ # omd config ...
+ elif [[ $subcmd =~ config ]]; then
+
+ # omd config ...
+ if [ ${#subcmdargs[@]} -eq 0 ]; then
+ opts="$(_omd_cmd_options $subcmd) set show"
+
+ # omd config set|show ...
+ elif [ ${#subcmdargs[@]} -eq 1 ]; then
+ opts=$(cat /omd/sites/$site/etc/omd/site.conf | awk -F= '{ print $1
}' | sed 's/^CONFIG_//')
+
+ # omd config set <option> ...
+ elif [ ${subcmdargs[0]} = "set" ]; then
+ if [ $EUID -eq 0 ]; then
+ opts=$(su - $site ./lib/omd/hooks/$prev choices | awk -F: '{ print $1
}')
+ else
+ opts=$(./lib/omd/hooks/$prev choices | awk -F: '{ print $1 }')
+ fi
+ fi
+
+ else
+ if [[ $subcmd =~ $SITECMDS|$ROOTSITECMDS|$GLOBALCMDS|$ROOTCMDS ]]; then
+ opts="$(_omd_cmd_options $subcmd)"
+ fi
+ fi
+
+ if [ "x$opts" != "x" ]; then
+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+ return 0
+ fi
+}
+
+# removing nospace breaks dir/file complete on ex. backup/restore
+complete -o default -o nospace -F _omd omd
diff --git a/omd/packages/omd/skel/.bashrc b/omd/packages/omd/skel/.bashrc
index ab0e03d..84047bc 100644
--- a/omd/packages/omd/skel/.bashrc
+++ b/omd/packages/omd/skel/.bashrc
@@ -21,6 +21,12 @@ if [ "$PS1" ]; then
alias la='ls -la'
if [ -f /etc/bash_completion ]; then
+ # Load system wide bash completion (if available)
. /etc/bash_completion
+
+ # Load site specific bash completions
+ for file in $OMD_ROOT/etc/bash_completion.d/*; do
+ . $file;
+ done
fi
fi