[Pkg-xen-devel] [PATCH 10/13] tools/xl/bash-completion: improved completion

Hans van Kranenburg hans at knorrie.org
Sun Feb 10 23:42:08 GMT 2019


While requesting removal of xm command line completion in the Debian
bash-completion package, I ran into a bts about completion for xl:

  https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=768005

I like the proposed improved version that Adi Kriegisch provides in
there. It makes sure  we're back on the same advanced level of
completion as in the past, with xm.

This change should be sent upstream asap, so that we can drop this patch
again. However don't wait for that to happen and ship it already.

Closes: #768005
---
 tools/xl/bash-completion | 223 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 210 insertions(+), 13 deletions(-)

diff --git a/tools/xl/bash-completion b/tools/xl/bash-completion
index b7cd6b3992..1db0bdc48e 100644
--- a/tools/xl/bash-completion
+++ b/tools/xl/bash-completion
@@ -1,20 +1,217 @@
-# Copy this file to /etc/bash_completion.d/xl.sh
+# bash completion for xl                                   -*- shell-script -*-
+# this is the original bash completion script for xm modified for use with xl
+
+_xen_domain_names()
+{
+    COMPREPLY=( $( compgen -W "$( xl list 2>/dev/null | \
+        awk '!/Name|Domain-0/ { print $1 }'  )" -- "$cur" ) )
+}
+
+_xen_domain_ids()
+{
+    COMPREPLY=( $( compgen -W "$( xl list 2>/dev/null | \
+        awk '!/Name|Domain-0/ { print $2 }' )" -- "$cur" ) )
+}
 
 _xl()
 {
-	local IFS=$'\n,'
+    local cur prev words cword
+    _init_completion || return
 
-	local cur opts xl
-	COMPREPLY=()
-	cur="${COMP_WORDS[COMP_CWORD]}"
-	xl=xl
+    # TODO: _split_longopt
 
-	if [[ $COMP_CWORD == 1 ]] ; then
-		opts=`${xl} help 2>/dev/null | sed '1,4d' | awk '/^ [^ ]/ {print $1}' | sed 's/$/ ,/g'` && COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
-		return 0
-	fi
+    local args command commands options
 
-	return 0
-}
+    # command list is taken from xen-util-common's xl.sh command completion
+    commands=$(xl help 2>/dev/null | sed '1,4d' | awk '/^ [^ ]/ {print $1}' | tr '\n' ' ')
+
+    if [[ $cword -eq 1 ]] ; then
+        COMPREPLY=( $( compgen -W "$commands" -- "$cur" ) )
+    else
+        if [[ "$cur" == *=* ]]; then
+            prev=${cur/=*/}
+            cur=${cur/*=/}
+        fi
+
+        command=${words[1]}
+        if [[ "$cur" == -* ]]; then
+            # possible options for the command
+            case $command in
+                create)
+                    options='-c'
+                    ;;
+                dmesg)
+                    options='--clear'
+                    ;;
+                list)
+                    options='--long'
+                    ;;
+                reboot)
+                    options='-w -a'
+                    ;;
+                shutdown)
+                    options='-w -a -R -H'
+                    ;;
+                sched-credit)
+                    options='-d -w -c'
+                    ;;
+                block-list|network-list|vtpm-list|vnet-list)
+                    options='-l --long'
+                    ;;
+                getpolicy)
+                    options='--dumpxml'
+                    ;;
+                new)
+                    options='-h --help --help_config -q --quiet --path= -f=
+                        --defconfig= -F= --config= -b --dryrun -x --xmldryrun
+                        -s --skipdtd -p --paused -c --console_autoconnect'
+                    ;;
+            esac
+            COMPREPLY=( $( compgen -W "$options" -- "$cur" ) )
+        else
+            case $command in
+                console|destroy|domname|domid|list|mem-set|mem-max| \
+                pause|reboot|rename|shutdown|unpause|vcpu-list|vcpu-pin| \
+                vcpu-set|block-list|network-list|vtpm-list)
+                    _count_args
+                    case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                    esac
+                    ;;
+                migrate)
+                    _count_args
+                        case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                        3)
+                            _known_hosts_real -- "$cur"
+                            ;;
+                    esac
+                    ;;
+                restore|dry-run|vnet-create)
+                    _filedir
+                    ;;
+                save)
+                    _count_args
+                    case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                        3)
+                            _filedir
+                            ;;
+                    esac
+                    ;;
+                sysrq)
+                    _count_args
+                    case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                        3)
+                            COMPREPLY=( $( compgen -W "r s e i u b" \
+                                -- "$cur" ) )
+                            ;;
+                    esac
+                    ;;
+                block-attach)
+                    _count_args
+                    case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                        3)
+                            COMPREPLY=( $( compgen -W "phy: file:" \
+                                -- "$cur" ) )
+                            ;;
+                        5)
+                            COMPREPLY=( $( compgen -W "w r" -- "$cur" ) )
+                            ;;
+                        6)
+                            _xen_domain_names
+                            ;;
+                    esac
+                    ;;
+                block-detach)
+                    _count_args
+                    case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                        3)
+                            COMPREPLY=( $( compgen -W "$( xl block-list $prev \
+                                2>/dev/null | awk '!/Vdev/ { print $1 }' )" \
+                                -- "$cur" ) )
+                            ;;
+                    esac
+                    ;;
+                network-attach)
+                    _count_args
+                    case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                        *)
+                            COMPREPLY=( $( compgen -W "script= ip= mac= bridge=
+                                backend=" -- "$cur" ) )
+                            ;;
+                    esac
+                    ;;
+                network-detach)
+                    _count_args
+                    case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                        3)
+                            COMPREPLY=( $(compgen -W "$( xl network-list $prev \
+                                2>/dev/null | awk '!/Idx/ { print $1 }' )" \
+                                -- "$cur" ) )
+                            ;;
+                    esac
+                    ;;
+                sched-credit)
+                    case $prev in
+                        -d)
+                            _xen_domain_names
+                            return
+                            ;;
+                    esac
+                    ;;
+                create)
+                    # we need to give the full path to the config file and
+                    # do not want to complete any other file outside /etc/xen
+                    _filedir
+                    COMPREPLY+=( \
+                        $( compgen -W '$( command ls /etc/xen/*.cfg 2>/dev/null )' \
+                        -- "$cur" ) )
+                    ;;
+                new)
+                    case $prev in
+                        -f|-F|--defconfig|--config)
+                            _filedir
+                            return
+                            ;;
+                        --path)
+                            _filedir -d
+                            return
+                            ;;
+                    esac
+
+                    _count_args
+                        case $args in
+                        2)
+                            _xen_domain_names
+                            ;;
+                    esac
+                    ;;
+            esac
+        fi
+    fi
+} &&
+complete -F _xl xl
 
-complete -F _xl -o nospace -o default xl
+# ex: filetype=sh
-- 
2.20.1




More information about the Pkg-xen-devel mailing list