[PATCH 1/2] virsh: Simplify vshTableRowAppend() calling in cmdList(), part one

Michal Privoznik posted 2 patches 3 months ago
[PATCH 1/2] virsh: Simplify vshTableRowAppend() calling in cmdList(), part one
Posted by Michal Privoznik 3 months ago
All calls to vshTableRowAppend() inside of cmdList() share couple
of same arguments: domain ID, domain name and domain state. While
the first one is stored in a variable and then passed to all
vshTableRowAppend() calls, the others are passed as a function
call. Switch the latter to variables too.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 tools/virsh-domain-monitor.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 37184baa69..80b6857fad 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1889,6 +1889,9 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
             ignore_value(virStrcpyStatic(id_buf, "-"));
 
         if (optTable) {
+            const char *domName = virDomainGetName(dom);
+            const char *stateStr = NULL;
+
             state = virshDomainState(ctl, dom, NULL);
 
             /* Domain could've been removed in the meantime */
@@ -1896,8 +1899,11 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
                 continue;
 
             if (managed && state == VIR_DOMAIN_SHUTOFF &&
-                virDomainHasManagedSaveImage(dom, 0) > 0)
-                state = -2;
+                virDomainHasManagedSaveImage(dom, 0) > 0) {
+                stateStr = _("saved");
+            } else {
+                stateStr = virshDomainStateToString(state);
+            }
 
             if (optTitle && !optUUID) {
                 g_autofree char *title = NULL;
@@ -1905,9 +1911,7 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
                 if (!(title = virshGetDomainDescription(ctl, dom, true, 0)))
                     goto cleanup;
                 if (vshTableRowAppend(table, id_buf,
-                                      virDomainGetName(dom),
-                                      state == -2 ? _("saved")
-                                      : virshDomainStateToString(state),
+                                      domName, stateStr,
                                       title, NULL) < 0)
                     goto cleanup;
             } else if (optUUID && !optTitle) {
@@ -1916,9 +1920,7 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
                     goto cleanup;
                 }
                 if (vshTableRowAppend(table, id_buf,
-                                      virDomainGetName(dom),
-                                      state == -2 ? _("saved")
-                                      : virshDomainStateToString(state),
+                                      domName, stateStr,
                                       uuid, NULL) < 0)
                     goto cleanup;
             } else if (optUUID && optTitle) {
@@ -1931,16 +1933,12 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
                     goto cleanup;
                 }
                 if (vshTableRowAppend(table, id_buf,
-                                      virDomainGetName(dom),
-                                      state == -2 ? _("saved")
-                                      : virshDomainStateToString(state),
+                                      domName, stateStr,
                                       title, uuid, NULL) < 0)
                     goto cleanup;
             } else {
                 if (vshTableRowAppend(table, id_buf,
-                                      virDomainGetName(dom),
-                                      state == -2 ? _("saved")
-                                      : virshDomainStateToString(state),
+                                      domName, stateStr,
                                       NULL) < 0)
                     goto cleanup;
             }
-- 
2.44.2
Re: [PATCH 1/2] virsh: Simplify vshTableRowAppend() calling in cmdList(), part one
Posted by Peter Krempa 3 months ago
On Mon, Aug 19, 2024 at 16:38:27 +0200, Michal Privoznik wrote:
> All calls to vshTableRowAppend() inside of cmdList() share couple
> of same arguments: domain ID, domain name and domain state. While
> the first one is stored in a variable and then passed to all
> vshTableRowAppend() calls, the others are passed as a function
> call. Switch the latter to variables too.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  tools/virsh-domain-monitor.c | 26 ++++++++++++--------------
>  1 file changed, 12 insertions(+), 14 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>