mmcli: coding style fixes in the new JSON output support

This commit is contained in:
Aleksander Morgado
2019-08-28 18:10:48 +02:00
parent d1ac5ca16b
commit ae55fbf964
2 changed files with 57 additions and 37 deletions

View File

@@ -1072,7 +1072,7 @@ dump_output_list_keyvalue (MmcF field)
}
/******************************************************************************/
/* Json-friendly output */
/* JSON-friendly output */
static gint
list_sort_by_keys (const OutputItem *item_a,
@@ -1105,7 +1105,9 @@ dump_output_json (void)
new_path = g_strsplit (field_infos[item_l->field].key, ".", -1);
new_dlen = g_strv_length (new_path) - 1;
if (current_path) {
guint min_dlen = MIN (cur_dlen, new_dlen);
guint min_dlen;
min_dlen = MIN (cur_dlen, new_dlen);
while (iter < min_dlen && g_strcmp0 (current_path[iter], new_path[iter]) == 0)
iter++;
@@ -1127,6 +1129,7 @@ dump_output_json (void)
} else {
g_print (",");
}
if (item_l->type == VALUE_TYPE_SINGLE) {
OutputItemSingle *single = (OutputItemSingle *) item_l;
gchar *escaped = NULL;
@@ -1138,25 +1141,29 @@ dump_output_json (void)
g_free (escaped);
} else if (item_l->type == VALUE_TYPE_MULTIPLE) {
OutputItemMultiple *multiple = (OutputItemMultiple *) item_l;
guint i, n = multiple->values ? g_strv_length (multiple->values) : 0;
guint i, n;
n = multiple->values ? g_strv_length (multiple->values) : 0;
g_print ("\"%s\":[", current_path[cur_dlen]);
for (i = 0; i < n; i++) {
gchar *escaped = g_regex_replace_literal(escape_regex, multiple->values[i], -1, 0, "\"", 0, NULL);
gchar *escaped;
escaped = g_regex_replace_literal (escape_regex, multiple->values[i], -1, 0, "\"", 0, NULL);
g_print("\"%s\"", escaped);
if (i < n - 1)
g_print(",");
g_free (escaped);
}
g_print("]");
} else {
} else
g_assert_not_reached ();
}
}
while (cur_dlen--)
g_print ("}");
g_print("}\n");
if (current_path)
g_strfreev (current_path);
g_regex_unref (escape_regex);
}
@@ -1165,11 +1172,15 @@ static void
dump_output_list_json (MmcF field)
{
GList *l;
g_assert (field != MMC_F_UNKNOWN);
g_print("{\"%s\":[", field_infos[field].key);
for (l = output_items; l; l = g_list_next (l)) {
OutputItem *item_l;
OutputItemListitem *listitem;
item_l = (OutputItem *)(l->data);
g_assert (item_l->type == VALUE_TYPE_LISTITEM);
listitem = (OutputItemListitem *)item_l;
@@ -1178,9 +1189,11 @@ dump_output_list_json (MmcF field)
/* All items must be of same type */
g_assert_cmpint (item_l->field, ==, field);
g_print("\"%s\"", listitem->value);
if (g_list_next (l))
g_print(",");
}
g_print("]}\n");
}

View File

@@ -246,12 +246,19 @@ main (gint argc, gchar **argv)
g_printerr ("error: only one output type supported at the same time\n");
exit (EXIT_FAILURE);
}
if (output_keyvalue_flag || output_json_flag) {
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 (output_keyvalue_flag ? MMC_OUTPUT_TYPE_KEYVALUE : MMC_OUTPUT_TYPE_JSON);
mmcli_output_set (MMC_OUTPUT_TYPE_KEYVALUE);
}
else if (output_json_flag) {
if (verbose_flag) {
g_printerr ("error: cannot set verbose output in JSON output type\n");
exit (EXIT_FAILURE);
}
mmcli_output_set (MMC_OUTPUT_TYPE_JSON);
} else {
mmcli_output_set (MMC_OUTPUT_TYPE_HUMAN);
}