Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dnf5] Implement new argument "--dump-variables" #875

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dnf5/include/dnf5/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 enable) { this->dump_variables = enable; }

bool get_dump_variables() const { return dump_variables; }

Plugins & get_plugins() { return *plugins; }

libdnf5::Goal * get_goal(bool new_if_not_exist = true);
Expand Down Expand Up @@ -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> plugins;
std::unique_ptr<libdnf5::Goal> goal;
Expand Down
36 changes: 35 additions & 1 deletion dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class RootCommand : public Command {
explicit RootCommand(libdnf5::cli::session::Session & context) : Command(context, "dnf5") {}
void set_parent_command() override { get_session().set_root_command(*this); }
void set_argument_parser() override;
void pre_configure() override { throw_missing_command(); }
void pre_configure() override;
};

void RootCommand::set_argument_parser() {
Expand Down Expand Up @@ -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");
ppisar marked this conversation as resolved.
Show resolved Hide resolved
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");
Expand Down Expand Up @@ -541,6 +555,14 @@ void RootCommand::set_argument_parser() {
}
}

void RootCommand::pre_configure() {
auto & arg_parser = get_context().get_argument_parser();
if (arg_parser.get_named_arg("dump-variables", false).get_parse_count() > 0) {
return;
}
throw_missing_command();
}

static void add_commands(Context & context) {
// First, add the "root" command.
context.add_and_initialize_command(std::make_unique<RootCommand>(context));
Expand Down Expand Up @@ -695,6 +717,14 @@ static void print_transaction_size_stats(Context & context) {
}
}

static void dump_variables(Context & context) {
std::cout << _("======== Variables: ========") << std::endl;
ppisar marked this conversation as resolved.
Show resolved Hide resolved
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;
ppisar marked this conversation as resolved.
Show resolved Hide resolved
ppisar marked this conversation as resolved.
Show resolved Hide resolved
}
}

} // namespace dnf5


Expand Down Expand Up @@ -810,6 +840,10 @@ int main(int argc, char * argv[]) try {
// Write messages from memory buffer logger to stream logger
dynamic_cast<libdnf5::MemoryBufferLogger &>(*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;
Expand Down
3 changes: 3 additions & 0 deletions doc/dnf5.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ Following options are applicable in the general context for any ``dnf5`` command
| This is a list option which can be specified multiple times.
| Accepted values are ids, or a glob of ids.

``--dump-variables``
| Print variable values to stdout.

``--enable-plugin=PLUGIN_NAME,...``
| Enable specified plugins for the purpose of the current ``DNF5`` command.
| This is a list option which can be specified multiple times.
Expand Down
Loading