fish: First pass at guestfish bash completion script.
authorRichard Jones <rjones@redhat.com>
Tue, 25 May 2010 19:13:55 +0000 (20:13 +0100)
committerRichard Jones <rjones@redhat.com>
Tue, 25 May 2010 19:13:55 +0000 (20:13 +0100)
fish/Makefile.am
fish/guestfish-bash-completion.sh [new file with mode: 0644]

index 9166f2d..bdb1acb 100644 (file)
@@ -1,5 +1,5 @@
 # libguestfs
-# Copyright (C) 2009 Red Hat Inc.
+# Copyright (C) 2009-2010 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
@@ -102,3 +102,8 @@ guestfish.1: guestfish.pod guestfish-actions.pod
          --name "guestfish" \
          --release "$(PACKAGE_NAME)-$(PACKAGE_VERSION)" \
          > $@
+
+# Bash completion script.
+
+bashcompletiondir = $(sysconfdir)/bash_completion.d
+bashcompletion_DATA = guestfish-bash-completion.sh
diff --git a/fish/guestfish-bash-completion.sh b/fish/guestfish-bash-completion.sh
new file mode 100644 (file)
index 0000000..243e72b
--- /dev/null
@@ -0,0 +1,90 @@
+# guestfish bash completion script
+# Copyright (C) 2010 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+# To use this script, copy it into /etc/bash_completion.d/ if
+# that directory exists.
+#
+# If your distro does not have that directory (or if you cannot or
+# do not want to install this in /etc) then you can copy this to
+# somewhere in your home directory such as
+# ~/.guestfish-bash-completion.sh and add this to your .bashrc:
+#   source ~/.guestfish-bash-completion.sh
+
+# This was "inspired" by the git bash completion script written by
+# Shawn O. Pearce.
+
+_guestfish_virsh_list ()
+{
+    local flag_ro=$1 flags
+
+    if [ "$flag_ro" -eq 1 ]; then
+        flags="--all"
+    else
+        flags="--inactive"
+    fi
+    virsh list $flags | head -n -1 | tail -n +3 | awk '{print $2}'
+}
+
+_guestfish ()
+{
+    local flag_i=0 flag_ro=0 c=1 word cmds doms
+
+    # See if user has specified -i option before the current word.
+    while [ $c -lt $COMP_CWORD ]; do
+        word="${COMP_WORDS[c]}"
+        case "$word" in
+            -i|--inspector) flag_i=1 ;;
+            -r|--ro) flag_ro=1 ;;
+        esac
+        c=$((++c))
+    done
+
+    # Now try to complete the current word.
+    word="${COMP_WORDS[COMP_CWORD]}"
+    case "$word" in
+        --*)
+            COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '
+                          --cmd-help
+                          --add
+                          --no-dest-paths
+                          --file
+                          --inspector
+                          --listen
+                          --mount
+                          --no-sync
+                          --new
+                          --remote
+                          --ro
+                          --selinux
+                          --verbose
+                          --version
+                        ' -- "$word")) ;;
+        *)
+            if [ "$flag_i" -eq 1 ]; then
+                doms=$(_guestfish_virsh_list "$flag_ro")
+                COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W "$doms" -- "$word"))
+            else
+                cmds=$(guestfish -h| head -n -1 | tail -n +2 | awk '{print $1}')
+                COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W "$cmds" -- "$word"))
+            fi ;;
+    esac
+}
+
+complete -o bashdefault -o default -F _guestfish guestfish 2>/dev/null \
+  || complete -o default -F _guestfish guestfish
+
+# EOF