virsh: Allow listing just domain IDs

Some completers for libvirt related tools might want to list
domain IDs only. Just like the one I've implemented for
virt-viewer [1]. I've worked around it using some awk magic,
but if it was possible to just 'virsh list --id' then I could
drop awk.

1: https://www.redhat.com/archives/virt-tools-list/2019-May/msg00014.html

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Michal Privoznik 2019-05-13 10:04:27 +02:00
parent ee1a90242a
commit 0995f20d0a
2 changed files with 42 additions and 24 deletions

View File

@ -621,7 +621,7 @@ list
list [--inactive | --all]
[--managed-save] [--title]
{ [--table] | --name | --uuid }
{ [--table] | --name | --uuid | --id }
[--persistent] [--transient]
[--with-managed-save] [--without-managed-save]
[--autostart] [--no-autostart]
@ -758,16 +758,18 @@ If *--managed-save* is specified, then domains that have managed save state
in the listing. This flag is usable only with the default *--table* output.
Note that this flag does not filter the list of domains.
If *--name* is specified, domain names are printed instead of the table
formatted one per line. If *--uuid* is specified domain's UUID's are printed
instead of names. Flag *--table* specifies that the legacy table-formatted
output should be used. This is the default.
If both *--name* and *--uuid* are specified, domain UUID's and names
are printed side by side without any header. Flag *--table* specifies
that the legacy table-formatted output should be used. This is the
default if neither *--name* nor *--uuid* are specified. Option
*--table* is mutually exclusive with options *--uuid* and *--name*.
If *--name* is specified, domain names are printed instead of the
table formatted one per line. If *--uuid* is specified domain's UUID's
are printed instead of names. If *--id* is specified then domain's ID's
are printed indead of names. However, it is possible to combine
*--name*, *--uuid* and *--id* to select only desired fields for
printing. Flag *--table* specifies that the legacy table-formatted
output should be used, but it is mutually exclusive with *--name*,
*--uuid* and *--id*. This is the default and will be used if neither of
*--name*, *--uuid* or *--id* is specified. If neither *--name* nor *--uuid* is
specified, but *--id* is, then only active domains are listed, even with the
*--all* parameter as otherwise the output would just contain bunch of lines
with just *-1*.
If *--title* is specified, then the short domain description (title) is
printed in an extra column. This flag is usable only with the default

View File

@ -1919,6 +1919,10 @@ static const vshCmdOptDef opts_list[] = {
.type = VSH_OT_BOOL,
.help = N_("list domain names only")
},
{.name = "id",
.type = VSH_OT_BOOL,
.help = N_("list domain IDs only")
},
{.name = "table",
.type = VSH_OT_BOOL,
.help = N_("list table (default)")
@ -1945,6 +1949,7 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
bool optTable = vshCommandOptBool(cmd, "table");
bool optUUID = vshCommandOptBool(cmd, "uuid");
bool optName = vshCommandOptBool(cmd, "name");
bool optID = vshCommandOptBool(cmd, "id");
size_t i;
char *title;
char uuid[VIR_UUID_STRING_BUFLEN];
@ -1988,8 +1993,9 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
VSH_EXCLUSIVE_OPTIONS("table", "name");
VSH_EXCLUSIVE_OPTIONS("table", "uuid");
VSH_EXCLUSIVE_OPTIONS("table", "id");
if (!optUUID && !optName)
if (!optUUID && !optName && !optID)
optTable = true;
if (!(list = virshDomainListCollect(ctl, flags)))
@ -2007,6 +2013,8 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
}
for (i = 0; i < list->ndomains; i++) {
const char *sep = "";
dom = list->domains[i];
id = virDomainGetID(dom);
if (id != (unsigned int) -1)
@ -2044,20 +2052,28 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
goto cleanup;
}
} else if (optUUID && optName) {
if (virDomainGetUUIDString(dom, uuid) < 0) {
vshError(ctl, "%s", _("Failed to get domain's UUID"));
goto cleanup;
} else {
if (optUUID) {
if (virDomainGetUUIDString(dom, uuid) < 0) {
vshError(ctl, "%s", _("Failed to get domain's UUID"));
goto cleanup;
}
vshPrint(ctl, "%s", uuid);
sep = " ";
}
vshPrint(ctl, "%-36s %-30s\n", uuid, virDomainGetName(dom));
} else if (optUUID) {
if (virDomainGetUUIDString(dom, uuid) < 0) {
vshError(ctl, "%s", _("Failed to get domain's UUID"));
goto cleanup;
if (optID) {
/* If we are asked to print IDs only then do that
* only for live domains. */
if (id == (unsigned int) -1 && !optUUID && !optName)
continue;
vshPrint(ctl, "%s%s", sep, id_buf);
sep = " ";
}
vshPrint(ctl, "%s\n", uuid);
} else if (optName) {
vshPrint(ctl, "%s\n", virDomainGetName(dom));
if (optName) {
vshPrint(ctl, "%s%s", sep, virDomainGetName(dom));
sep = " ";
}
vshPrint(ctl, "\n");
}
}