From 898fc80fde06da1a04a328271c28e08a91f63366 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Tue, 21 Aug 2018 09:59:12 +0100 Subject: [PATCH] Allow updating just one specific device from the command line --- src/fu-util.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/src/fu-util.c b/src/fu-util.c index 42587e636..a332972cc 100644 --- a/src/fu-util.c +++ b/src/fu-util.c @@ -2010,7 +2010,7 @@ fu_util_update_device_with_release (FuUtilPrivate *priv, } static gboolean -fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) +fu_util_update_all (FuUtilPrivate *priv, GError **error) { gboolean requires_reboot = FALSE; g_autoptr(GPtrArray) devices = NULL; @@ -2067,6 +2067,67 @@ fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) return TRUE; } +static gboolean +fu_util_update_by_id (FuUtilPrivate *priv, const gchar *device_id, GError **error) +{ + FwupdRelease *rel; + g_autoptr(FwupdDevice) dev = NULL; + g_autoptr(GPtrArray) rels = NULL; + + /* do not allow a partial device-id */ + dev = fwupd_client_get_device_by_id (priv->client, device_id, NULL, error); + if (dev == NULL) + return FALSE; + + /* get devices from daemon */ + priv->current_operation = FU_UTIL_OPERATION_UPDATE; + g_signal_connect (priv->client, "device-changed", + G_CALLBACK (fu_util_update_device_changed_cb), priv); + + /* get the releases for this device and filter for validity */ + rels = fwupd_client_get_upgrades (priv->client, + fwupd_device_get_id (dev), + NULL, error); + if (rels == NULL) + return FALSE; + rel = g_ptr_array_index (rels, 0); + if (!fu_util_update_device_with_release (priv, dev, rel, error)) + return FALSE; + + /* we don't want to ask anything */ + if (priv->no_reboot_check) { + g_debug ("skipping reboot check"); + return TRUE; + } + + /* the update needs the user to restart the computer */ + if (fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_REBOOT)) { + g_print ("\n%s %s [Y|n]: ", + /* TRANSLATORS: exactly one update needs this */ + _("The update requires a reboot to complete."), + /* TRANSLATORS: reboot to apply the update */ + _("Restart now?")); + if (!fu_util_prompt_for_boolean (TRUE)) + return TRUE; + return fu_util_update_reboot (error); + } + return TRUE; +} + +static gboolean +fu_util_update (FuUtilPrivate *priv, gchar **values, GError **error) +{ + if (g_strv_length (values) == 0) + return fu_util_update_all (priv, error); + if (g_strv_length (values) == 1) + return fu_util_update_by_id (priv, values[0], error); + g_set_error_literal (error, + FWUPD_ERROR, + FWUPD_ERROR_INVALID_ARGS, + "Invalid arguments"); + return FALSE; +} + static gboolean fu_util_remote_modify (FuUtilPrivate *priv, gchar **values, GError **error) {