Skip to content

Commit

Permalink
[dnf5] Implement new argument "--dump-repo-config"
Browse files Browse the repository at this point in the history
This argument supports a list of repository ids (globs are supported).
If dnf5 is run with this argument, the configuration of the matching
repositories will be printed to standard output.

The old DNF4 supports the "--dump" argument, which also dumps
the repositories configuration if a list of them follows.
But it only works with the `config-manager` plugin.

This implements part of the functionality of the dnf4 config-manager plugin,
but for general use.
  • Loading branch information
jrohel committed Sep 7, 2023
1 parent a2023fb commit cf28524
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dnf5/include/dnf5/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class Context : public libdnf5::cli::session::Session {

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; }

Plugins & get_plugins() { return *plugins; }

libdnf5::Goal * get_goal(bool new_if_not_exist = true);
Expand Down Expand Up @@ -131,6 +138,7 @@ class Context : public libdnf5::cli::session::Session {

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

std::unique_ptr<Plugins> plugins;
std::unique_ptr<libdnf5::Goal> goal;
Expand Down
82 changes: 82 additions & 0 deletions dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ void register_group_with_args(
command.register_group(&group);
}

std::vector<std::string> split(const char * input_string) {
std::vector<std::string> list;
const char * ch = input_string;
while (*ch != '\0') {
while (*ch == ',' || *ch == ' ') {
++ch;
}
auto begin = ch;
while (*ch != '\0' && *ch != ',' && *ch != ' ') {
++ch;
}
if (const auto length = ch - begin; length > 0) {
list.emplace_back(begin, length);
}
}
return list;
}

} // namespace

class RootCommand : public Command {
Expand Down Expand Up @@ -521,6 +539,23 @@ void RootCommand::set_argument_parser() {
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) {
ctx.set_dump_repo_config_id_list(split(value));
return true;
});
global_options_group->register_argument(dump_repo_config);
}

{
auto version = parser.add_new_named_arg("version");
version->set_long_name("version");
Expand Down Expand Up @@ -729,6 +764,49 @@ static void dump_main_configuration(Context & context) {
}
}

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;
}
}
}
}

} // namespace dnf5


Expand Down Expand Up @@ -857,6 +935,10 @@ int main(int argc, char * argv[]) try {

context.apply_repository_setopts();

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

// Run selected command
command->configure();
{
Expand Down

0 comments on commit cf28524

Please sign in to comment.