virsh: Implement new table API for virsh list

Instead of printing it straight in virsh, it creates table struct
which is filled with header and rows(domains). It allows us to know
more about table before printing to calculate alignment right.

Signed-off-by: Simon Kobyda <skobyda@redhat.com>
This commit is contained in:
Simon Kobyda 2018-08-23 17:53:42 +02:00 committed by Michal Privoznik
parent 9417f0b3f6
commit 2e97450425
2 changed files with 31 additions and 26 deletions

View File

@ -98,9 +98,9 @@ static int testCompareListDefault(const void *data ATTRIBUTE_UNUSED)
{ {
const char *const argv[] = { VIRSH_DEFAULT, "list", NULL }; const char *const argv[] = { VIRSH_DEFAULT, "list", NULL };
const char *exp = "\ const char *exp = "\
Id Name State\n\ Id Name State \n\
----------------------------------------------------\n\ ----------------------\n\
1 test running\n\ 1 test running \n\
\n"; \n";
return testCompareOutputLit(exp, NULL, argv); return testCompareOutputLit(exp, NULL, argv);
} }
@ -109,10 +109,10 @@ static int testCompareListCustom(const void *data ATTRIBUTE_UNUSED)
{ {
const char *const argv[] = { VIRSH_CUSTOM, "list", NULL }; const char *const argv[] = { VIRSH_CUSTOM, "list", NULL };
const char *exp = "\ const char *exp = "\
Id Name State\n\ Id Name State \n\
----------------------------------------------------\n\ ----------------------\n\
1 fv0 running\n\ 1 fv0 running \n\
2 fc4 running\n\ 2 fc4 running \n\
\n"; \n";
return testCompareOutputLit(exp, NULL, argv); return testCompareOutputLit(exp, NULL, argv);
} }

View File

@ -39,6 +39,7 @@
#include "virmacaddr.h" #include "virmacaddr.h"
#include "virxml.h" #include "virxml.h"
#include "virstring.h" #include "virstring.h"
#include "vsh-table.h"
VIR_ENUM_DECL(virshDomainIOError) VIR_ENUM_DECL(virshDomainIOError)
VIR_ENUM_IMPL(virshDomainIOError, VIR_ENUM_IMPL(virshDomainIOError,
@ -1901,6 +1902,7 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
char id_buf[INT_BUFSIZE_BOUND(unsigned int)]; char id_buf[INT_BUFSIZE_BOUND(unsigned int)];
unsigned int id; unsigned int id;
unsigned int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE; unsigned int flags = VIR_CONNECT_LIST_DOMAINS_ACTIVE;
vshTablePtr table = NULL;
/* construct filter flags */ /* construct filter flags */
if (vshCommandOptBool(cmd, "inactive") || if (vshCommandOptBool(cmd, "inactive") ||
@ -1940,15 +1942,12 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
/* print table header in legacy mode */ /* print table header in legacy mode */
if (optTable) { if (optTable) {
if (optTitle) if (optTitle)
vshPrintExtra(ctl, " %-5s %-30s %-10s %-20s\n%s\n", table = vshTableNew("Id", "Name", "State", "Title", NULL);
_("Id"), _("Name"), _("State"), _("Title"),
"-----------------------------------------"
"-----------------------------------------");
else else
vshPrintExtra(ctl, " %-5s %-30s %s\n%s\n", table = vshTableNew("Id", "Name", "State", NULL);
_("Id"), _("Name"), _("State"),
"-----------------------------------------" if (!table)
"-----------"); goto cleanup;
} }
for (i = 0; i < list->ndomains; i++) { for (i = 0; i < list->ndomains; i++) {
@ -1973,20 +1972,22 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
if (optTitle) { if (optTitle) {
if (!(title = virshGetDomainDescription(ctl, dom, true, 0))) if (!(title = virshGetDomainDescription(ctl, dom, true, 0)))
goto cleanup; goto cleanup;
if (vshTableRowAppend(table, id_buf,
vshPrint(ctl, " %-5s %-30s %-10s %-20s\n", id_buf, virDomainGetName(dom),
virDomainGetName(dom), state == -2 ? _("saved")
state == -2 ? _("saved") : virshDomainStateToString(state),
: virshDomainStateToString(state), title, NULL) < 0)
title); goto cleanup;
VIR_FREE(title); VIR_FREE(title);
} else { } else {
vshPrint(ctl, " %-5s %-30s %s\n", id_buf, if (vshTableRowAppend(table, id_buf,
virDomainGetName(dom), virDomainGetName(dom),
state == -2 ? _("saved") state == -2 ? _("saved")
: virshDomainStateToString(state)); : virshDomainStateToString(state),
NULL) < 0)
goto cleanup;
} }
} else if (optUUID && optName) { } else if (optUUID && optName) {
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"));
@ -2004,8 +2005,12 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
} }
} }
if (optTable)
vshTablePrintToStdout(table, ctl);
ret = true; ret = true;
cleanup: cleanup:
vshTableFree(table);
virshDomainListFree(list); virshDomainListFree(list);
return ret; return ret;
} }