]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: Simplify vshTableRowAppend() calling in cmdList(), part two
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 19 Aug 2024 10:15:01 +0000 (12:15 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 20 Aug 2024 07:08:35 +0000 (09:08 +0200)
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>
tools/virsh-domain-monitor.c

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