From e2794e26816b4b512145507793083ce203c6a2d2 Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Mon, 26 Aug 2024 12:07:32 +0200 Subject: [PATCH] dnf5: Print rpm scriptlets outputs to the user If a scriptlet fails, its output is displayed to the user in addition to being written to the log. For scriptlets that complete successfully, the output is only logged. --- dnf5/context.cpp | 18 +++++++++++++++++- dnf5/include/dnf5/context.hpp | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/dnf5/context.cpp b/dnf5/context.cpp index cdf35b627..d42ca047d 100644 --- a/dnf5/context.cpp +++ b/dnf5/context.cpp @@ -843,6 +843,17 @@ void RpmTransCB::cpio_error(const libdnf5::rpm::TransactionItem & item) { multi_progress_bar.print(); } +void RpmTransCB::script_output_to_progress(const libdnf5::cli::progressbar::MessageType message_type) { + auto transaction = context.get_transaction(); + auto output = transaction->get_last_script_output(); + if (!output.empty()) { + active_progress_bar->add_message(message_type, "Scriptlet output:"); + for (auto & line : libdnf5::utils::string::split(output, "\n")) { + active_progress_bar->add_message(message_type, line); + } + } +} + void RpmTransCB::script_error( [[maybe_unused]] const libdnf5::rpm::TransactionItem * item, libdnf5::rpm::Nevra nevra, @@ -855,6 +866,7 @@ void RpmTransCB::script_error( script_type_to_string(type), to_full_nevra_string(nevra), return_code)); + script_output_to_progress(libdnf5::cli::progressbar::MessageType::ERROR); multi_progress_bar.print(); } @@ -883,7 +895,11 @@ void RpmTransCB::script_stop( // Print the error message here. active_progress_bar->add_message( libdnf5::cli::progressbar::MessageType::WARNING, - fmt::format("Error in {} scriptlet: {}", script_type_to_string(type), to_full_nevra_string(nevra))); + fmt::format( + "Non-critical error in {} scriptlet: {}", + script_type_to_string(type), + to_full_nevra_string(nevra))); + script_output_to_progress(libdnf5::cli::progressbar::MessageType::WARNING); [[fallthrough]]; default: active_progress_bar->add_message( diff --git a/dnf5/include/dnf5/context.hpp b/dnf5/include/dnf5/context.hpp index 505fa8401..3661def8a 100644 --- a/dnf5/include/dnf5/context.hpp +++ b/dnf5/include/dnf5/context.hpp @@ -259,6 +259,8 @@ class DNF_API RpmTransCB : public libdnf5::rpm::TransactionCallbacks { DNF_LOCAL static std::chrono::time_point prev_print_time; + DNF_LOCAL void script_output_to_progress(libdnf5::cli::progressbar::MessageType message_type); + libdnf5::cli::progressbar::MultiProgressBar multi_progress_bar; libdnf5::cli::progressbar::DownloadProgressBar * active_progress_bar{nullptr}; Context & context;