aboutsummaryrefslogtreecommitdiff
path: root/cli/mmcli-manager.c
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2018-10-27 14:46:38 +0200
committerDan Williams <dcbw@redhat.com>2018-11-26 16:51:35 +0000
commitb0fba14b1e122522a1db2581779ffb94ee7cfa37 (patch)
treef0ab362f9e124b7f01f8c7e1b97a84d0c40f3edc /cli/mmcli-manager.c
parentcafc40245d2007ad3a9a33875fc6b71d25e6a254 (diff)
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.
Diffstat (limited to 'cli/mmcli-manager.c')
-rw-r--r--cli/mmcli-manager.c66
1 files changed, 35 insertions, 31 deletions
diff --git a/cli/mmcli-manager.c b/cli/mmcli-manager.c
index ed8a9926..41bda211 100644
--- a/cli/mmcli-manager.c
+++ b/cli/mmcli-manager.c
@@ -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;
-
- 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");
+ gchar *extra;
+ const gchar *manufacturer;
+ const gchar *model;
+
+ 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