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(a)redhat.com>
---
I just realized that I've never sent this and it was sitting in a
branch for 1.5 years. <facepalm/>
tools/virsh-domain-monitor.c | 41 ++++++++++++++++++++++++------------
1 file changed, 28 insertions(+), 13 deletions(-)
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index e0491d48ac..9e66d573e6 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -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,12 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
VSH_EXCLUSIVE_OPTIONS("table", "name");
VSH_EXCLUSIVE_OPTIONS("table", "uuid");
+ VSH_EXCLUSIVE_OPTIONS("table", "id");
+ VSH_EXCLUSIVE_OPTIONS("all", "id");
+ VSH_EXCLUSIVE_OPTIONS("inactive", "id");
+ VSH_EXCLUSIVE_OPTIONS("state-shutoff", "id");
- if (!optUUID && !optName)
+ if (!optUUID && !optName && !optID)
optTable = true;
if (!(list = virshDomainListCollect(ctl, flags)))
@@ -2007,6 +2016,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 +2055,24 @@ 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) {
+ 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");
}
}
--
2.26.2