Skip to content

Commit

Permalink
libdnf5-cli::output: Use ifaces instead templates. Move code to .cpp …
Browse files Browse the repository at this point in the history
…files
  • Loading branch information
jrohel committed Mar 12, 2024
1 parent 044699f commit 8a6edc7
Show file tree
Hide file tree
Showing 64 changed files with 2,301 additions and 1,729 deletions.
28 changes: 18 additions & 10 deletions dnf5-plugins/automatic_plugin/automatic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "transaction_callbacks_simple.hpp"

#include <curl/curl.h>
#include <libdnf5-cli/output/adapters/transaction.hpp>
#include <libdnf5-cli/output/transaction_table.hpp>
#include <libdnf5/conf/const.hpp>
#include <libdnf5/repo/package_downloader.hpp>
Expand Down Expand Up @@ -336,19 +337,26 @@ void AutomaticCommand::run() {
// print resolve logs and the transaction table to the output stream
{
output_stream << std::endl << _("Resolved transaction:") << std::endl;
libdnf5::cli::output::print_resolve_logs(transaction, output_stream);
libdnf5::cli::output::TransactionAdapter cli_output_transaction(*context.get_transaction());
libdnf5::cli::output::print_resolve_logs(
static_cast<libdnf5::cli::output::ITransaction &>(cli_output_transaction), output_stream);

if (!transaction.empty()) {
libdnf5::cli::output::TransactionSummary summary;
auto tt = libdnf5::cli::output::create_transaction_table(transaction, summary);
scols_table_enable_colors(*tt, false);
scols_table_set_termwidth(*tt, 80);
char * tt_string = nullptr;
scols_print_table_to_string(*tt, &tt_string);
output_stream << tt_string << std::endl;
char * tt_string;
size_t size;
auto * fd = open_memstream(&tt_string, &size);
{
libdnf5::cli::output::TransactionTable table(
static_cast<libdnf5::cli::output::ITransaction &>(cli_output_transaction));
table.set_colors_enabled(false);
table.set_term_width(80);
table.set_output_stream(fd);
table.print_table();
table.print_summary();
}
fclose(fd);
output_stream << tt_string;
free(tt_string);

summary.print(output_stream);
}
}

Expand Down
4 changes: 3 additions & 1 deletion dnf5/commands/advisory/advisory_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "advisory_info.hpp"

#include <libdnf5-cli/output/adapters/advisory.hpp>
#include <libdnf5-cli/output/advisoryinfo.hpp>
#include <libdnf5/rpm/package_query.hpp>

Expand Down Expand Up @@ -62,7 +63,8 @@ void AdvisoryInfoCommand::process_and_print_queries(

for (auto advisory : advisories) {
libdnf5::cli::output::AdvisoryInfo advisory_info;
advisory_info.add_advisory(advisory);
output::AdvisoryAdapter cli_advisory(advisory);
advisory_info.add_advisory(cli_advisory);
advisory_info.print();
std::cout << std::endl;
}
Expand Down
13 changes: 12 additions & 1 deletion dnf5/commands/advisory/advisory_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "advisory_list.hpp"

#include <libdnf5-cli/output/adapters/advisory.hpp>
#include <libdnf5-cli/output/advisorylist.hpp>
#include <libdnf5/advisory/advisory_package.hpp>
#include <libdnf5/rpm/package_query.hpp>
Expand Down Expand Up @@ -63,12 +64,22 @@ void AdvisoryListCommand::process_and_print_queries(
not_installed_pkgs = advisories.get_advisory_packages_sorted(installed_packages, libdnf5::sack::QueryCmp::GT);
}

std::vector<std::unique_ptr<libdnf5::cli::output::IAdvisoryPackage>> cli_installed_pkgs;
std::vector<std::unique_ptr<libdnf5::cli::output::IAdvisoryPackage>> cli_not_installed_pkgs;
if (with_bz->get_value()) {
libdnf5::cli::output::print_advisorylist_references_table(not_installed_pkgs, installed_pkgs, "bugzilla");
} else if (with_cve->get_value()) {
libdnf5::cli::output::print_advisorylist_references_table(not_installed_pkgs, installed_pkgs, "cve");
} else {
libdnf5::cli::output::print_advisorylist_table(not_installed_pkgs, installed_pkgs);
cli_installed_pkgs.reserve(installed_pkgs.size());
for (const auto & obj : installed_pkgs) {
cli_installed_pkgs.emplace_back(new output::AdvisoryPackageAdapter(obj));
}
cli_not_installed_pkgs.reserve(not_installed_pkgs.size());
for (const auto & obj : not_installed_pkgs) {
cli_not_installed_pkgs.emplace_back(new output::AdvisoryPackageAdapter(obj));
}
libdnf5::cli::output::print_advisorylist_table(cli_not_installed_pkgs, cli_installed_pkgs);
}
}

Expand Down
6 changes: 5 additions & 1 deletion dnf5/commands/environment/environment_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "environment_info.hpp"

#include <libdnf5-cli/output/adapters/comps.hpp>
#include <libdnf5-cli/output/environmentinfo.hpp>
#include <libdnf5/comps/environment/environment.hpp>
#include <libdnf5/comps/environment/query.hpp>
#include <libdnf5/conf/const.hpp>

#include <iostream>

namespace dnf5 {

using namespace libdnf5::cli;
Expand Down Expand Up @@ -67,7 +70,8 @@ void EnvironmentInfoCommand::run() {
}

for (auto environment : query.list()) {
libdnf5::cli::output::print_environmentinfo_table(environment);
libdnf5::cli::output::EnvironmentAdapter cli_env(environment);
libdnf5::cli::output::print_environmentinfo_table(cli_env);
std::cout << '\n';
}
}
Expand Down
7 changes: 6 additions & 1 deletion dnf5/commands/environment/environment_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "environment_list.hpp"

#include <libdnf5-cli/output/adapters/comps.hpp>
#include <libdnf5-cli/output/environmentlist.hpp>
#include <libdnf5/comps/environment/environment.hpp>
#include <libdnf5/comps/environment/query.hpp>
Expand Down Expand Up @@ -66,7 +67,11 @@ void EnvironmentListCommand::run() {
query.filter_installed(false);
}

libdnf5::cli::output::print_environmentlist_table(query.list());
std::vector<std::unique_ptr<libdnf5::cli::output::IEnvironment>> cli_envs;
for (auto & env : query.list()) {
cli_envs.emplace_back(new libdnf5::cli::output::EnvironmentAdapter(env));
}
libdnf5::cli::output::print_environmentlist_table(cli_envs);
}

} // namespace dnf5
6 changes: 5 additions & 1 deletion dnf5/commands/group/group_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "group_info.hpp"

#include <libdnf5-cli/output/adapters/comps.hpp>
#include <libdnf5-cli/output/groupinfo.hpp>

#include <iostream>

namespace dnf5 {

using namespace libdnf5::cli;

void GroupInfoCommand::print(const libdnf5::comps::GroupQuery & query) {
for (auto group : query.list()) {
libdnf5::cli::output::print_groupinfo_table(group);
libdnf5::cli::output::GroupAdapter cli_group(group);
libdnf5::cli::output::print_groupinfo_table(cli_group);
std::cout << '\n';
}
}
Expand Down
8 changes: 7 additions & 1 deletion dnf5/commands/group/group_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "group_list.hpp"

#include <libdnf5-cli/output/adapters/comps.hpp>
#include <libdnf5-cli/output/grouplist.hpp>
#include <libdnf5/comps/group/group.hpp>
#include <libdnf5/comps/group/query.hpp>
Expand Down Expand Up @@ -90,7 +91,12 @@ void GroupListCommand::run() {
}

void GroupListCommand::print(const libdnf5::comps::GroupQuery & query) {
libdnf5::cli::output::print_grouplist_table(query.list());
std::vector<std::unique_ptr<libdnf5::cli::output::IGroup>> items;
items.reserve(query.list().size());
for (auto & obj : query.list()) {
items.emplace_back(new libdnf5::cli::output::GroupAdapter(obj));
}
libdnf5::cli::output::print_grouplist_table(items);
}

} // namespace dnf5
8 changes: 7 additions & 1 deletion dnf5/commands/module/module_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "module_info.hpp"

#include <libdnf5-cli/output/adapters/module.hpp>
#include <libdnf5-cli/output/moduleinfo.hpp>
#include <libdnf5/module/module_query.hpp>

namespace dnf5 {

void ModuleInfoCommand::print(const libdnf5::module::ModuleQuery & query) {
libdnf5::cli::output::print_moduleinfo_table(query.list());
std::vector<std::unique_ptr<libdnf5::cli::output::IModuleItem>> items;
items.reserve(query.list().size());
for (auto & obj : query.list()) {
items.emplace_back(new libdnf5::cli::output::ModuleItemAdapter(obj));
}
libdnf5::cli::output::print_moduleinfo_table(items);
}

void ModuleInfoCommand::print_hint() {
Expand Down
8 changes: 7 additions & 1 deletion dnf5/commands/module/module_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "module_list.hpp"

#include <libdnf5-cli/output/adapters/module.hpp>
#include <libdnf5-cli/output/modulelist.hpp>
#include <libdnf5/module/module_query.hpp>
#include <libdnf5/utils/bgettext/bgettext-lib.h>
Expand Down Expand Up @@ -87,7 +88,12 @@ void ModuleListCommand::run() {
}

void ModuleListCommand::print(const libdnf5::module::ModuleQuery & query) {
libdnf5::cli::output::print_modulelist_table(query.list());
std::vector<std::unique_ptr<output::IModuleItem>> items;
items.reserve(query.list().size());
for (auto & obj : query.list()) {
items.emplace_back(new output::ModuleItemAdapter(obj));
}
libdnf5::cli::output::print_modulelist_table(items);
}

void ModuleListCommand::print_hint() {
Expand Down
7 changes: 4 additions & 3 deletions dnf5/commands/provides/provides.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/
#include "libdnf5-cli/output/provides.hpp"

#include "provides.hpp"

#include "libdnf5/common/sack/query_cmp.hpp"
#include "libdnf5/conf/option_string.hpp"

#include <libdnf5-cli/output/adapters/package.hpp>
#include <libdnf5-cli/output/provides.hpp>
#include <libdnf5/conf/const.hpp>
#include <libdnf5/rpm/package_query.hpp>

Expand Down Expand Up @@ -116,7 +116,8 @@ void ProvidesCommand::run() {
auto matched = filter_spec(spec, full_package_query);
for (auto package : matched.first) {
if (matched.second != libdnf5::cli::output::ProvidesMatchedBy::NO_MATCH) {
libdnf5::cli::output::print_provides_table(package, spec.c_str(), matched.second);
libdnf5::cli::output::PackageAdapter cli_package(package);
libdnf5::cli::output::print_provides_table(cli_package, spec.c_str(), matched.second);
any_match = true;
} else {
unmatched_specs.insert(spec);
Expand Down
81 changes: 45 additions & 36 deletions dnf5/commands/repo/repo_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,76 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "repo_info.hpp"

#include <libdnf5-cli/output/interfaces/repo.hpp>
#include <libdnf5-cli/output/repo_info.hpp>
#include <libdnf5/rpm/package_query.hpp>

#include <iostream>

namespace dnf5 {

struct RepoInfoWrapper {
class RepoInfoWrapper : public libdnf5::cli::output::IRepoInfo {
public:
RepoInfoWrapper(libdnf5::repo::Repo & repo, uint64_t size, uint64_t pkgs, uint64_t available_pkgs)
: repo(repo),
: repo(&repo),
size(size),
pkgs(pkgs),
available_pkgs(available_pkgs) {}

std::string get_id() const { return repo.get_id(); }
std::string get_name() const { return repo.get_name(); }
std::string get_type() const { return repo.type_to_string(repo.get_type()); }
bool is_enabled() const { return repo.is_enabled(); }
int get_priority() const { return repo.get_config().get_priority_option().get_value(); }
int get_cost() const { return repo.get_config().get_cost_option().get_value(); }
std::vector<std::string> get_baseurl() const { return repo.get_config().get_baseurl_option().get_value(); }
std::string get_metalink() const {
auto & option = repo.get_config().get_metalink_option();
std::string get_id() const override { return repo->get_id(); }
std::string get_name() const override { return repo->get_name(); }
std::string get_type() const override { return libdnf5::repo::Repo::type_to_string(repo->get_type()); }
bool is_enabled() const override { return repo->is_enabled(); }
int get_priority() const override { return repo->get_config().get_priority_option().get_value(); }
int get_cost() const override { return repo->get_config().get_cost_option().get_value(); }
std::vector<std::string> get_baseurl() const override {
return repo->get_config().get_baseurl_option().get_value();
}
std::string get_metalink() const override {
auto & option = repo->get_config().get_metalink_option();
if (option.empty()) {
return "";
} else {
return option.get_value();
}
}
std::string get_mirrorlist() const {
auto & option = repo.get_config().get_mirrorlist_option();
std::string get_mirrorlist() const override {
auto & option = repo->get_config().get_mirrorlist_option();
if (option.empty()) {
return "";
} else {
return option.get_value();
}
}
int get_metadata_expire() const { return repo.get_config().get_metadata_expire_option().get_value(); }
std::vector<std::string> get_excludepkgs() const { return repo.get_config().get_excludepkgs_option().get_value(); }
std::vector<std::string> get_includepkgs() const {
return repo.get_config().get_includepkgs_option().get_value();
int get_metadata_expire() const override { return repo->get_config().get_metadata_expire_option().get_value(); }
std::vector<std::string> get_excludepkgs() const override {
return repo->get_config().get_excludepkgs_option().get_value();
}
std::vector<std::string> get_includepkgs() const override {
return repo->get_config().get_includepkgs_option().get_value();
;
}
bool get_skip_if_unavailable() const { return repo.get_config().get_skip_if_unavailable_option().get_value(); }
std::vector<std::string> get_gpgkey() const { return repo.get_config().get_gpgkey_option().get_value(); }
bool get_gpgcheck() const { return repo.get_config().get_gpgcheck_option().get_value(); }
bool get_repo_gpgcheck() const { return repo.get_config().get_repo_gpgcheck_option().get_value(); }
std::string get_repo_file_path() const { return repo.get_repo_file_path(); }
std::string get_revision() const { return repo.get_revision(); }
std::vector<std::string> get_content_tags() const { return repo.get_content_tags(); }
std::vector<std::pair<std::string, std::string>> get_distro_tags() const { return repo.get_distro_tags(); }
int64_t get_timestamp() const { return repo.get_timestamp(); }
int get_max_timestamp() const { return repo.get_max_timestamp(); }
uint64_t get_size() const { return size; }
uint64_t get_pkgs() const { return pkgs; }
uint64_t get_available_pkgs() const { return available_pkgs; }
std::vector<std::string> get_mirrors() const { return repo.get_mirrors(); }
bool get_skip_if_unavailable() const override {
return repo->get_config().get_skip_if_unavailable_option().get_value();
}
std::vector<std::string> get_gpgkey() const override { return repo->get_config().get_gpgkey_option().get_value(); }
bool get_gpgcheck() const override { return repo->get_config().get_gpgcheck_option().get_value(); }
bool get_repo_gpgcheck() const override { return repo->get_config().get_repo_gpgcheck_option().get_value(); }
std::string get_repo_file_path() const override { return repo->get_repo_file_path(); }
std::string get_revision() const override { return repo->get_revision(); }
std::vector<std::string> get_content_tags() const override { return repo->get_content_tags(); }
std::vector<std::pair<std::string, std::string>> get_distro_tags() const override {
return repo->get_distro_tags();
}
int64_t get_timestamp() const override { return repo->get_timestamp(); }
int get_max_timestamp() const override { return repo->get_max_timestamp(); }
uint64_t get_size() const override { return size; }
uint64_t get_pkgs() const override { return pkgs; }
uint64_t get_available_pkgs() const override { return available_pkgs; }
std::vector<std::string> get_mirrors() const override { return repo->get_mirrors(); }

private:
libdnf5::repo::Repo & repo;
libdnf5::repo::Repo * repo;
uint64_t size;
uint64_t pkgs;
uint64_t available_pkgs;
Expand Down Expand Up @@ -110,10 +119,10 @@ void RepoInfoCommand::print(const libdnf5::repo::RepoQuery & query, [[maybe_unus
repo_size += pkg.get_download_size();
}

libdnf5::cli::output::RepoInfo repo_info;
auto repo_wrapper = RepoInfoWrapper(*repo, repo_size, pkgs.size(), available_pkgs.size());
repo_info.add_repo(repo_wrapper);
repo_info.print();
libdnf5::cli::output::RepoInfo repo_info_table;
RepoInfoWrapper repo_wrapper(*repo, repo_size, pkgs.size(), available_pkgs.size());
repo_info_table.add_repo(repo_wrapper);
repo_info_table.print();
std::cout << std::endl;
}
}
Expand Down
Loading

0 comments on commit 8a6edc7

Please sign in to comment.