[libvirt] PATCH: Bash auto-completion for virsh

Daniel P. Berrange berrange at redhat.com
Mon Jul 14 11:32:52 UTC 2008


On Mon, Jul 14, 2008 at 11:57:07AM +0100, Daniel P. Berrange wrote:
> Everyone[1] seems to be doing funky bash auto-completion for commands these
> days, so I thought I'd make a stab at doing something for virsh.
> 
> First of all I needed the command line help in an easier format to deal
> with, so I've added

This is an additive patch to make it preserve the '--connect URI' arg
when auto-completing domains, network and storage pool objects, instead
of using the default connection URI. It will explicitly disable this
completion if its a remote URI - ie one with a hostname, since we don't
want the completion function prompting for auth!

Daniel

diff -r 63b8398c302e src/virsh.bash
--- a/src/virsh.bash	Mon Jul 14 12:18:23 2008 +0100
+++ b/src/virsh.bash	Mon Jul 14 12:29:44 2008 +0100
@@ -1,7 +1,7 @@
 
 _virsh()
 {
-    local cur prev cmd cmd_index arg arg_index i results
+    local cur prev cmd cmd_index arg arg_index i results skipconn
     local virsh="$1"
     local canonical=0
 
@@ -10,14 +10,19 @@
     prev="$3"
 
     if [[ $COMP_CWORD > 1 && ${COMP_WORDS[COMP_CWORD-1]} = '--connect' ]]; then
-        results="$("$virsh" _complete-uri)" || nets=""
+        results="$($virsh _complete-uri)" || nets=""
     fi
 
+    skipconn=0
     if [ -z "$results" ]; then
         # searching for the command name
         for ((i=1; $i<=$COMP_CWORD; i++)); do
             if [[ ${COMP_WORDS[i]} != -* ]]; then
-                if [[ ${COMP_WORDS[i-1]} != '--connect' ]]; then
+                if [[ ${COMP_WORDS[i-1]} = '--connect' ]]; then
+                    echo "${COMP_WORDS[i]}" | grep '///' >/dev/null 2>&1
+                    test "$?" = 1 && skipconn=1
+                    test "$skipconn" = 0 && virsh="$virsh ${COMP_WORDS[i-1]} ${COMP_WORDS[i]}"
+                else
                     if [[ $i < $COMP_CWORD ]]; then
                        cmd="${COMP_WORDS[i]}"
                        cmd_index=$i
@@ -30,15 +35,15 @@
 
         if [[ "$cur" == -* ]]; then
             # Generate args - global or command specific
-            results="$("$virsh" _complete-command options "$cmd")"
+            results="$($virsh _complete-command options "$cmd")"
         else
             if [ -z "$cmd" ]; then
                 # No command set, so generate list of all commands
-                results="$("$virsh" _complete-command commands)" || commands=""
+                results="$($virsh _complete-command commands)" || commands=""
             else
                 # Command set, to generate command specific args
                 n=0
-                for i in "$("$virsh" _complete-command arguments "$cmd")"
+                for i in "$($virsh _complete-command arguments "$cmd")"
                 do
                     n=`expr $n + 1`
                     if [ $n = $arg_index ]; then
@@ -53,19 +58,19 @@
                     ;;
 
                   domain)
-                    results="$("$virsh" _complete-domain)" || doms=""
+                    test "$skipconn" = 0 && results="$($virsh _complete-domain)" || doms=""
                     ;;
 
                   network)
-                    results="$("$virsh" _complete-network)" || nets=""
+                    test "$skipconn" = 0 && results="$($virsh _complete-network)" || nets=""
                     ;;
 
                   pool)
-                    results="$("$virsh" _complete-pool)" || nets=""
+                    test "$skipconn" = 0 && results="$($virsh _complete-pool)" || nets=""
                     ;;
 
                   uri)
-                    results="$("$virsh" _complete-uri)" || nets=""
+                    results="$($virsh _complete-uri)" || nets=""
                     ;;
                 esac
             fi

-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|




More information about the libvir-list mailing list