]> git.ipfire.org Git - pakfire.git/commitdiff
transaction: Add a simple confirmation before proceeding
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Jun 2021 13:16:49 +0000 (13:16 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 5 Jun 2021 13:16:49 +0000 (13:16 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/include/pakfire/ui.h [new file with mode: 0644]
src/libpakfire/transaction.c
src/libpakfire/ui.c [new file with mode: 0644]
src/pakfire/cli.py

index ff751080340efcf330c1fb4d48026de839a4d44c..8611b539b4c54372899f2a3b756d6a8f805aa504 100644 (file)
@@ -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 (file)
index 0000000..ab27400
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */
index 3954348a05ff4f6fa6e6e14f7a2633b774d20000..bb0634516486907ca9a7380f83ed41612b4f33fc 100644 (file)
@@ -35,6 +35,7 @@
 #include <pakfire/repo.h>
 #include <pakfire/transaction.h>
 #include <pakfire/types.h>
+#include <pakfire/ui.h>
 #include <pakfire/util.h>
 
 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 (file)
index 0000000..f9fbaa8
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <pakfire/pakfire.h>
+#include <pakfire/ui.h>
+
+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;
+}
index 338d1fd281e8ccc8e410c38405caed8d6b9fe6dc..5c7ee405ee732753f35617066868efabfb291f05 100644 (file)
@@ -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):