From b5a9903efce6fbb5709c65cf1ec229e925169cca Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Wed, 17 Jun 2020 16:32:12 +0200 Subject: [PATCH] cli: Automatically install authenticator as needed --- app/flatpak-cli-transaction.c | 44 +++++++++++++++++++++++++++++++++ app/flatpak-quiet-transaction.c | 42 +++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/app/flatpak-cli-transaction.c b/app/flatpak-cli-transaction.c index 13d7365b..7b294383 100644 --- a/app/flatpak-cli-transaction.c +++ b/app/flatpak-cli-transaction.c @@ -140,6 +140,49 @@ add_new_remote (FlatpakTransaction *transaction, return FALSE; } +static void +install_authenticator (FlatpakTransaction *old_transaction, + const char *remote, + const char *ref) +{ + FlatpakCliTransaction *old_cli = FLATPAK_CLI_TRANSACTION (old_transaction); + g_autoptr(FlatpakTransaction) transaction2 = NULL; + g_autoptr(GError) local_error = NULL; + FlatpakInstallation *installation = flatpak_transaction_get_installation (old_transaction); + FlatpakDir *dir = flatpak_installation_get_dir (installation, NULL); + + if (dir == NULL) + { + /* This should not happen */ + g_warning ("No dir in install_authenticator"); + return; + } + + transaction2 = flatpak_cli_transaction_new (dir, old_cli->disable_interaction, TRUE, FALSE, &local_error); + if (transaction2 == NULL) + { + g_printerr ("Unable to install authenticator: %s\n", local_error->message); + return; + } + + g_print ("Installing required authenticator for remote %s\n", remote); + if (!flatpak_transaction_add_install (transaction2, remote, ref, NULL, &local_error)) + { + if (!g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_ALREADY_INSTALLED)) + g_printerr ("Unable to install authenticator: %s\n", local_error->message); + return; + } + + if (!flatpak_transaction_run (transaction2, NULL, &local_error)) + { + if (!g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_ABORTED)) + g_printerr ("Unable to install authenticator: %s\n", local_error->message); + return; + } + + return; +} + static char * op_type_to_string (FlatpakTransactionOperationType operation_type) { @@ -1152,6 +1195,7 @@ flatpak_cli_transaction_class_init (FlatpakCliTransactionClass *klass) transaction_class->webflow_start = webflow_start; transaction_class->webflow_done = webflow_done; transaction_class->basic_auth_start = basic_auth_start; + transaction_class->install_authenticator = install_authenticator; } FlatpakTransaction * diff --git a/app/flatpak-quiet-transaction.c b/app/flatpak-quiet-transaction.c index c3729f7c..0677661c 100644 --- a/app/flatpak-quiet-transaction.c +++ b/app/flatpak-quiet-transaction.c @@ -159,6 +159,47 @@ operation_error (FlatpakTransaction *transaction, return non_fatal; /* Continue if non-fatal */ } +static void +install_authenticator (FlatpakTransaction *old_transaction, + const char *remote, + const char *ref) +{ + g_autoptr(FlatpakTransaction) transaction2 = NULL; + g_autoptr(GError) local_error = NULL; + FlatpakInstallation *installation = flatpak_transaction_get_installation (old_transaction); + FlatpakDir *dir = flatpak_installation_get_dir (installation, NULL); + + if (dir == NULL) + { + /* This should not happen */ + g_warning ("No dir in install_authenticator"); + return; + } + + transaction2 = flatpak_quiet_transaction_new (dir, &local_error); + if (transaction2 == NULL) + { + g_printerr ("Unable to install authenticator: %s\n", local_error->message); + return; + } + + if (!flatpak_transaction_add_install (transaction2, remote, ref, NULL, &local_error)) + { + if (!g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_ALREADY_INSTALLED)) + g_printerr ("Unable to install authenticator: %s\n", local_error->message); + return; + } + + if (!flatpak_transaction_run (transaction2, NULL, &local_error)) + { + if (!g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_ABORTED)) + g_printerr ("Unable to install authenticator: %s\n", local_error->message); + return; + } + + return; +} + static gboolean end_of_lifed_with_rebase (FlatpakTransaction *transaction, const char *remote, @@ -247,6 +288,7 @@ flatpak_quiet_transaction_class_init (FlatpakQuietTransactionClass *class) transaction_class->operation_error = operation_error; transaction_class->end_of_lifed_with_rebase = end_of_lifed_with_rebase; transaction_class->run = flatpak_quiet_transaction_run; + transaction_class->install_authenticator = install_authenticator; } FlatpakTransaction *