2017-11-02 13:41:53 +00:00
|
|
|
#
|
|
|
|
# virsh & virt-admin completion command
|
|
|
|
#
|
|
|
|
|
|
|
|
_vsh_complete()
|
|
|
|
{
|
|
|
|
local words cword c=0 i=0 cur RO URI CMDLINE INPUT A
|
|
|
|
|
|
|
|
# Here, $COMP_WORDS is an array of words on the bash
|
|
|
|
# command line that user wants to complete. However, when
|
|
|
|
# parsing command line, the default set of word breaks is
|
|
|
|
# applied. This doesn't work for us as it mangles libvirt
|
|
|
|
# arguments, e.g. connection URI (with the default set it's
|
|
|
|
# split into multiple items within the array). Fortunately,
|
|
|
|
# there's a fixup function for the array.
|
|
|
|
_get_comp_words_by_ref -n "\"'><=;|&(:" -w words -i cword
|
|
|
|
COMP_WORDS=( "${words[@]}" )
|
|
|
|
COMP_CWORD=${cword}
|
|
|
|
cur=${COMP_WORDS[$COMP_CWORD]}
|
|
|
|
|
|
|
|
# See what URI is user trying to connect to and if they are
|
|
|
|
# connecting RO. Honour that.
|
|
|
|
while [ $c -le $COMP_CWORD ]; do
|
|
|
|
word="${COMP_WORDS[c]}"
|
|
|
|
case "$word" in
|
|
|
|
-r|--readonly) RO=1 ;;
|
|
|
|
-c|--connect) c=$((++c)); URI=${COMP_WORDS[c]} ;;
|
|
|
|
*) if [ $c -ne 0 ] && [ $i -eq 0 ]; then i=$c; break; fi ;;
|
|
|
|
esac
|
|
|
|
c=$((++c))
|
|
|
|
done
|
|
|
|
|
|
|
|
CMDLINE=
|
|
|
|
if [ -n "${RO}" ]; then
|
|
|
|
CMDLINE="${CMDLINE} -r"
|
|
|
|
fi
|
|
|
|
if [ -n "${URI}" ]; then
|
|
|
|
CMDLINE="${CMDLINE} -c ${URI}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
INPUT=( "${COMP_WORDS[@]:$i:$COMP_CWORD}" )
|
2020-11-10 09:50:59 +00:00
|
|
|
INPUT[-1]=${INPUT[-1]//\\:/:}
|
2017-11-02 13:41:53 +00:00
|
|
|
|
|
|
|
# Uncomment these lines for easy debug.
|
|
|
|
# echo;
|
|
|
|
# echo "RO=${flag_ro}";
|
|
|
|
# echo "URI=${URI}";
|
|
|
|
# echo "CMDLINE=${CMDLINE}";
|
|
|
|
# echo "INPUT[${#INPUT[@]}]=**${INPUT[@]}**";
|
|
|
|
# echo "cur=${cur}";
|
|
|
|
# echo;
|
|
|
|
# return 0;
|
|
|
|
|
|
|
|
# Small shortcut here. According to manpage:
|
|
|
|
# When the function is executed, the first argument ($1) is
|
|
|
|
# the name of the command whose arguments are being
|
|
|
|
# completed.
|
|
|
|
# Therefore, we might just run $1.
|
tools: Set IFS for bash completion script
The way our bash completion string is that is gets user's input
and lets virsh completion code do all the work by calling 'virsh
complete -- $INPUT". The 'complete' command is a "secret",
unlisted command that exists solely for this purpose. After it
has done it's part, it prints candidates onto stdout, each
candidate on its own line, e.g. like this:
# virsh complete -- "net-u"
net-undefine
net-update
net-uuid
These strings are then stored into a bash array $A like this:
A=($($1 ${CMDLINE} complete -- "${INPUT[@]}" 2>/dev/null))
This array is then thrown back at bash completion to produce
desired output. So far so good. Except, when there is an option
with space. For instance:
# virsh complete -- start --domain ""
uefi\ duplicate
uefi
Bash interprets that as another array item because by default,
Internal Field Separator (IFS) = set of characters that bash uses
to split words at, is: space, TAB, newline. We don't want space
nor TAB. Therefore, we have to set $IFS when storing 'virsh
complete' output into the array.
Thanks to Peter who suggested it.
Resolves: https://gitlab.com/libvirt/libvirt/-/issues/116
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-25 12:48:31 +00:00
|
|
|
IFS=$'\n' A=($($1 ${CMDLINE} complete -- "${INPUT[@]}" 2>/dev/null))
|
2017-11-02 13:41:53 +00:00
|
|
|
|
|
|
|
COMPREPLY=($(compgen -W "${A[*]%--}" -- ${cur}))
|
|
|
|
__ltrim_colon_completions "$cur"
|
|
|
|
return 0
|
|
|
|
} &&
|
|
|
|
complete -o default -o filenames -F _vsh_complete virsh &&
|
|
|
|
complete -o default -o filenames -F _vsh_complete virt-admin
|
|
|
|
|
|
|
|
# vim: ft=sh:et:ts=4:sw=4:tw=80
|