[libvirt] [PATCH v2 13/13] virt-admin: Implement vshTable API to server-list and client-list

Simon Kobyda skobyda at redhat.com
Fri Sep 21 14:17:24 UTC 2018


Signed-off-by: Simon Kobyda <skobyda at redhat.com>
---
 tools/virt-admin.c | 47 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index 63822bc13e..d119e4d960 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -40,6 +40,7 @@
 #include "virgettext.h"
 #include "virtime.h"
 #include "virt-admin-completer.h"
+#include "vsh-table.h"
 
 /* Gnulib doesn't guarantee SA_SIGINFO support.  */
 #ifndef SA_SIGINFO
@@ -382,6 +383,7 @@ cmdSrvList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     char *uri = NULL;
     virAdmServerPtr *srvs = NULL;
     vshAdmControlPtr priv = ctl->privData;
+    vshTablePtr table = NULL;
 
     /* Obtain a list of available servers on the daemon */
     if ((nsrvs = virAdmConnectListServers(priv->conn, &srvs, 0)) < 0) {
@@ -391,13 +393,27 @@ cmdSrvList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
         goto cleanup;
     }
 
-    vshPrintExtra(ctl, " %-5s %-15s\n", "Id", "Name");
-    vshPrintExtra(ctl, "---------------\n");
-    for (i = 0; i < nsrvs; i++)
-        vshPrint(ctl, " %-5zu %-15s\n", i, virAdmServerGetName(srvs[i]));
+    table = vshTableNew(_("Id"), _("Name"), NULL);
+    if (!table)
+        goto cleanup;
+
+    for (i = 0; i < nsrvs; i++) {
+        VIR_AUTOFREE(char *) idStr = NULL;
+        if (virAsprintf(&idStr, "%lu", i) < 0)
+            goto cleanup;
+
+        if (vshTableRowAppend(table,
+                              idStr,
+                              virAdmServerGetName(srvs[i]),
+                              NULL) < 0)
+            goto cleanup;
+    }
+
+    vshTablePrintToStdout(table, ctl);
 
     ret = true;
  cleanup:
+    vshTableFree(table);
     if (srvs) {
         for (i = 0; i < nsrvs; i++)
             virAdmServerFree(srvs[i]);
@@ -613,10 +629,10 @@ cmdSrvClientsList(vshControl *ctl, const vshCmd *cmd)
     const char *srvname = NULL;
     unsigned long long id;
     virClientTransport transport;
-    char *timestr = NULL;
     virAdmServerPtr srv = NULL;
     virAdmClientPtr *clts = NULL;
     vshAdmControlPtr priv = ctl->privData;
+    vshTablePtr table = NULL;
 
     if (vshCommandOptStringReq(ctl, cmd, "server", &srvname) < 0)
         return false;
@@ -631,12 +647,13 @@ cmdSrvClientsList(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
     }
 
-    vshPrintExtra(ctl, " %-5s %-15s %-15s\n%s\n", _("Id"), _("Transport"),
-                  _("Connected since"),
-                  "-------------------------"
-                  "-------------------------");
+    table = vshTableNew(_("Id"), _("Transport"), _("Connected sice"), NULL);
+    if (!table)
+        goto cleanup;
 
     for (i = 0; i < nclts; i++) {
+        VIR_AUTOFREE(char *) timestr = NULL;
+        VIR_AUTOFREE(char *) idStr = NULL;
         virAdmClientPtr client = clts[i];
         id = virAdmClientGetID(client);
         transport = virAdmClientGetTransport(client);
@@ -644,14 +661,20 @@ cmdSrvClientsList(vshControl *ctl, const vshCmd *cmd)
                              &timestr) < 0)
             goto cleanup;
 
-        vshPrint(ctl, " %-5llu %-15s %-15s\n",
-                 id, vshAdmClientTransportToString(transport), timestr);
-        VIR_FREE(timestr);
+        if (virAsprintf(&idStr, "%llu", id) < 0)
+            goto cleanup;
+        if (vshTableRowAppend(table, idStr,
+                              vshAdmClientTransportToString(transport),
+                              timestr, NULL) < 0)
+            goto cleanup;
     }
 
+    vshTablePrintToStdout(table, ctl);
+
     ret = true;
 
  cleanup:
+    vshTableFree(table);
     if (clts) {
         for (i = 0; i < nclts; i++)
             virAdmClientFree(clts[i]);
-- 
2.17.1




More information about the libvir-list mailing list