From 506956bdd23b5633949e9707e2318b72bc6ad579 Mon Sep 17 00:00:00 2001 From: Jaroslav Rohel Date: Wed, 6 Sep 2023 11:49:36 +0200 Subject: [PATCH] [dnf5] Implement new argument "--dump-variables" If dnf5 is run with this argument, variable values are added to standard output. The old DNF4 supported this argument only with the `config-manager` plugin. If the user wanted to know the values of the variables with which a certain command will run, he had to first use `config-manager` and then run the desired command with the same settings (same environment and configuration files) and hope that nothing changed in between (e.g. thanks to a plugin). --- dnf5/include/dnf5/context.hpp | 6 ++++++ dnf5/main.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/dnf5/include/dnf5/context.hpp b/dnf5/include/dnf5/context.hpp index 4dde2f83ba..f25fa72d98 100644 --- a/dnf5/include/dnf5/context.hpp +++ b/dnf5/include/dnf5/context.hpp @@ -97,6 +97,11 @@ class Context : public libdnf5::cli::session::Session { bool get_quiet() const { return quiet; } + /// Set to true to print information about variables + void set_dump_variables(bool dump_variables) { this->dump_variables = dump_variables; } + + bool get_dump_variables() const { return dump_variables; } + Plugins & get_plugins() { return *plugins; } libdnf5::Goal * get_goal(bool new_if_not_exist = true); @@ -125,6 +130,7 @@ class Context : public libdnf5::cli::session::Session { const char * comment{nullptr}; bool quiet{false}; + bool dump_variables{false}; std::unique_ptr plugins; std::unique_ptr goal; diff --git a/dnf5/main.cpp b/dnf5/main.cpp index 07d3b826fa..b3380a73ca 100644 --- a/dnf5/main.cpp +++ b/dnf5/main.cpp @@ -507,6 +507,20 @@ void RootCommand::set_argument_parser() { global_options_group->register_argument(debug_solver); } + { + auto dump_variables = parser.add_new_named_arg("dump-variables"); + dump_variables->set_long_name("dump-variables"); + dump_variables->set_description("Print variable values to stdout"); + dump_variables->set_parse_hook_func([&ctx]( + [[maybe_unused]] ArgumentParser::NamedArg * arg, + [[maybe_unused]] const char * option, + [[maybe_unused]] const char * value) { + ctx.set_dump_variables(true); + return true; + }); + global_options_group->register_argument(dump_variables); + } + { auto version = parser.add_new_named_arg("version"); version->set_long_name("version"); @@ -695,6 +709,14 @@ static void print_transaction_size_stats(Context & context) { } } +static void dump_variables(Context & context) { + std::cout << _("======== Variables: ========") << std::endl; + for (const auto & var : context.base.get_vars()->get_variables()) { + const auto & val = var.second; + std::cout << fmt::format("{} = {}", var.first, val.value) << std::endl; + } +} + } // namespace dnf5 @@ -808,6 +830,10 @@ int main(int argc, char * argv[]) try { // Write messages from memory buffer logger to stream logger dynamic_cast(*file_logger).write_to_logger(log_router); + if (context.get_dump_variables()) { + dump_variables(context); + } + auto repo_sack = base.get_repo_sack(); repo_sack->create_repos_from_system_configuration(); any_repos_from_system_configuration = repo_sack->size() > 0;