From: Peter Krempa <pkre...@redhat.com>

Currently the handler functions in the virt shells return only a boolean
signalling if the command was successful or not. In preparation for a
command which will want to return another value (timeout) convert
vshCommand run to actually return the requested exit code instead and
document the conversion from boolean.

Signed-off-by: Peter Krempa <pkre...@redhat.com>
---
 tools/virsh.c      |  4 ++--
 tools/virt-admin.c |  4 ++--
 tools/vsh.c        | 29 ++++++++++++++++++++---------
 tools/vsh.h        |  2 +-
 4 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 244ca655ee..643ef6b453 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -836,7 +836,7 @@ main(int argc, char **argv)
     vshControl _ctl = { 0 };
     vshControl *ctl = &_ctl;
     virshControl virshCtl = { 0 };
-    bool ret = true;
+    int ret = EXIT_SUCCESS;

     ctl->name = "virsh";        /* hardcoded name of the binary */
     ctl->env_prefix = "VIRSH";
@@ -930,5 +930,5 @@ main(int argc, char **argv)
     }

     virshDeinit(ctl);
-    exit(ret ? EXIT_SUCCESS : EXIT_FAILURE);
+    exit(ret);
 }
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index b701ed1fe4..ac8f3202b6 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -1530,7 +1530,7 @@ main(int argc, char **argv)
     vshControl _ctl = { 0 };
     vshControl *ctl = &_ctl;
     vshAdmControl virtAdminCtl = { 0 };
-    bool ret = true;
+    int ret = EXIT_SUCCESS;

     ctl->name = "virt-admin";        /* hardcoded name of the binary */
     ctl->env_prefix = "VIRT_ADMIN";
@@ -1612,5 +1612,5 @@ main(int argc, char **argv)
     }

     vshAdmDeinit(ctl);
-    exit(ret ? EXIT_SUCCESS : EXIT_FAILURE);
+    exit(ret);
 }
diff --git a/tools/vsh.c b/tools/vsh.c
index e892cbca22..497b7ec631 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -1341,14 +1341,22 @@ vshBlockJobOptionBandwidth(vshControl *ctl,
 }


-/*
- * Executes command(s) and returns return code from last command
+/**
+ * vshCommandRun:
+ * @ctl: virt shell data
+ * @cmd: command to execute
+ *
+ * Returns return code from last command. Return values from command handlers
+ * which return boolean are converted as:
+ *   true -> EXIT_SUCCESS
+ *   false -> EXIT_FAILURE
  */
-bool
-vshCommandRun(vshControl *ctl, const vshCmd *cmd)
+int
+vshCommandRun(vshControl *ctl,
+              const vshCmd *cmd)
 {
     const vshClientHooks *hooks = ctl->hooks;
-    bool ret = true;
+    int ret = EXIT_SUCCESS;

     while (cmd) {
         gint64 before, after;
@@ -1358,16 +1366,19 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd)

         if ((cmd->def->flags & VSH_CMD_FLAG_NOCONNECT) ||
             (hooks && hooks->connHandler && hooks->connHandler(ctl))) {
-            ret = cmd->def->handler(ctl, cmd);
+            if (cmd->def->handler(ctl, cmd))
+                ret = EXIT_SUCCESS;
+            else
+                ret = EXIT_FAILURE;
         } else {
             /* connection is not usable, return error */
-            ret = false;
+            ret = EXIT_FAILURE;
         }

         after = g_get_real_time();

         /* try to automatically catch disconnections */
-        if (!ret &&
+        if (ret != EXIT_SUCCESS &&
             ((last_error != NULL) &&
              (((last_error->code == VIR_ERR_SYSTEM_ERROR) &&
                (last_error->domain == VIR_FROM_REMOTE)) ||
@@ -1376,7 +1387,7 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd)
               (last_error->code == VIR_ERR_INVALID_CONN))))
             disconnected++;

-        if (!ret)
+        if (ret != EXIT_SUCCESS)
             vshReportError(ctl);

         if (STREQ(cmd->def->name, "quit") ||
diff --git a/tools/vsh.h b/tools/vsh.h
index 3b75216e11..284da36e32 100644
--- a/tools/vsh.h
+++ b/tools/vsh.h
@@ -287,7 +287,7 @@ int vshBlockJobOptionBandwidth(vshControl *ctl,
                                bool bytes,
                                unsigned long *bandwidth);
 bool vshCommandOptBool(const vshCmd *cmd, const char *name);
-bool vshCommandRun(vshControl *ctl, const vshCmd *cmd);
+int vshCommandRun(vshControl *ctl, const vshCmd *cmd);
 bool vshCommandStringParse(vshControl *ctl, char *cmdstr,
                            vshCmd **partial);

-- 
2.49.0

Reply via email to