[PATCH 0/3] virsh: Add some completers

Add some basic completers Amneesh Singh (3): virsh: Provide completer for virtualization types virsh: Provide completer for CPU architectures virsh: Provide completer for PCI backend drivers src/util/virarch.c | 2 +- src/util/virarch.h | 2 +- tools/virsh-completer-host.c | 24 ++++++++++++++++++++++++ tools/virsh-completer-host.h | 10 ++++++++++ tools/virsh-completer-nodedev.c | 11 +++++++++++ tools/virsh-completer-nodedev.h | 4 ++++ tools/virsh-host.c | 8 ++++++++ tools/virsh-nodedev.c | 2 ++ 8 files changed, 61 insertions(+), 2 deletions(-) -- 2.35.1

Related: https://gitlab.com/libvirt/libvirt/-/issues/9 Signed-off-by: Amneesh Singh <natto@weirdnatto.in> --- tools/virsh-completer-host.c | 12 ++++++++++++ tools/virsh-completer-host.h | 5 +++++ tools/virsh-host.c | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c index cbdc3f0..14c68d3 100644 --- a/tools/virsh-completer-host.c +++ b/tools/virsh-completer-host.c @@ -27,6 +27,7 @@ #include "virxml.h" #include "virutil.h" #include "virsh-host.h" +#include "conf/domain_conf.h" static char * virshPagesizeNodeToString(xmlNodePtr node) @@ -180,3 +181,14 @@ virshNodeSuspendTargetCompleter(vshControl *ctl G_GNUC_UNUSED, return virshEnumComplete(VIR_NODE_SUSPEND_TARGET_LAST, virshNodeSuspendTargetTypeToString); } + +char ** +virshVirtTypeCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + virCheckFlags(0, NULL); + + return virshEnumComplete(VIR_DOMAIN_VIRT_LAST, + virDomainVirtTypeToString); +} diff --git a/tools/virsh-completer-host.h b/tools/virsh-completer-host.h index e71ccff..372ac14 100644 --- a/tools/virsh-completer-host.h +++ b/tools/virsh-completer-host.h @@ -41,3 +41,8 @@ char ** virshNodeSuspendTargetCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char ** +virshVirtTypeCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 1e83d19..664f9fd 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -77,6 +77,7 @@ static const vshCmdInfo info_domcapabilities[] = { static const vshCmdOptDef opts_domcapabilities[] = { {.name = "virttype", .type = VSH_OT_STRING, + .completer = virshVirtTypeCompleter, .help = N_("virtualization type (/domain/@type)"), }, {.name = "emulatorbin", @@ -559,6 +560,7 @@ static const vshCmdInfo info_maxvcpus[] = { static const vshCmdOptDef opts_maxvcpus[] = { {.name = "type", .type = VSH_OT_STRING, + .completer = virshVirtTypeCompleter, .help = N_("domain type") }, {.name = NULL} @@ -1577,6 +1579,7 @@ static const vshCmdOptDef opts_hypervisor_cpu_compare[] = { VIRSH_COMMON_OPT_FILE(N_("file containing an XML CPU description")), {.name = "virttype", .type = VSH_OT_STRING, + .completer = virshVirtTypeCompleter, .help = N_("virtualization type (/domain/@type)"), }, {.name = "emulator", @@ -1686,6 +1689,7 @@ static const vshCmdOptDef opts_hypervisor_cpu_baseline[] = { VIRSH_COMMON_OPT_FILE(N_("file containing XML CPU descriptions")), {.name = "virttype", .type = VSH_OT_STRING, + .completer = virshVirtTypeCompleter, .help = N_("virtualization type (/domain/@type)"), }, {.name = "emulator", -- 2.35.1

On 4/2/22 12:31, Amneesh Singh wrote:
Related: https://gitlab.com/libvirt/libvirt/-/issues/9 Signed-off-by: Amneesh Singh <natto@weirdnatto.in> --- tools/virsh-completer-host.c | 12 ++++++++++++ tools/virsh-completer-host.h | 5 +++++ tools/virsh-host.c | 4 ++++ 3 files changed, 21 insertions(+)
diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c index cbdc3f0..14c68d3 100644 --- a/tools/virsh-completer-host.c +++ b/tools/virsh-completer-host.c @@ -27,6 +27,7 @@ #include "virxml.h" #include "virutil.h" #include "virsh-host.h" +#include "conf/domain_conf.h"
static char * virshPagesizeNodeToString(xmlNodePtr node) @@ -180,3 +181,14 @@ virshNodeSuspendTargetCompleter(vshControl *ctl G_GNUC_UNUSED, return virshEnumComplete(VIR_NODE_SUSPEND_TARGET_LAST, virshNodeSuspendTargetTypeToString); } + +char **
Notice how the rest of the file uses two empty lines to separate functions? It's easier to spot the function you're looking for when scrolling through file that way.
+virshVirtTypeCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + virCheckFlags(0, NULL); + + return virshEnumComplete(VIR_DOMAIN_VIRT_LAST, + virDomainVirtTypeToString); +}
Michal

Related: https://gitlab.com/libvirt/libvirt/-/issues/9 Signed-off-by: Amneesh Singh <natto@weirdnatto.in> --- src/util/virarch.c | 2 +- src/util/virarch.h | 2 +- tools/virsh-completer-host.c | 12 ++++++++++++ tools/virsh-completer-host.h | 5 +++++ tools/virsh-host.c | 4 ++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/util/virarch.c b/src/util/virarch.c index 2134dd6..b1ea85e 100644 --- a/src/util/virarch.c +++ b/src/util/virarch.c @@ -123,7 +123,7 @@ virArchEndian virArchGetEndian(virArch arch) * * Return the string name of the architecture */ -const char *virArchToString(virArch arch) +const char *virArchToString(int arch) { if (arch >= VIR_ARCH_LAST) arch = VIR_ARCH_NONE; diff --git a/src/util/virarch.h b/src/util/virarch.h index 81b1b27..7daa0f1 100644 --- a/src/util/virarch.h +++ b/src/util/virarch.h @@ -110,7 +110,7 @@ typedef enum { unsigned int virArchGetWordSize(virArch arch); virArchEndian virArchGetEndian(virArch arch); -const char *virArchToString(virArch arch); +const char *virArchToString(int arch); virArch virArchFromString(const char *name); virArch virArchFromHost(void); diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c index 14c68d3..67a7687 100644 --- a/tools/virsh-completer-host.c +++ b/tools/virsh-completer-host.c @@ -27,6 +27,7 @@ #include "virxml.h" #include "virutil.h" #include "virsh-host.h" +#include "virarch.h" #include "conf/domain_conf.h" static char * @@ -192,3 +193,14 @@ virshVirtTypeCompleter(vshControl *ctl G_GNUC_UNUSED, return virshEnumComplete(VIR_DOMAIN_VIRT_LAST, virDomainVirtTypeToString); } + +char ** +virshArchCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + virCheckFlags(0, NULL); + + return virshEnumComplete(VIR_ARCH_LAST, + virArchToString); +} diff --git a/tools/virsh-completer-host.h b/tools/virsh-completer-host.h index 372ac14..b34163c 100644 --- a/tools/virsh-completer-host.h +++ b/tools/virsh-completer-host.h @@ -46,3 +46,8 @@ char ** virshVirtTypeCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char ** +virshArchCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 664f9fd..8f94d2a 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -86,6 +86,7 @@ static const vshCmdOptDef opts_domcapabilities[] = { }, {.name = "arch", .type = VSH_OT_STRING, + .completer = virshArchCompleter, .help = N_("domain architecture (/domain/os/type/@arch)"), }, {.name = "machine", @@ -1312,6 +1313,7 @@ static const vshCmdInfo info_cpu_models[] = { static const vshCmdOptDef opts_cpu_models[] = { {.name = "arch", .type = VSH_OT_DATA, + .completer = virshArchCompleter, .flags = VSH_OFLAG_REQ, .help = N_("architecture") }, @@ -1588,6 +1590,7 @@ static const vshCmdOptDef opts_hypervisor_cpu_compare[] = { }, {.name = "arch", .type = VSH_OT_STRING, + .completer = virshArchCompleter, .help = N_("CPU architecture (/domain/os/type/@arch)"), }, {.name = "machine", @@ -1698,6 +1701,7 @@ static const vshCmdOptDef opts_hypervisor_cpu_baseline[] = { }, {.name = "arch", .type = VSH_OT_STRING, + .completer = virshArchCompleter, .help = N_("CPU architecture (/domain/os/type/@arch)"), }, {.name = "machine", -- 2.35.1

On 4/2/22 12:31, Amneesh Singh wrote:
Related: https://gitlab.com/libvirt/libvirt/-/issues/9 Signed-off-by: Amneesh Singh <natto@weirdnatto.in> --- src/util/virarch.c | 2 +- src/util/virarch.h | 2 +- tools/virsh-completer-host.c | 12 ++++++++++++ tools/virsh-completer-host.h | 5 +++++ tools/virsh-host.c | 4 ++++ 5 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/src/util/virarch.c b/src/util/virarch.c index 2134dd6..b1ea85e 100644 --- a/src/util/virarch.c +++ b/src/util/virarch.c @@ -123,7 +123,7 @@ virArchEndian virArchGetEndian(virArch arch) * * Return the string name of the architecture */ -const char *virArchToString(virArch arch) +const char *virArchToString(int arch)
I think instead of changing this type (which should go into a separate patch anyway) ...
{ if (arch >= VIR_ARCH_LAST) arch = VIR_ARCH_NONE; diff --git a/src/util/virarch.h b/src/util/virarch.h index 81b1b27..7daa0f1 100644 --- a/src/util/virarch.h +++ b/src/util/virarch.h @@ -110,7 +110,7 @@ typedef enum {
unsigned int virArchGetWordSize(virArch arch); virArchEndian virArchGetEndian(virArch arch); -const char *virArchToString(virArch arch); +const char *virArchToString(int arch); virArch virArchFromString(const char *name);
virArch virArchFromHost(void); diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c index 14c68d3..67a7687 100644 --- a/tools/virsh-completer-host.c +++ b/tools/virsh-completer-host.c @@ -27,6 +27,7 @@ #include "virxml.h" #include "virutil.h" #include "virsh-host.h" +#include "virarch.h" #include "conf/domain_conf.h"
static char * @@ -192,3 +193,14 @@ virshVirtTypeCompleter(vshControl *ctl G_GNUC_UNUSED, return virshEnumComplete(VIR_DOMAIN_VIRT_LAST, virDomainVirtTypeToString); } + +char ** +virshArchCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + virCheckFlags(0, NULL); + + return virshEnumComplete(VIR_ARCH_LAST, + virArchToString);
.. this callback can be typecasted. The reason is that virarch.c uses virArch for type of variables instead of int. I worry that switching to plain int may tempt to use int for variables too in which case we'd lose this nice feature of compilers: switch (var) warns you if var is of an enum type, but it doesn't if it's a plain int. Michal

Related: https://gitlab.com/libvirt/libvirt/-/issues/9 Signed-off-by: Amneesh Singh <natto@weirdnatto.in> --- tools/virsh-completer-nodedev.c | 11 +++++++++++ tools/virsh-completer-nodedev.h | 4 ++++ tools/virsh-nodedev.c | 2 ++ 3 files changed, 17 insertions(+) diff --git a/tools/virsh-completer-nodedev.c b/tools/virsh-completer-nodedev.c index bf6e809..58f0a9d 100644 --- a/tools/virsh-completer-nodedev.c +++ b/tools/virsh-completer-nodedev.c @@ -23,6 +23,7 @@ #include "virsh-completer-nodedev.h" #include "virsh-util.h" #include "conf/node_device_conf.h" +#include "conf/domain_conf.h" #include "viralloc.h" #include "virsh-nodedev.h" #include "virsh.h" @@ -102,3 +103,13 @@ virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, return virshCommaStringListComplete(cap_str, (const char **)tmp); } + +char **virshNodeDevicePCIBackendCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + virCheckFlags(0, NULL); + + return virshEnumComplete(VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST, + virDomainHostdevSubsysPCIBackendTypeToString); +} diff --git a/tools/virsh-completer-nodedev.h b/tools/virsh-completer-nodedev.h index 743f7b2..231b4c2 100644 --- a/tools/virsh-completer-nodedev.h +++ b/tools/virsh-completer-nodedev.h @@ -36,3 +36,7 @@ char ** virshNodeDeviceCapabilityNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char **virshNodeDevicePCIBackendCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c index d01bdf6..d21b2ba 100644 --- a/tools/virsh-nodedev.c +++ b/tools/virsh-nodedev.c @@ -19,6 +19,7 @@ */ #include <config.h> +#include "virsh-completer-nodedev.h" #include "virsh-nodedev.h" #include "virsh-util.h" @@ -613,6 +614,7 @@ static const vshCmdOptDef opts_node_device_detach[] = { }, {.name = "driver", .type = VSH_OT_STRING, + .completer = virshNodeDevicePCIBackendCompleter, .help = N_("pci device assignment backend driver (e.g. 'vfio' or 'kvm')") }, {.name = NULL} -- 2.35.1

On 4/2/22 12:31, Amneesh Singh wrote:
Related: https://gitlab.com/libvirt/libvirt/-/issues/9 Signed-off-by: Amneesh Singh <natto@weirdnatto.in> --- tools/virsh-completer-nodedev.c | 11 +++++++++++ tools/virsh-completer-nodedev.h | 4 ++++ tools/virsh-nodedev.c | 2 ++ 3 files changed, 17 insertions(+)
diff --git a/tools/virsh-completer-nodedev.c b/tools/virsh-completer-nodedev.c index bf6e809..58f0a9d 100644 --- a/tools/virsh-completer-nodedev.c +++ b/tools/virsh-completer-nodedev.c @@ -23,6 +23,7 @@ #include "virsh-completer-nodedev.h" #include "virsh-util.h" #include "conf/node_device_conf.h" +#include "conf/domain_conf.h" #include "viralloc.h" #include "virsh-nodedev.h" #include "virsh.h" @@ -102,3 +103,13 @@ virshNodeDeviceCapabilityNameCompleter(vshControl *ctl,
return virshCommaStringListComplete(cap_str, (const char **)tmp); } + +char **virshNodeDevicePCIBackendCompleter(vshControl *ctl G_GNUC_UNUSED, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags)
Formatting.
+{ + virCheckFlags(0, NULL); + + return virshEnumComplete(VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST, + virDomainHostdevSubsysPCIBackendTypeToString);
Initially I wanted to suggest using virPCIStubDriver and virPCIStubDriverTypeToString, but that's not really how virDomainDriverNodeDeviceDetachFlags() translates the string either. So your code is okay. Michal

On 4/2/22 12:31, Amneesh Singh wrote:
Add some basic completers
Amneesh Singh (3): virsh: Provide completer for virtualization types virsh: Provide completer for CPU architectures virsh: Provide completer for PCI backend drivers
src/util/virarch.c | 2 +- src/util/virarch.h | 2 +- tools/virsh-completer-host.c | 24 ++++++++++++++++++++++++ tools/virsh-completer-host.h | 10 ++++++++++ tools/virsh-completer-nodedev.c | 11 +++++++++++ tools/virsh-completer-nodedev.h | 4 ++++ tools/virsh-host.c | 8 ++++++++ tools/virsh-nodedev.c | 2 ++ 8 files changed, 61 insertions(+), 2 deletions(-)
I'm fixing all the small nits I've raised in my review and pushing. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Congratulations on your first libvirt contribution! Michal
participants (2)
-
Amneesh Singh
-
Michal Prívozník