From: Michael Tremer Date: Sat, 5 Jun 2021 13:16:49 +0000 (+0000) Subject: transaction: Add a simple confirmation before proceeding X-Git-Tag: 0.9.28~1285^2~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=be6d221a3e188e14cb56e43a55ab5c49c416a800;p=pakfire.git transaction: Add a simple confirmation before proceeding Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index ff7510803..8611b539b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -257,6 +257,7 @@ libpakfire_la_SOURCES = \ src/libpakfire/snapshot.c \ src/libpakfire/solution.c \ src/libpakfire/transaction.c \ + src/libpakfire/ui.c \ src/libpakfire/util.c pkginclude_HEADERS += \ @@ -293,6 +294,7 @@ pkginclude_HEADERS += \ src/libpakfire/include/pakfire/solution.h \ src/libpakfire/include/pakfire/transaction.h \ src/libpakfire/include/pakfire/types.h \ + src/libpakfire/include/pakfire/ui.h \ src/libpakfire/include/pakfire/util.h libpakfire_la_CFLAGS = \ diff --git a/src/libpakfire/include/pakfire/ui.h b/src/libpakfire/include/pakfire/ui.h new file mode 100644 index 000000000..ab274004a --- /dev/null +++ b/src/libpakfire/include/pakfire/ui.h @@ -0,0 +1,30 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2021 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_UI_H +#define PAKFIRE_UI_H + +#ifdef PAKFIRE_PRIVATE + +int pakfire_ui_confirm(Pakfire pakfire, const char* message, const char* question); + +#endif + +#endif /* PAKFIRE_UI_H */ diff --git a/src/libpakfire/transaction.c b/src/libpakfire/transaction.c index 3954348a0..bb0634516 100644 --- a/src/libpakfire/transaction.c +++ b/src/libpakfire/transaction.c @@ -35,6 +35,7 @@ #include #include #include +#include #include struct pakfire_transaction { @@ -911,15 +912,31 @@ PAKFIRE_EXPORT int pakfire_transaction_run(struct pakfire_transaction* transacti return 0; } + // Show what would be done + char* dump = pakfire_transaction_dump(transaction, 80); + + // Check if we should continue + r = pakfire_ui_confirm(transaction->pakfire, dump, _("Is this okay? [y/N]")); + if (r) { + ERROR(transaction->pakfire, "Transaction aborted upon user request\n"); + goto ERROR; + } + + // Write transaction dump to log + INFO(transaction->pakfire, "%s\n", dump); + // Download what we need r = pakfire_transaction_download(transaction); if (r) - return r; + goto ERROR; // Perform all steps r = pakfire_transaction_perform(transaction); if (r) - return r; + goto ERROR; - return 0; +ERROR: + free(dump); + + return r; } diff --git a/src/libpakfire/ui.c b/src/libpakfire/ui.c new file mode 100644 index 000000000..f9fbaa82d --- /dev/null +++ b/src/libpakfire/ui.c @@ -0,0 +1,58 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2021 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include + +int pakfire_ui_confirm(Pakfire pakfire, const char* message, const char* question) { + // The message is only printed once + printf("%s\n", message); + + // Skip this, if running in non-interactive mode + if (pakfire_has_flag(pakfire, PAKFIRE_FLAGS_NON_INTERACTIVE)) + return 1; + + while (1) { + // Print question + printf("%s ", question); + + // Wait for user to enter something + char p = getchar(); + + switch (p) { + // Positive response + case 'Y': + case 'y': + return 1; + + // Negative response + case EOF: + case 'N': + case 'n': + return 0; + + // Unknown input, repeat + default: + continue; + } + } + + return 0; +} diff --git a/src/pakfire/cli.py b/src/pakfire/cli.py index 338d1fd28..5c7ee405e 100644 --- a/src/pakfire/cli.py +++ b/src/pakfire/cli.py @@ -287,20 +287,6 @@ class Cli(object): self.ui.message(line) def _execute_transaction(self, transaction): - # Don't do anything if the transaction is empty - if len(transaction) == 0: - self.ui.message(_("Nothing to do")) - return - - # Dump the transaction - self._dump_transaction(transaction) - - # Ask the user to confirm to go ahead - if not self.ui.confirm(): - self.ui.message(_("Aborted by user")) - return - - # Run the transaction transaction.run() def handle_info(self, ns):