virsh: Simplify vshTableRowAppend() calling in cmdList(), part two

Instead of having many if-else statements, each with its own
vshTableRowAppend() call, we can use a simple trick - have an
array of string pointers, set array members in the if bodies and
then call vshTableRowAppend() once.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Michal Privoznik 2024-08-19 12:15:01 +02:00
parent cf0e0715e0
commit f644cba8ae

View File

@ -1891,6 +1891,8 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
if (optTable) { if (optTable) {
const char *domName = virDomainGetName(dom); const char *domName = virDomainGetName(dom);
const char *stateStr = NULL; const char *stateStr = NULL;
g_autofree char *title = NULL;
const char *arg[2] = {};
state = virshDomainState(ctl, dom, NULL); state = virshDomainState(ctl, dom, NULL);
@ -1906,43 +1908,33 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
} }
if (optTitle && !optUUID) { if (optTitle && !optUUID) {
g_autofree char *title = NULL;
if (!(title = virshGetDomainDescription(ctl, dom, true, 0))) if (!(title = virshGetDomainDescription(ctl, dom, true, 0)))
goto cleanup; goto cleanup;
if (vshTableRowAppend(table, id_buf,
domName, stateStr, arg[0] = title;
title, NULL) < 0)
goto cleanup;
} else if (optUUID && !optTitle) { } else if (optUUID && !optTitle) {
if (virDomainGetUUIDString(dom, uuid) < 0) { if (virDomainGetUUIDString(dom, uuid) < 0) {
vshError(ctl, "%s", _("Failed to get domain's UUID")); vshError(ctl, "%s", _("Failed to get domain's UUID"));
goto cleanup; goto cleanup;
} }
if (vshTableRowAppend(table, id_buf,
domName, stateStr,
uuid, NULL) < 0)
goto cleanup;
} else if (optUUID && optTitle) {
g_autofree char *title = NULL;
arg[0] = uuid;
} else if (optUUID && optTitle) {
if (!(title = virshGetDomainDescription(ctl, dom, true, 0))) if (!(title = virshGetDomainDescription(ctl, dom, true, 0)))
goto cleanup; goto cleanup;
if (virDomainGetUUIDString(dom, uuid) < 0) { if (virDomainGetUUIDString(dom, uuid) < 0) {
vshError(ctl, "%s", _("Failed to get domain's UUID")); vshError(ctl, "%s", _("Failed to get domain's UUID"));
goto cleanup; goto cleanup;
} }
if (vshTableRowAppend(table, id_buf,
domName, stateStr, arg[0] = title;
title, uuid, NULL) < 0) arg[1] = uuid;
goto cleanup;
} else {
if (vshTableRowAppend(table, id_buf,
domName, stateStr,
NULL) < 0)
goto cleanup;
} }
if (vshTableRowAppend(table, id_buf,
domName, stateStr,
arg[0], arg[1], NULL) < 0)
goto cleanup;
} else { } else {
if (optUUID) { if (optUUID) {
if (virDomainGetUUIDString(dom, uuid) < 0) { if (virDomainGetUUIDString(dom, uuid) < 0) {