[Libguestfs] [p2v PATCH] Add bash completion scripts

Pino Toscano ptoscano at redhat.com
Thu Jul 11 09:06:31 UTC 2019


Copy over the bash completion machinery from libguestfs, along with the
simple completion script; use this script for all the tools.
---
 .gitignore                      |  2 +
 Makefile.am                     |  3 ++
 bash/Makefile.am                | 68 +++++++++++++++++++++++++++++++++
 bash/README                     |  6 +++
 bash/test-complete-in-script.sh | 40 +++++++++++++++++++
 bash/virt-p2v-make-disk         | 59 ++++++++++++++++++++++++++++
 configure.ac                    |  5 +++
 m4/p2v-bash-completion.m4       | 29 ++++++++++++++
 8 files changed, 212 insertions(+)
 create mode 100644 bash/Makefile.am
 create mode 100644 bash/README
 create mode 100755 bash/test-complete-in-script.sh
 create mode 100644 bash/virt-p2v-make-disk
 create mode 100644 m4/p2v-bash-completion.m4

diff --git a/.gitignore b/.gitignore
index e1d27b7..a81b59a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,8 @@ Makefile.in
 /about-authors.c
 /aclocal.m4
 /autom4te.cache/
+/bash/virt-p2v-make-kickstart
+/bash/virt-p2v-make-kiwi
 /blank-part.img
 /build-aux/
 /config.c
diff --git a/Makefile.am b/Makefile.am
index ac7e62f..2d0178c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,6 +24,9 @@ if ENABLE_GNULIB_TESTS
 SUBDIRS += gnulib/tests
 endif
 
+# bash-completion
+SUBDIRS += bash
+
 EXTRA_DIST = \
 	$(BUILT_SOURCES) \
 	$(TESTS) $(LIBGUESTFS_TESTS) $(SLOW_TESTS) \
diff --git a/bash/Makefile.am b/bash/Makefile.am
new file mode 100644
index 0000000..2a59f75
--- /dev/null
+++ b/bash/Makefile.am
@@ -0,0 +1,68 @@
+# libguestfs
+# Copyright (C) 2013-2019 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+include $(top_srcdir)/subdir-rules.mk
+
+scripts = \
+	virt-p2v-make-disk
+
+# Some of the scripts are simply symbolic links.
+symlinks = \
+	virt-p2v-make-kickstart \
+	virt-p2v-make-kiwi
+
+# Note: Don't distribute the symbolic links, only the real files.
+EXTRA_DIST = \
+	README \
+	$(scripts) \
+	$(TESTS)
+
+CLEANFILES += \
+	$(symlinks)
+
+$(symlinks):
+	rm -f $@
+	$(LN_S) virt-p2v-make-disk $@
+
+if HAVE_BASH_COMPLETION
+
+# Bash completion script.
+
+bashcompletiondir = $(BASH_COMPLETIONS_DIR)
+#bashcompletion_DATA = $(scripts)
+
+all-local: $(scripts) $(symlinks)
+	-test $(srcdir) != $(builddir) && cd $(srcdir) && cp $(scripts) $(abs_builddir)/
+
+install-data-local:
+	$(mkdir_p) $(DESTDIR)$(bashcompletiondir)
+	cp -d $(scripts) $(symlinks) $(DESTDIR)$(bashcompletiondir)
+
+clean-local:
+	-test $(srcdir) != $(builddir) && rm -f $(scripts)
+
+endif
+
+# Tests.
+TESTS_ENVIRONMENT = \
+	scripts="$(scripts)" \
+	symlinks="$(scripts)" \
+	commands="$(scripts) $(symlinks)" \
+	$(top_builddir)/run --test
+
+TESTS = \
+	test-complete-in-script.sh
diff --git a/bash/README b/bash/README
new file mode 100644
index 0000000..314bfd0
--- /dev/null
+++ b/bash/README
@@ -0,0 +1,6 @@
+This directory contains the scripts for tab-completing commands in
+bash.  Note these new-style demand-loaded scripts require
+'bash-completion' >= 1.99.
+
+Tip: To test the bash completions without having to install them,
+simply start a new shell and 'source ./virt-foo'.
diff --git a/bash/test-complete-in-script.sh b/bash/test-complete-in-script.sh
new file mode 100755
index 0000000..7fff0bc
--- /dev/null
+++ b/bash/test-complete-in-script.sh
@@ -0,0 +1,40 @@
+#!/bin/bash -
+# libguestfs bash completion test script
+# Copyright (C) 2016 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test that the correct 'complete' command is included in the script.
+# Mainly prevents symlinking errors and some omissions.
+
+$TEST_FUNCTIONS
+skip_if_skipped
+
+if [ -z "$commands" ]; then
+    echo "$0: \$commands is not defined.  Use 'make check' to run this test."
+    exit 1
+fi
+
+for cmd in $commands; do
+    if [ ! -f $cmd ]; then
+        echo "$0: script or symlink '$cmd' is missing"
+        exit 1
+    fi
+    if ! grep "^complete.*$cmd\$" $cmd; then
+        echo "$0: script or symlink '$cmd' does not have"
+        echo "a 'complete' rule for '$cmd'"
+        exit 1
+    fi
+done
diff --git a/bash/virt-p2v-make-disk b/bash/virt-p2v-make-disk
new file mode 100644
index 0000000..6a879a1
--- /dev/null
+++ b/bash/virt-p2v-make-disk
@@ -0,0 +1,59 @@
+# virt-p2v-make-disk bash completion script -*- shell-script -*-
+# Copyright (C) 2014-2019 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+_p2v_options_only ()
+{
+    local cur prev words cword split
+    local shortopts longopts tool="$1"
+
+    _init_completion -s || return
+
+    case "$cur" in
+        --*)
+            # --options
+            longopts="$($tool --long-options)"
+            COMPREPLY=( $(compgen -W "$longopts" -- "$cur") )
+            return ;;
+        -*)
+            # -o and --options
+            shortopts="$($tool --short-options)"
+            longopts="$($tool --long-options)"
+            COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") )
+            return ;;
+        *)
+            COMPREPLY=( $(compgen "$cur") )
+            return ;;
+    esac
+}
+
+_virt_p2v_make_disk ()
+{
+    _p2v_options_only "virt-p2v-make-disk"
+} &&
+complete -o default -F _virt_p2v_make_disk virt-p2v-make-disk
+
+_virt_p2v_make_kickstart ()
+{
+    _p2v_options_only "virt-p2v-make-kickstart"
+} &&
+complete -o default -F _virt_p2v_make_kickstart virt-p2v-make-kickstart
+
+_virt_p2v_make_kiwi ()
+{
+    _p2v_options_only "virt-p2v-make-kiwi"
+} &&
+complete -o default -F _virt_p2v_make_kiwi virt-p2v-make-kiwi
diff --git a/configure.ac b/configure.ac
index dbd46cb..aeb50a6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -78,6 +78,10 @@ dnl Any C libraries required by virt-p2v.
 HEADING([Checking for libraries used by virt-p2v])
 m4_include([m4/p2v-libraries.m4])
 
+dnl Bash completion.
+HEADING([Checking for bash completion])
+m4_include([m4/p2v-bash-completion.m4])
+
 dnl All the programs used for tests.
 HEADING([Checking for programs for tests])
 m4_include([m4/p2v-tests.m4])
@@ -100,6 +104,7 @@ AC_CONFIG_FILES([run],
                 [chmod +x,-w run])
 
 AC_CONFIG_FILES([Makefile
+                 bash/Makefile
                  gnulib/lib/Makefile
                  gnulib/tests/Makefile])
 
diff --git a/m4/p2v-bash-completion.m4 b/m4/p2v-bash-completion.m4
new file mode 100644
index 0000000..eee5067
--- /dev/null
+++ b/m4/p2v-bash-completion.m4
@@ -0,0 +1,29 @@
+# libguestfs
+# Copyright (C) 2009-2019 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+dnl Bash completion.
+PKG_CHECK_MODULES([BASH_COMPLETION], [bash-completion >= 2.0], [
+    bash_completion=yes
+    AC_MSG_CHECKING([for bash-completions directory])
+    BASH_COMPLETIONS_DIR="`pkg-config --variable=completionsdir bash-completion`"
+    AC_MSG_RESULT([$BASH_COMPLETIONS_DIR])
+    AC_SUBST([BASH_COMPLETIONS_DIR])
+],[
+    bash_completion=no
+    AC_MSG_WARN([bash-completion not installed])
+])
+AM_CONDITIONAL([HAVE_BASH_COMPLETION],[test "x$bash_completion" = "xyes"])
-- 
2.21.0




More information about the Libguestfs mailing list