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 arguments "--dump-main-config", "--dump-repo-config=REPO_ID,..." #877

Merged
merged 3 commits into from
Oct 10, 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
14 changes: 14 additions & 0 deletions dnf5/include/dnf5/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ class Context : public libdnf5::cli::session::Session {

bool get_quiet() const { return quiet; }

/// Set to true to print information about main configuration
void set_dump_main_config(bool enable) { this->dump_main_config = enable; }

bool get_dump_main_config() const { return dump_main_config; }

/// Set a list of repository IDs to print information about their configuration
void set_dump_repo_config_id_list(const std::vector<std::string> & repo_id_list) {
this->dump_repo_config_id_list = repo_id_list;
}

const std::vector<std::string> & get_dump_repo_config_id_list() const { return dump_repo_config_id_list; }

/// Set to true to print information about variables
void set_dump_variables(bool enable) { this->dump_variables = enable; }

Expand Down Expand Up @@ -130,6 +142,8 @@ class Context : public libdnf5::cli::session::Session {
const char * comment{nullptr};

bool quiet{false};
bool dump_main_config{false};
std::vector<std::string> dump_repo_config_id_list;
bool dump_variables{false};

std::unique_ptr<Plugins> plugins;
Expand Down
111 changes: 110 additions & 1 deletion dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,38 @@ void RootCommand::set_argument_parser() {
global_options_group->register_argument(debug_solver);
}

{
auto dump_config = parser.add_new_named_arg("dump-main-config");
dump_config->set_long_name("dump-main-config");
dump_config->set_description("Print main configuration values to stdout");
dump_config->set_parse_hook_func([&ctx](
[[maybe_unused]] ArgumentParser::NamedArg * arg,
[[maybe_unused]] const char * option,
[[maybe_unused]] const char * value) {
ctx.set_dump_main_config(true);
return true;
});
global_options_group->register_argument(dump_config);
}

{
auto dump_repo_config = parser.add_new_named_arg("dump-repo-config");
dump_repo_config->set_long_name("dump-repo-config");
dump_repo_config->set_has_value(true);
dump_repo_config->set_arg_value_help("REPO_ID,...");
dump_repo_config->set_description(
"Print repository configuration values to stdout. List option. Supports globs");
dump_repo_config->set_parse_hook_func([&ctx](
[[maybe_unused]] ArgumentParser::NamedArg * arg,
[[maybe_unused]] const char * option,
[[maybe_unused]] const char * value) {
libdnf5::OptionStringList repoid_patterns(value);
ctx.set_dump_repo_config_id_list(repoid_patterns.get_value());
return true;
});
global_options_group->register_argument(dump_repo_config);
}

{
auto dump_variables = parser.add_new_named_arg("dump-variables");
dump_variables->set_long_name("dump-variables");
Expand Down Expand Up @@ -557,9 +589,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) {

// With these options it is possible to run dnf5 without a command.
if (arg_parser.get_named_arg("dump-variables", false).get_parse_count() > 0 ||
arg_parser.get_named_arg("dump-main-config", false).get_parse_count() > 0 ||
arg_parser.get_named_arg("dump-repo-config", false).get_parse_count() > 0) {
return;
}

throw_missing_command();
}

Expand Down Expand Up @@ -717,6 +754,69 @@ static void print_transaction_size_stats(Context & context) {
}
}

static void dump_main_configuration(Context & context) {
libdnf5::Base & base = context.base;
std::cout << _("======== Main configuration: ========") << std::endl;
for (const auto & option : base.get_config().opt_binds()) {
const auto & val = option.second;
std::string value;
bool was_set{false};
try {
value = val.get_value_string();
was_set = true;
} catch (const libdnf5::OptionError &) {
}
if (was_set) {
std::cout << fmt::format("{} = {}", option.first, value) << std::endl;
} else {
std::cout << fmt::format("{}", option.first) << std::endl;
}
}
}

static void dump_repository_configuration(Context & context, const std::vector<std::string> & repo_id_list) {
libdnf5::Base & base = context.base;
auto & log_router = *base.get_logger();

std::set<libdnf5::repo::RepoWeakPtr> matching_repos;
std::set<std::string_view> not_matchind_repos_id;

for (const auto & repo_id : repo_id_list) {
libdnf5::repo::RepoQuery query(base);
query.filter_id(repo_id, libdnf5::sack::QueryCmp::GLOB);

if (query.empty()) {
not_matchind_repos_id.insert(repo_id);
} else {
matching_repos.insert(query.begin(), query.end());
}
}

for (auto repo_id : not_matchind_repos_id) {
log_router.warning("No matching repo to dump configuration: \"{}\"", repo_id);
}

for (auto & repo : matching_repos) {
std::cout << libdnf5::utils::sformat(_("======== \"{}\" repository configuration: ========"), repo->get_id())
<< std::endl;
for (const auto & option : repo->get_config().opt_binds()) {
const auto & val = option.second;
std::string value;
bool was_set{false};
try {
value = val.get_value_string();
was_set = true;
} catch (const libdnf5::OptionError &) {
}
if (was_set) {
std::cout << fmt::format("{} = {}", option.first, value) << std::endl;
} else {
std::cout << fmt::format("{}", option.first) << std::endl;
}
}
}
}

static void dump_variables(Context & context) {
std::cout << _("======== Variables: ========") << std::endl;
for (const auto & var : context.base.get_vars()->get_variables()) {
Expand Down Expand Up @@ -857,6 +957,15 @@ int main(int argc, char * argv[]) try {

// Run selected command
command->configure();

if (context.get_dump_main_config()) {
dump_main_configuration(context);
}

if (const auto & repo_id_list = context.get_dump_repo_config_id_list(); !repo_id_list.empty()) {
dump_repository_configuration(context, repo_id_list);
}

{
if (context.get_load_available_repos() != dnf5::Context::LoadAvailableRepos::NONE) {
context.load_repos(context.get_load_system_repo());
Expand Down
8 changes: 8 additions & 0 deletions doc/dnf5.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ 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-main-config``
| Print main configuration values to stdout.

``--dump-repo-config=REPO_ID,...``
| Print repository configuration values to stdout.
| 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.

Expand Down
Loading