Skip to content

Commit

Permalink
dnf5daemon-client: advisory command
Browse files Browse the repository at this point in the history
  • Loading branch information
m-blaha committed Jun 30, 2023
1 parent a375c71 commit 7397fe7
Show file tree
Hide file tree
Showing 10 changed files with 753 additions and 1 deletion.
48 changes: 48 additions & 0 deletions dnf5daemon-client/commands/advisory/advisory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright Contributors to the libdnf project.
This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 "advisory.hpp"

#include "advisory_list.hpp"

namespace dnfdaemon::client {

void AdvisoryCommand::set_parent_command() {
auto * arg_parser_parent_cmd = get_session().get_argument_parser().get_root_command();
auto * arg_parser_this_cmd = get_argument_parser_command();
arg_parser_parent_cmd->register_command(arg_parser_this_cmd);
arg_parser_parent_cmd->get_group("subcommands").register_argument(arg_parser_this_cmd);
}

void AdvisoryCommand::set_argument_parser() {
get_argument_parser_command()->set_description("Manage advisories");
}

void AdvisoryCommand::register_subcommands() {
auto * query_commands_advisory = get_context().get_argument_parser().add_new_group("advisory_query_commands");
query_commands_advisory->set_header("Query Commands:");
get_argument_parser_command()->register_group(query_commands_advisory);
register_subcommand(std::make_unique<AdvisoryListCommand>(get_context()), query_commands_advisory);
}

void AdvisoryCommand::pre_configure() {
throw_missing_command();
}

} // namespace dnfdaemon::client
40 changes: 40 additions & 0 deletions dnf5daemon-client/commands/advisory/advisory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright Contributors to the libdnf project.
This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
*/

#ifndef DNF5_COMMANDS_ADVISORY_ADVISORY_HPP
#define DNF5_COMMANDS_ADVISORY_ADVISORY_HPP

#include "commands/command.hpp"
#include "context.hpp"

namespace dnfdaemon::client {

class AdvisoryCommand : public DaemonCommand {
public:
explicit AdvisoryCommand(Context & context) : DaemonCommand(context, "advisory") {}
void set_parent_command() override;
void set_argument_parser() override;
void register_subcommands() override;
void pre_configure() override;
};

} // namespace dnfdaemon::client


#endif // DNF5_COMMANDS_ADVISORY_ADVISORY_HPP
60 changes: 60 additions & 0 deletions dnf5daemon-client/commands/advisory/advisory_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright Contributors to the libdnf project.
This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 "advisory_list.hpp"

#include "../../wrappers/dbus_advisory_wrapper.hpp"

#include <libdnf5-cli/output/advisorylist.hpp>

#include <iostream>

namespace dnfdaemon::client {

void AdvisoryListCommand::process_and_print_queries(const std::vector<DbusAdvisoryWrapper> & advisories) {
std::vector<DbusAdvisoryPackageWrapper> installed_pkgs;
std::vector<DbusAdvisoryPackageWrapper> not_installed_pkgs;

for (const auto & advisory : advisories) {
/*XXX
std::cout << advisory.get_name() << " " << advisory.get_type() << std::endl;
for (const auto & ref : advisory.get_references()) {
std::cout << " REF:" << ref.get_id() << "/" << ref.get_type() << "/" << ref.get_title() << "/" << ref.get_url() << std::endl;
}
*/
for (const auto & col : advisory.get_collections()) {
for (const auto & pkg : col.get_packages()) {
auto applicability = pkg.get_applicability();
if (applicability == "installed") {
installed_pkgs.push_back(pkg);
} else if (applicability == "available") {
not_installed_pkgs.push_back(pkg);
}
}
}
}

libdnf5::cli::output::print_advisorylist_table(not_installed_pkgs, installed_pkgs);
}

void AdvisoryListCommand::pre_configure() {
advisory_attrs = std::vector<std::string>{"name", "type", "severity", "buildtime", "collections", "references"};
}

} // namespace dnfdaemon::client
44 changes: 44 additions & 0 deletions dnf5daemon-client/commands/advisory/advisory_list.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright Contributors to the libdnf project.
This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
*/

#ifndef DNF5_COMMANDS_ADVISORY_ADVISORY_LIST_HPP
#define DNF5_COMMANDS_ADVISORY_ADVISORY_LIST_HPP

#include "../../wrappers/dbus_advisory_wrapper.hpp"
#include "advisory_subcommand.hpp"

namespace dnfdaemon::client {

class AdvisoryListCommand : public AdvisorySubCommand {
public:
explicit AdvisoryListCommand(Context & context) : AdvisorySubCommand(context, "list") {}

void set_argument_parser() override {
AdvisorySubCommand::set_argument_parser();
get_argument_parser_command()->set_description(_("List advisories"));
}
void pre_configure() override;

protected:
void process_and_print_queries(const std::vector<DbusAdvisoryWrapper> & advisories) override;
};

} // namespace dnfdaemon::client

#endif // DNF5_COMMANDS_ADVISORY_ADVISORY_LIST_HPP
126 changes: 126 additions & 0 deletions dnf5daemon-client/commands/advisory/advisory_subcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright Contributors to the libdnf project.
This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 "advisory_subcommand.hpp"

#include "../../wrappers/dbus_advisory_wrapper.hpp"
#include "context.hpp"

#include <dnf5daemon-server/dbus.hpp>

namespace dnfdaemon::client {

using namespace libdnf5::cli;

void AdvisorySubCommand::set_argument_parser() {
all = std::make_unique<AdvisoryAllOption>(*this);
available = std::make_unique<AdvisoryAvailableOption>(*this);
installed = std::make_unique<AdvisoryInstalledOption>(*this);
updates = std::make_unique<AdvisoryUpdatesOption>(*this);
all->arg->add_conflict_argument(*available->arg);
all->arg->add_conflict_argument(*installed->arg);
all->arg->add_conflict_argument(*updates->arg);
available->arg->add_conflict_argument(*installed->arg);
available->arg->add_conflict_argument(*updates->arg);
installed->arg->add_conflict_argument(*updates->arg);

contains_pkgs = std::make_unique<AdvisoryContainsPkgsOption>(*this);
advisory_security = std::make_unique<SecurityOption>(*this);
advisory_bugfix = std::make_unique<BugfixOption>(*this);
advisory_enhancement = std::make_unique<EnhancementOption>(*this);
advisory_newpackage = std::make_unique<NewpackageOption>(*this);
advisory_severity = std::make_unique<AdvisorySeverityOption>(*this);
advisory_bz = std::make_unique<BzOption>(*this);
advisory_cve = std::make_unique<CveOption>(*this);
with_bz = std::make_unique<AdvisoryWithBzOption>(*this);
with_cve = std::make_unique<AdvisoryWithCveOption>(*this);

advisory_specs = std::make_unique<AdvisorySpecArguments>(*this);
}

dnfdaemon::KeyValueMap AdvisorySubCommand::session_config() {
dnfdaemon::KeyValueMap cfg = {};
cfg["load_system_repo"] = true;
cfg["load_available_repos"] = true;
// XXX cfg["optional_metadata_types"].add_item(libdnf5::METADATA_TYPE_UPDATEINFO);
return cfg;
}

void AdvisorySubCommand::run() {
auto & ctx = get_context();

// convert arguments to dbus call options
dnfdaemon::KeyValueMap options = {};

options["spec"] = advisory_specs->get_value();

// by default return available advisories
std::string availability = "available";
if (all->get_value()) {
availability = "all";
} else if (installed->get_value()) {
availability = "installed";
} else if (updates->get_value()) {
availability = "updates";
}
options["availability"] = availability; // string

// advisory types
std::vector<std::string> advisory_type{};
if (advisory_security->get_value()) {
advisory_type.emplace_back("security");
}
if (advisory_bugfix->get_value()) {
advisory_type.emplace_back("bugfix");
}
if (advisory_enhancement->get_value()) {
advisory_type.emplace_back("enhancement");
}
if (advisory_newpackage->get_value()) {
advisory_type.emplace_back("newpackage");
}
options["type"] = advisory_type; // vector<string>

options["contains_pkgs"] = contains_pkgs->get_value(); // vector<string>
options["severity"] = advisory_severity->get_value(); // vector<string>
options["reference_bz"] = advisory_bz->get_value(); // vector<string>
options["reference_cve"] = advisory_cve->get_value(); // vector<string>
options["with_bz"] = with_bz->get_value(); // bool
options["with_cve"] = with_cve->get_value(); // bool

options["advisory_attrs"] = advisory_attrs;

// call the server
dnfdaemon::KeyValueMapList raw_advisories;
ctx.session_proxy->callMethod("list")
.onInterface(dnfdaemon::INTERFACE_ADVISORY)
.withTimeout(static_cast<uint64_t>(-1))
.withArguments(options)
.storeResultsTo(raw_advisories);

std::vector<DbusAdvisoryWrapper> advisories;
advisories.reserve(raw_advisories.size());
for (const auto & raw_advisory : raw_advisories) {
advisories.emplace_back(raw_advisory);
}

process_and_print_queries(advisories);
}

} // namespace dnfdaemon::client
64 changes: 64 additions & 0 deletions dnf5daemon-client/commands/advisory/advisory_subcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright Contributors to the libdnf project.
This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
*/

#ifndef DNF5_COMMANDS_ADVISORY_ADVISORY_SUBCOMMAND_HPP
#define DNF5_COMMANDS_ADVISORY_ADVISORY_SUBCOMMAND_HPP

#include "../../wrappers/dbus_advisory_wrapper.hpp"
#include "arguments.hpp"
#include "commands/command.hpp"
#include "context.hpp"

#include <memory>

namespace dnfdaemon::client {

class AdvisorySubCommand : public DaemonCommand {
public:
void set_argument_parser() override;
dnfdaemon::KeyValueMap session_config() override;
void run() override;

protected:
AdvisorySubCommand(Context & context, const std::string & name) : DaemonCommand(context, name) {}
virtual void process_and_print_queries(const std::vector<DbusAdvisoryWrapper> & advisories) = 0;

std::unique_ptr<AdvisoryAvailableOption> available{nullptr};
std::unique_ptr<AdvisoryInstalledOption> installed{nullptr};
std::unique_ptr<AdvisoryAllOption> all{nullptr};
std::unique_ptr<AdvisoryUpdatesOption> updates{nullptr};
std::unique_ptr<AdvisoryContainsPkgsOption> contains_pkgs{nullptr};
std::unique_ptr<AdvisorySpecArguments> advisory_specs{nullptr};
std::unique_ptr<AdvisoryWithBzOption> with_bz{nullptr};
std::unique_ptr<AdvisoryWithCveOption> with_cve{nullptr};

std::unique_ptr<SecurityOption> advisory_security{nullptr};
std::unique_ptr<BugfixOption> advisory_bugfix{nullptr};
std::unique_ptr<EnhancementOption> advisory_enhancement{nullptr};
std::unique_ptr<NewpackageOption> advisory_newpackage{nullptr};
std::unique_ptr<AdvisorySeverityOption> advisory_severity{nullptr};
std::unique_ptr<BzOption> advisory_bz{nullptr};
std::unique_ptr<CveOption> advisory_cve{nullptr};

std::vector<std::string> advisory_attrs{};
};

} // namespace dnfdaemon::client

#endif // DNF5_COMMANDS_ADVISORY_ADVISORY_SUBCOMMAND_HPP
Loading

0 comments on commit 7397fe7

Please sign in to comment.