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-11-26 16:51:35 +00:00
committed by Dan Williams
parent cafc40245d
commit b0fba14b1e
18 changed files with 1976 additions and 890 deletions
+34 -30
View File
@@ -38,6 +38,7 @@
#include "mmcli.h"
#include "mmcli-common.h"
#include "mmcli-output.h"
/* Context */
typedef struct {
@@ -130,8 +131,13 @@ mmcli_manager_options_enabled (void)
exit (EXIT_FAILURE);
}
if (monitor_modems_flag)
if (monitor_modems_flag) {
if (mmcli_output_get () != MMC_OUTPUT_TYPE_HUMAN) {
g_printerr ("error: modem monitoring not available in keyvalue output\n");
exit (EXIT_FAILURE);
}
mmcli_force_async_operation ();
}
#if defined WITH_UDEV
if (report_kernel_event_auto_scan)
@@ -265,58 +271,56 @@ scan_devices_ready (MMManager *manager,
mmcli_async_operation_done ();
}
#define FOUND_ACTION_PREFIX " "
#define ADDED_ACTION_PREFIX "(+) "
#define REMOVED_ACTION_PREFIX "(-) "
static void
print_modem_short_info (MMObject *modem)
output_modem_info (MMObject *obj,
const gchar *prefix)
{
const gchar *manufacturer, *model;
gchar *extra;
const gchar *manufacturer;
const gchar *model;
manufacturer = mm_modem_get_manufacturer (mm_object_peek_modem (modem));
model = mm_modem_get_model (mm_object_peek_modem (modem));
g_print ("\t%s [%s] %s\n",
mm_object_get_path (modem),
manufacturer ? manufacturer : "unknown",
model ? model : "unknown");
manufacturer = mm_modem_get_manufacturer (mm_object_peek_modem (obj));
model = mm_modem_get_model (mm_object_peek_modem (obj));
extra = g_strdup_printf ("[%s] %s",
manufacturer ? manufacturer : "manufacturer unknown",
model ? model : "model unknown");
mmcli_output_listitem (MMC_F_MODEM_LIST_DBUS_PATH,
prefix,
mm_object_get_path (obj),
extra);
g_free (extra);
}
static void
device_added (MMManager *manager,
MMObject *modem)
{
g_print ("Added modem:\n");
print_modem_short_info (modem);
fflush (stdout);
output_modem_info (modem, ADDED_ACTION_PREFIX);
mmcli_output_list_dump (MMC_F_MODEM_LIST_DBUS_PATH);
}
static void
device_removed (MMManager *manager,
MMObject *modem)
{
g_print ("Removed modem:\n");
print_modem_short_info (modem);
fflush (stdout);
output_modem_info (modem, REMOVED_ACTION_PREFIX);
mmcli_output_list_dump (MMC_F_MODEM_LIST_DBUS_PATH);
}
static void
list_current_modems (MMManager *manager)
{
GList *modems;
GList *l;
modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (ctx->manager));
g_print ("\n");
if (!modems)
g_print ("No modems were found\n");
else {
GList *l;
g_print ("Found %u modems:\n", g_list_length (modems));
for (l = modems; l; l = g_list_next (l)) {
print_modem_short_info (MM_OBJECT (l->data));
}
g_list_free_full (modems, g_object_unref);
}
g_print ("\n");
for (l = modems; l; l = g_list_next (l))
output_modem_info ((MMObject *)(l->data), FOUND_ACTION_PREFIX);
mmcli_output_list_dump (MMC_F_MODEM_LIST_DBUS_PATH);
}
static void