After initial RFC [1] I had some time to work on this and here is
the result.
What's implemented?
===================
Auto completion for both interactive and non-interactive
virsh/virt-admin.
Known limitations
=================
Currently, just options completers work. I mean, to bring up list
of domains you have to:
virsh # start --domain <TAB><TAB>
Doing bare:
virsh # start <TAB><TAB>
brings up list of --options. I am working on this meanwhile. But
one can argue that having to use --option is not that much of a
problem since we have good completion now.
The other known limitation - and actually room for others to join
and help - is missing completers. I mean, the last 3 patches give
an example how to do that. But that is still very few. We need
more completers.
How does this work?
===================
The basic idea is simple, when defining opts for command two new
struct members can be set:
{.name = "mac",
.type = VSH_OT_STRING,
+ .completer = virshDomainInterfaceCompleter,
+ .completer_flags = VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC,
.help = N_("MAC address")
},
@completer is the callback that is used when user wants to bring
up list of possible strings. @completer_flags can then be used to
refine return value of @completer. In this specific example,
virshDomainInterfaceCompleter() brings up list of interfaces for
given domain. It tries to return /interface/target/@dev but if
not found (e.g. because domain is not running),
/interface/mac/@address is returned otherwise. However, in one
specific case we are interested only in MAC addresses (regardless
of domain state) and thus VIRSH_DOMAIN_INTERFACE_COMPLETER_MAC
flag is specified which alters @completer behaviour.
For non-interactive virsh/virt-admin there's
tools/bash-completion/vsh script which does no more than calling:
$1 complete $USER_INPUT
(where $1 is name of the binary user intents to run).
How to test this?
=================
Interactive completion should work out of the box. Just make sure
you're connected. Completers don't connect! You certainly don't
want ssh's 'Password:' prompt show on <TAB><TAB>, do you?
Non-interactive completers do connect, but in order to avoid any
password prompts, /dev/stdin is closed before anything else is
done. In order to test it, just:
libvirt.git $ source tools/bash-completion/vsh
Now, bash completion should work:
libvirt.git $ ./tools/virsh -c qemu:///system start --domain <TAB><TAB>
Notes
=====
This is that part of vsh that nobody wants to touch, but with
these patches in we have the framework to just write small
functions (as can be seen in examples). User would benefit from
this!
As usual, you can find all the patches on my github [2].
Michal
1:
https://www.redhat.com/archives/libvir-list/2017-October/msg01374.html
2:
https://github.com/zippy2/libvirt/tree/bash_autocomplete
Michal Privoznik (11):
vshCommandStringParse: Allow retrieving partial result
vshCommandOpt: Relax check for valid options
vsh: Add @silent to vshConnectionHook
vsh: Fix vshCompleter signature
vsh: Call vshCmdOptDef.completer properly
vshCompleter: Pass partially parsed command
vsh: Introduce complete command
tools: Provide bash autompletion file
virsh: Introduce virshDomainNameCompleter
virsh: Introduce virshDomainInterfaceCompleter
virt-admin: Introduce vshAdmServerCompleter
configure.ac | 3 +
libvirt.spec.in | 2 +
m4/virt-bash-completion.m4 | 74 ++++++++++++++++
tools/Makefile.am | 40 ++++++++-
tools/bash-completion/vsh | 73 ++++++++++++++++
tools/virsh-completer.c | 150 +++++++++++++++++++++++++++++++++
tools/virsh-completer.h | 41 +++++++++
tools/virsh-domain-monitor.c | 33 ++++----
tools/virsh-domain.c | 188 +++++++++++++++++++++--------------------
tools/virsh-snapshot.c | 24 +++---
tools/virsh.c | 72 +++++++++-------
tools/virsh.h | 12 ++-
tools/virt-admin-completer.c | 76 +++++++++++++++++
tools/virt-admin-completer.h | 33 ++++++++
tools/virt-admin.c | 62 ++++++++------
tools/vsh.c | 195 ++++++++++++++++++++++++++++++++-----------
tools/vsh.h | 23 ++++-
17 files changed, 873 insertions(+), 228 deletions(-)
create mode 100644 m4/virt-bash-completion.m4
create mode 100644 tools/bash-completion/vsh
create mode 100644 tools/virsh-completer.c
create mode 100644 tools/virsh-completer.h
create mode 100644 tools/virt-admin-completer.c
create mode 100644 tools/virt-admin-completer.h
--
2.13.6