cli: allow multiple output format types

In addition to the standard human-friendly output, we now allow a
machine-friendly key-value pair output, much easier to parse and use
by programs that look at the mmcli output.

This new key-value pair output should be treated as API from now on, so
third-party programs can assume the output is compatible from one
release to another.
This commit is contained in:
Aleksander Morgado
2018-10-27 14:46:38 +02:00
committed by Dan Williams
parent cafc40245d
commit b0fba14b1e
18 changed files with 1976 additions and 890 deletions

View File

@@ -34,6 +34,7 @@
#include "mmcli.h"
#include "mmcli-common.h"
#include "mmcli-output.h"
#define PROGRAM_NAME "mmcli"
#define PROGRAM_VERSION PACKAGE_VERSION
@@ -43,12 +44,17 @@ static GMainLoop *loop;
static GCancellable *cancellable;
/* Context */
static gboolean output_keyvalue_flag;
static gboolean verbose_flag;
static gboolean version_flag;
static gboolean async_flag;
static gint timeout = 30; /* by default, use 30s for all operations */
static GOptionEntry main_entries[] = {
{ "output-keyvalue", 'K', 0, G_OPTION_ARG_NONE, &output_keyvalue_flag,
"Run action with machine-friendly key-value output",
NULL
},
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag,
"Run action with verbose logs",
NULL
@@ -230,6 +236,16 @@ main (gint argc, gchar **argv)
if (verbose_flag)
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, log_handler, NULL);
/* Setup output */
if (output_keyvalue_flag) {
if (verbose_flag) {
g_printerr ("error: cannot set verbose output in keyvalue output type\n");
exit (EXIT_FAILURE);
}
mmcli_output_set (MMC_OUTPUT_TYPE_KEYVALUE);
} else
mmcli_output_set (MMC_OUTPUT_TYPE_HUMAN);
/* Setup signals */
signal (SIGINT, signals_handler);
signal (SIGHUP, signals_handler);