[PATCH 0/2] virsh: Introduce --xpath and --wrap to (dom)capabilities

*** BLURB HERE *** Michal Prívozník (2): virsh: Introduce --xpath and --wrap to capabilities virsh: Introduce --xpath and --wrap to domcapabilities docs/manpages/virsh.rst | 17 ++++++++++++++- tools/virsh-host.c | 46 ++++++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 9 deletions(-) -- 2.39.2

Similarly to dumpxml, let's have --xpath and --wrap to the 'capabilities' command since users might be interested only in a subset of capabilities XML. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- docs/manpages/virsh.rst | 9 ++++++++- tools/virsh-host.c | 27 +++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 26c328d390..279e8e103e 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -541,7 +541,7 @@ capabilities :: - capabilities + capabilities [--xpath EXPRESSION] [--wrap] Print an XML document describing the capabilities of the hypervisor we are currently connected to. This includes a section on the host @@ -553,6 +553,13 @@ description see: The XML also show the NUMA topology information if available. +If the **--xpath** argument provides an XPath expression, it will be +evaluated against the output XML and only those matching nodes will +be printed. The default behaviour is to print each matching node as +a standalone document, however, for ease of additional processing, +the **--wrap** argument will cause the matching node to be wrapped +in a common root node. + domcapabilities --------------- diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 4df599afdf..1504eab0f1 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -32,6 +32,7 @@ #include "virstring.h" #include "virfile.h" #include "virenum.h" +#include "virsh-util.h" /* * "capabilities" command @@ -43,22 +44,40 @@ static const vshCmdInfo info_capabilities[] = { {.name = "desc", .data = N_("Returns capabilities of hypervisor/driver.") }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_capabilities[] = { + {.name = "xpath", + .type = VSH_OT_STRING, + .flags = VSH_OFLAG_REQ_OPT, + .completer = virshCompleteEmpty, + .help = N_("xpath expression to filter the XML document") + }, + {.name = "wrap", + .type = VSH_OT_BOOL, + .help = N_("wrap xpath results in an common root element"), + }, {.name = NULL} }; static bool -cmdCapabilities(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED) +cmdCapabilities(vshControl *ctl, const vshCmd *cmd) { g_autofree char *caps = NULL; virshControl *priv = ctl->privData; + bool wrap = vshCommandOptBool(cmd, "wrap"); + const char *xpath = NULL; + + if (vshCommandOptStringQuiet(ctl, cmd, "xpath", &xpath) < 0) + return false; if ((caps = virConnectGetCapabilities(priv->conn)) == NULL) { vshError(ctl, "%s", _("failed to get capabilities")); return false; } - vshPrint(ctl, "%s\n", caps); - return true; + return virshDumpXML(ctl, caps, "capabilities", xpath, wrap); } /* @@ -1785,7 +1804,7 @@ const vshCmdDef hostAndHypervisorCmds[] = { }, {.name = "capabilities", .handler = cmdCapabilities, - .opts = NULL, + .opts = opts_capabilities, .info = info_capabilities, .flags = 0 }, -- 2.39.2

Similarly to dumpxml, let's have --xpath and --wrap to the 'domcapabilities' command since users might be interested only in a subset of domcapabilities XML. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- docs/manpages/virsh.rst | 8 ++++++++ tools/virsh-host.c | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 279e8e103e..c3014f7b06 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -569,6 +569,7 @@ domcapabilities :: domcapabilities [virttype] [emulatorbin] [arch] [machine] + [--xpath EXPRESSION] [--wrap] Print an XML document describing the domain capabilities for the @@ -603,6 +604,13 @@ supplied along with either the *emulatorbin* or *arch* in order to generate output for the default *machine*. Supplying a *machine* value will generate output for the specific machine. +If the **--xpath** argument provides an XPath expression, it will be +evaluated against the output XML and only those matching nodes will +be printed. The default behaviour is to print each matching node as +a standalone document, however, for ease of additional processing, +the **--wrap** argument will cause the matching node to be wrapped +in a common root node. + pool-capabilities ----------------- diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 1504eab0f1..0bda327cae 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -112,6 +112,16 @@ static const vshCmdOptDef opts_domcapabilities[] = { .type = VSH_OT_STRING, .help = N_("machine type (/domain/os/type/@machine)"), }, + {.name = "xpath", + .type = VSH_OT_STRING, + .flags = VSH_OFLAG_REQ_OPT, + .completer = virshCompleteEmpty, + .help = N_("xpath expression to filter the XML document") + }, + {.name = "wrap", + .type = VSH_OT_BOOL, + .help = N_("wrap xpath results in an common root element"), + }, {.name = NULL} }; @@ -123,13 +133,16 @@ cmdDomCapabilities(vshControl *ctl, const vshCmd *cmd) const char *emulatorbin = NULL; const char *arch = NULL; const char *machine = NULL; + const char *xpath = NULL; const unsigned int flags = 0; /* No flags so far */ + bool wrap = vshCommandOptBool(cmd, "wrap"); virshControl *priv = ctl->privData; if (vshCommandOptStringReq(ctl, cmd, "virttype", &virttype) < 0 || vshCommandOptStringReq(ctl, cmd, "emulatorbin", &emulatorbin) < 0 || vshCommandOptStringReq(ctl, cmd, "arch", &arch) < 0 || - vshCommandOptStringReq(ctl, cmd, "machine", &machine) < 0) + vshCommandOptStringReq(ctl, cmd, "machine", &machine) < 0 || + vshCommandOptStringQuiet(ctl, cmd, "xpath", &xpath) < 0) return false; caps = virConnectGetDomainCapabilities(priv->conn, emulatorbin, @@ -139,9 +152,7 @@ cmdDomCapabilities(vshControl *ctl, const vshCmd *cmd) return false; } - vshPrint(ctl, "%s\n", caps); - - return true; + return virshDumpXML(ctl, caps, "domcapabilities", xpath, wrap); } /* -- 2.39.2

On Fri, Apr 14, 2023 at 4:23 PM Michal Privoznik <mprivozn@redhat.com> wrote:
*** BLURB HERE ***
Michal Prívozník (2): virsh: Introduce --xpath and --wrap to capabilities virsh: Introduce --xpath and --wrap to domcapabilities
docs/manpages/virsh.rst | 17 ++++++++++++++- tools/virsh-host.c | 46 ++++++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 9 deletions(-)
Reviewed-by: Kristina Hanicova <khanicov@redhat.com> Kristina
participants (2)
-
Kristina Hanicova
-
Michal Privoznik