mmcli: add json output support

Signed-off-by: Maxim Anisimov <maxim.anisimov.ua@gmail.com>
This commit is contained in:
Maxim Anisimov
2019-08-08 11:12:33 +03:00
committed by Aleksander Morgado
parent b0ec3030a3
commit d1ac5ca16b
3 changed files with 133 additions and 3 deletions

View File

@@ -45,6 +45,7 @@ static GCancellable *cancellable;
/* Context */
static gboolean output_keyvalue_flag;
static gboolean output_json_flag;
static gboolean verbose_flag;
static gboolean version_flag;
static gboolean async_flag;
@@ -55,6 +56,10 @@ static GOptionEntry main_entries[] = {
"Run action with machine-friendly key-value output",
NULL
},
{ "output-json", 'J', 0, G_OPTION_ARG_NONE, &output_json_flag,
"Run action with machine-friendly json output",
NULL
},
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag,
"Run action with verbose logs",
NULL
@@ -237,14 +242,19 @@ main (gint argc, gchar **argv)
g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, log_handler, NULL);
/* Setup output */
if (output_keyvalue_flag) {
if (output_keyvalue_flag && output_json_flag) {
g_printerr ("error: only one output type supported at the same time\n");
exit (EXIT_FAILURE);
}
if (output_keyvalue_flag || output_json_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 (output_keyvalue_flag ? MMC_OUTPUT_TYPE_KEYVALUE : MMC_OUTPUT_TYPE_JSON);
} else {
mmcli_output_set (MMC_OUTPUT_TYPE_HUMAN);
}
/* Setup signals */
signal (SIGINT, signals_handler);