[lxc-devel] [PATCH] Add bash auto completion

Stéphane Graber stgraber at ubuntu.com
Tue Jan 21 17:06:05 UTC 2014


This adds a basic bash auto-completion profile.

It supports 3 things at this time:
 - Auto-complete of container name (-n or -o)
 - Auto-complete of template name (-t)
 - Auto-complete of state names (-s)

It's configured in a way to be as little disruptive as possible, any
argument that's not explicitly handled by the profile will fallack to
bash's default completion.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 config/Makefile.am      |   2 +-
 config/bash/Makefile.am |  14 +++++++
 config/bash/lxc.in      | 105 ++++++++++++++++++++++++++++++++++++++++++++++++
 configure.ac            |   9 +++++
 4 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 config/bash/Makefile.am
 create mode 100644 config/bash/lxc.in

diff --git a/config/Makefile.am b/config/Makefile.am
index f9ce6fb..9515965 100644
--- a/config/Makefile.am
+++ b/config/Makefile.am
@@ -1 +1 @@
-SUBDIRS = apparmor etc init templates
+SUBDIRS = apparmor bash etc init templates
diff --git a/config/bash/Makefile.am b/config/bash/Makefile.am
new file mode 100644
index 0000000..b1768c9
--- /dev/null
+++ b/config/bash/Makefile.am
@@ -0,0 +1,14 @@
+EXTRA_DIST = lxc
+
+if ENABLE_BASH
+install-bash:
+	$(MKDIR_P) $(DESTDIR)$(sysconfdir)/bash_completion.d/
+	$(INSTALL_DATA) lxc $(DESTDIR)$(sysconfdir)/bash_completion.d/
+
+uninstall-bash:
+	rm -f $(DESTDIR)$(sysconfdir)/bash_completion.d/lxc
+	rmdir $(DESTDIR)$(sysconfdir)/bash_completion.d/ || :
+
+install-data-local: install-bash
+uninstall-local: uninstall-bash
+endif
diff --git a/config/bash/lxc.in b/config/bash/lxc.in
new file mode 100644
index 0000000..c1c9041
--- /dev/null
+++ b/config/bash/lxc.in
@@ -0,0 +1,105 @@
+#!bash
+
+have lxc-start && {
+    _lxc_names() {
+        COMPREPLY=( $( compgen -W "$( lxc-ls )" "$cur" ) )
+    }
+
+    _lxc_states() {
+        COMPREPLY=( $( compgen -W "STOPPED STARTING RUNNING STOPPING ABORTING FREEZING FROZEN THAWED" "$cur" ) )
+    }
+
+    _lxc_templates() {
+        COMPREPLY=( $( compgen -W "$(ls @LXCTEMPLATEDIR@/ | sed -e 's|^lxc-||' )" "$cur" ) )
+    }
+
+    _lxc-generic-n() {
+        local cur prev
+
+        COMPREPLY=()
+        _get_comp_words_by_ref cur prev
+
+        case $prev in
+            -n)
+                _lxc_names "$cur"
+                return 0
+            ;;
+        esac
+
+        return 1
+    }
+
+    _lxc-generic-ns() {
+        local cur prev
+
+        COMPREPLY=()
+        _get_comp_words_by_ref cur prev
+
+        case $prev in
+            -n)
+                _lxc_names "$cur"
+                return 0
+            ;;
+
+            -s)
+                _lxc_states "$cur"
+                return 0
+            ;;
+        esac
+
+        return 1
+    }
+
+    _lxc-generic-t() {
+        local cur prev
+
+        COMPREPLY=()
+        _get_comp_words_by_ref cur prev
+
+        case $prev in
+            -t)
+                _lxc_templates "$cur"
+                return 0
+            ;;
+        esac
+
+        return 1
+    }
+
+    _lxc-generic-o() {
+        local cur prev
+
+        COMPREPLY=()
+        _get_comp_words_by_ref cur prev
+
+        case $prev in
+            -o)
+                _lxc_names "$cur"
+                return 0
+            ;;
+        esac
+
+        return 1
+    }
+
+    complete -o default -F _lxc-generic-n lxc-attach
+    complete -o default -F _lxc-generic-n lxc-cgroup
+    complete -o default -F _lxc-generic-n lxc-console
+    complete -o default -F _lxc-generic-n lxc-destroy
+    complete -o default -F _lxc-generic-n lxc-device
+    complete -o default -F _lxc-generic-n lxc-execute
+    complete -o default -F _lxc-generic-n lxc-freeze
+    complete -o default -F _lxc-generic-n lxc-info
+    complete -o default -F _lxc-generic-n lxc-monitor
+    complete -o default -F _lxc-generic-n lxc-snapshot
+    complete -o default -F _lxc-generic-n lxc-start
+    complete -o default -F _lxc-generic-n lxc-stop
+    complete -o default -F _lxc-generic-n lxc-unfreeze
+
+    complete -o default -F _lxc-generic-ns lxc-wait
+
+    complete -o default -F _lxc-generic-t lxc-create
+
+    complete -o default -F _lxc-generic-o lxc-clone
+    complete -o default -F _lxc-generic-o lxc-start-ephemeral
+}
diff --git a/configure.ac b/configure.ac
index 4179dcf..aad5760 100644
--- a/configure.ac
+++ b/configure.ac
@@ -385,6 +385,12 @@ AM_COND_IF([ENABLE_LUA],
 		[LUA_INSTALL_LMOD=$datadir/lua/$LUA_VERSION])
 	])
 
+# Optional bash integration
+AC_ARG_ENABLE([bash],
+	[AC_HELP_STRING([--enable-bash], [build bash integration [default=yes]])],
+	[], [enable_bash=yes])
+AM_CONDITIONAL([ENABLE_BASH], [test "x$enable_bash" = "xyes"])
+
 # Optional test binaries
 AC_ARG_ENABLE([tests],
 	[AC_HELP_STRING([--enable-tests], [build test/example binaries [default=no]])],
@@ -548,6 +554,8 @@ AC_CONFIG_FILES([
 
 	config/Makefile
 	config/apparmor/Makefile
+	config/bash/Makefile
+	config/bash/lxc
 	config/init/Makefile
 	config/init/sysvinit/Makefile
 	config/init/systemd/Makefile
@@ -701,6 +709,7 @@ Environment:
  - init script type(s): $init_script
  - rpath: $enable_rpath
  - GnuTLS: $enable_gnutls
+ - Bash integration: $enable_bash
 
 Security features:
  - Apparmor: $enable_apparmor
-- 
1.8.5.3



More information about the lxc-devel mailing list