[libvirt] [PATCH 00/11] Make auto completion better

Michal Privoznik posted 11 patches 6 years, 5 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/cover.1510055948.git.mprivozn@redhat.com
There is a newer version of this series
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
[libvirt] [PATCH 00/11] Make auto completion better
Posted by Michal Privoznik 6 years, 5 months ago
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

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 00/11] Make auto completion better
Posted by Martin Kletzander 6 years, 5 months ago
On Tue, Nov 07, 2017 at 01:22:48PM +0100, Michal Privoznik wrote:
>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>
>

So if I do this, it works, but if I copy it where it is supposed to be installed
it doesn't for some reason.  I haven't dug into that, I don't even know where to
start, probably.  Also it is very possible that it's my fault.

I also found one more thing, but that's only easy to fix in few cases.  If you
type 'virsh start --domain fedora <TAB><TAB>' then one of the strings it offers
is '--domain'.  It'd be nice to have that filtered out.  Not needed for ACK,
though.  Just an idea for you.

Apart from that it looks sane, so ACK to the general concept, I would just
change 2/11 as written in the reply, drop (or completely replace) 3/11 and
instead do the '-qq' thing.  And of course adjust other code depending on that.
I'd ACK the rest, I would just like to have the middle-of-the-command completion
working too so it doesn't confuse people.
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list