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,104 +1072,115 @@ dump_output_list_keyvalue (MmcF field)
} }
/******************************************************************************/ /******************************************************************************/
/* Json-friendly output */ /* JSON-friendly output */
static gint static gint
list_sort_by_keys (const OutputItem *item_a, list_sort_by_keys (const OutputItem *item_a,
const OutputItem *item_b) const OutputItem *item_b)
{ {
return g_strcmp0(field_infos[item_a->field].key, field_infos[item_b->field].key); return g_strcmp0 (field_infos[item_a->field].key, field_infos[item_b->field].key);
} }
static void static void
dump_output_json (void) dump_output_json (void)
{ {
GList *l; GList *l;
MmcF current_field = MMC_F_UNKNOWN; MmcF current_field = MMC_F_UNKNOWN;
gchar **current_path = NULL; gchar **current_path = NULL;
guint cur_dlen = 0; guint cur_dlen = 0;
GRegex *escape_regex; GRegex *escape_regex;
output_items = g_list_sort (output_items, (GCompareFunc) list_sort_by_keys); output_items = g_list_sort (output_items, (GCompareFunc) list_sort_by_keys);
escape_regex = g_regex_new("'", G_REGEX_MULTILINE | G_REGEX_RAW, 0, NULL); escape_regex = g_regex_new ("'", G_REGEX_MULTILINE | G_REGEX_RAW, 0, NULL);
g_print("{"); g_print("{");
for (l = output_items; l; l = g_list_next (l)) { for (l = output_items; l; l = g_list_next (l)) {
OutputItem *item_l = (OutputItem *)(l->data); OutputItem *item_l = (OutputItem *)(l->data);
if (current_field != item_l->field) { if (current_field != item_l->field) {
guint new_dlen; guint new_dlen;
guint iter = 0; guint iter = 0;
gchar **new_path; gchar **new_path;
new_path = g_strsplit(field_infos[item_l->field].key, ".", -1); new_path = g_strsplit (field_infos[item_l->field].key, ".", -1);
new_dlen = g_strv_length(new_path) - 1; new_dlen = g_strv_length (new_path) - 1;
if (current_path) { if (current_path) {
guint min_dlen = MIN (cur_dlen, new_dlen); guint min_dlen;
while(iter < min_dlen && g_strcmp0(current_path[iter], new_path[iter]) == 0)
min_dlen = MIN (cur_dlen, new_dlen);
while (iter < min_dlen && g_strcmp0 (current_path[iter], new_path[iter]) == 0)
iter++; iter++;
g_strfreev(current_path); g_strfreev (current_path);
if (iter < min_dlen || new_dlen < cur_dlen) if (iter < min_dlen || new_dlen < cur_dlen)
for(min_dlen = iter; min_dlen < cur_dlen; min_dlen++) for (min_dlen = iter; min_dlen < cur_dlen; min_dlen++)
g_print("}"); g_print ("}");
g_print(","); g_print (",");
} }
while(iter < new_dlen) while (iter < new_dlen)
g_print("\"%s\":{", new_path[iter++]); g_print ("\"%s\":{", new_path[iter++]);
cur_dlen = new_dlen; cur_dlen = new_dlen;
current_path = new_path; current_path = new_path;
current_field = item_l->field; current_field = item_l->field;
} else { } else {
g_print(","); g_print (",");
} }
if (item_l->type == VALUE_TYPE_SINGLE) { if (item_l->type == VALUE_TYPE_SINGLE) {
OutputItemSingle *single = (OutputItemSingle *) item_l; OutputItemSingle *single = (OutputItemSingle *) item_l;
gchar *escaped = NULL; gchar *escaped = NULL;
if (single->value) if (single->value)
escaped = g_regex_replace_literal(escape_regex, single->value, -1, 0, "\"", 0, NULL); escaped = g_regex_replace_literal (escape_regex, single->value, -1, 0, "\"", 0, NULL);
g_print ("\"%s\":\"%s\"", current_path[cur_dlen], escaped ? escaped : "--"); g_print ("\"%s\":\"%s\"", current_path[cur_dlen], escaped ? escaped : "--");
g_free (escaped); g_free (escaped);
} else if (item_l->type == VALUE_TYPE_MULTIPLE) { } else if (item_l->type == VALUE_TYPE_MULTIPLE) {
OutputItemMultiple *multiple = (OutputItemMultiple *) item_l; 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]); g_print ("\"%s\":[", current_path[cur_dlen]);
for(i = 0; i < n; i++) { 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); g_print("\"%s\"", escaped);
if (i < n - 1) if (i < n - 1)
g_print(","); g_print(",");
g_free (escaped); g_free (escaped);
} }
g_print("]"); g_print("]");
} else { } else
g_assert_not_reached (); g_assert_not_reached ();
}
} }
while(cur_dlen--)
g_print("}"); while (cur_dlen--)
g_print ("}");
g_print("}\n"); g_print("}\n");
if (current_path)
g_strfreev(current_path); g_strfreev (current_path);
g_regex_unref(escape_regex); g_regex_unref (escape_regex);
} }
static void static void
dump_output_list_json (MmcF field) dump_output_list_json (MmcF field)
{ {
GList *l; GList *l;
g_assert (field != MMC_F_UNKNOWN); g_assert (field != MMC_F_UNKNOWN);
g_print("{\"%s\":[", field_infos[field].key); g_print("{\"%s\":[", field_infos[field].key);
for (l = output_items; l; l = g_list_next (l)) { for (l = output_items; l; l = g_list_next (l)) {
OutputItem *item_l; OutputItem *item_l;
OutputItemListitem *listitem; OutputItemListitem *listitem;
item_l = (OutputItem *)(l->data); item_l = (OutputItem *)(l->data);
g_assert (item_l->type == VALUE_TYPE_LISTITEM); g_assert (item_l->type == VALUE_TYPE_LISTITEM);
listitem = (OutputItemListitem *)item_l; listitem = (OutputItemListitem *)item_l;
@@ -1178,9 +1189,11 @@ dump_output_list_json (MmcF field)
/* All items must be of same type */ /* All items must be of same type */
g_assert_cmpint (item_l->field, ==, field); g_assert_cmpint (item_l->field, ==, field);
g_print("\"%s\"", listitem->value); g_print("\"%s\"", listitem->value);
if (g_list_next(l))
if (g_list_next (l))
g_print(","); g_print(",");
} }
g_print("]}\n"); 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"); g_printerr ("error: only one output type supported at the same time\n");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
if (output_keyvalue_flag || output_json_flag) { if (output_keyvalue_flag) {
if (verbose_flag) { if (verbose_flag) {
g_printerr ("error: cannot set verbose output in keyvalue output type\n"); g_printerr ("error: cannot set verbose output in keyvalue output type\n");
exit (EXIT_FAILURE); 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 { } else {
mmcli_output_set (MMC_OUTPUT_TYPE_HUMAN); mmcli_output_set (MMC_OUTPUT_TYPE_HUMAN);
} }