virsh: Introduce --xpath and --wrap to capabilities

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>
Reviewed-by: Kristina Hanicova <khanicov@redhat.com>
This commit is contained in:
Michal Privoznik 2023-04-14 16:01:56 +02:00
parent edd604a672
commit 332385aa7e
2 changed files with 31 additions and 5 deletions

View File

@ -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
---------------

View File

@ -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
},