From 440469681c8ec06884dcf8dbd73c2ab9105eeed1 Mon Sep 17 00:00:00 2001 From: Jan Kolarik Date: Wed, 21 Feb 2024 15:14:53 +0000 Subject: [PATCH] dnf5daemon: Add repo enable/disable client commands Also create parent command where all related repo subcommands are located. --- dnf5daemon-client/commands/repo/repo.cpp | 50 +++++++++++++++ dnf5daemon-client/commands/repo/repo.hpp | 40 ++++++++++++ .../commands/repo/repo_disable.hpp | 35 +++++++++++ .../commands/repo/repo_enable.hpp | 35 +++++++++++ .../commands/repo/repo_enable_disable.cpp | 62 +++++++++++++++++++ .../commands/repo/repo_enable_disable.hpp | 42 +++++++++++++ dnf5daemon-client/main.cpp | 3 + 7 files changed, 267 insertions(+) create mode 100644 dnf5daemon-client/commands/repo/repo.cpp create mode 100644 dnf5daemon-client/commands/repo/repo.hpp create mode 100644 dnf5daemon-client/commands/repo/repo_disable.hpp create mode 100644 dnf5daemon-client/commands/repo/repo_enable.hpp create mode 100644 dnf5daemon-client/commands/repo/repo_enable_disable.cpp create mode 100644 dnf5daemon-client/commands/repo/repo_enable_disable.hpp diff --git a/dnf5daemon-client/commands/repo/repo.cpp b/dnf5daemon-client/commands/repo/repo.cpp new file mode 100644 index 000000000..56c671346 --- /dev/null +++ b/dnf5daemon-client/commands/repo/repo.cpp @@ -0,0 +1,50 @@ +/* +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 . +*/ + +#include "repo.hpp" + +#include "repo_disable.hpp" +#include "repo_enable.hpp" + +namespace dnfdaemon::client { + +void RepoCommand::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 RepoCommand::set_argument_parser() { + get_argument_parser_command()->set_description("Manage repositories"); +} + +void RepoCommand::register_subcommands() { + auto * config_commands_repo = get_context().get_argument_parser().add_new_group("repo_config_commands"); + config_commands_repo->set_header("Configuration Commands:"); + get_argument_parser_command()->register_group(config_commands_repo); + register_subcommand(std::make_unique(get_context()), config_commands_repo); + register_subcommand(std::make_unique(get_context()), config_commands_repo); +} + +void RepoCommand::pre_configure() { + throw_missing_command(); +} + +} // namespace dnfdaemon::client diff --git a/dnf5daemon-client/commands/repo/repo.hpp b/dnf5daemon-client/commands/repo/repo.hpp new file mode 100644 index 000000000..ff182a698 --- /dev/null +++ b/dnf5daemon-client/commands/repo/repo.hpp @@ -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 . +*/ + +#ifndef DNF5DAEMON_CLIENT_COMMANDS_REPO_REPO_HPP +#define DNF5DAEMON_CLIENT_COMMANDS_REPO_REPO_HPP + +#include "commands/command.hpp" +#include "context.hpp" + +namespace dnfdaemon::client { + +class RepoCommand : public DaemonCommand { +public: + explicit RepoCommand(Context & context) : DaemonCommand(context, "repo") {} + void set_parent_command() override; + void set_argument_parser() override; + void register_subcommands() override; + void pre_configure() override; +}; + +} // namespace dnfdaemon::client + + +#endif // DNF5DAEMON_CLIENT_COMMANDS_REPO_REPO_HPP diff --git a/dnf5daemon-client/commands/repo/repo_disable.hpp b/dnf5daemon-client/commands/repo/repo_disable.hpp new file mode 100644 index 000000000..fa0a7aa28 --- /dev/null +++ b/dnf5daemon-client/commands/repo/repo_disable.hpp @@ -0,0 +1,35 @@ +/* +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 . +*/ + +#ifndef DNF5DAEMON_CLIENT_COMMANDS_REPO_DISABLE_HPP +#define DNF5DAEMON_CLIENT_COMMANDS_REPO_DISABLE_HPP + +#include "commands/command.hpp" +#include "repo_enable_disable.hpp" + +namespace dnfdaemon::client { + +class RepoDisableCommand : public RepoEnableDisableCommand { +public: + explicit RepoDisableCommand(Context & context) : RepoEnableDisableCommand(context, "disable") {} +}; + +} // namespace dnfdaemon::client + +#endif // DNF5DAEMON_CLIENT_COMMANDS_REPO_DISABLE_HPP diff --git a/dnf5daemon-client/commands/repo/repo_enable.hpp b/dnf5daemon-client/commands/repo/repo_enable.hpp new file mode 100644 index 000000000..961f8e8ee --- /dev/null +++ b/dnf5daemon-client/commands/repo/repo_enable.hpp @@ -0,0 +1,35 @@ +/* +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 . +*/ + +#ifndef DNF5DAEMON_CLIENT_COMMANDS_REPO_ENABLE_HPP +#define DNF5DAEMON_CLIENT_COMMANDS_REPO_ENABLE_HPP + +#include "commands/command.hpp" +#include "repo_enable_disable.hpp" + +namespace dnfdaemon::client { + +class RepoEnableCommand : public RepoEnableDisableCommand { +public: + explicit RepoEnableCommand(Context & context) : RepoEnableDisableCommand(context, "enable") {} +}; + +} // namespace dnfdaemon::client + +#endif // DNF5DAEMON_CLIENT_COMMANDS_REPO_ENABLE_HPP diff --git a/dnf5daemon-client/commands/repo/repo_enable_disable.cpp b/dnf5daemon-client/commands/repo/repo_enable_disable.cpp new file mode 100644 index 000000000..99ba89dfd --- /dev/null +++ b/dnf5daemon-client/commands/repo/repo_enable_disable.cpp @@ -0,0 +1,62 @@ +/* +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 . +*/ + +#include "repo_enable_disable.hpp" + +#include + +namespace dnfdaemon::client { + +using namespace libdnf5::cli; + +void RepoEnableDisableCommand::set_argument_parser() { + auto & parser = get_context().get_argument_parser(); + auto & cmd = *get_argument_parser_command(); + + patterns_options = parser.add_new_values(); + auto repos = parser.add_new_positional_arg( + "repo_ids", + libdnf5::cli::ArgumentParser::PositionalArg::UNLIMITED, + parser.add_init_value(std::unique_ptr(new libdnf5::OptionString(nullptr))), + patterns_options); + + repos->set_description(fmt::format("List of repos to be {}d", command)); + cmd.set_description(fmt::format("{} given repositories", command)); + cmd.register_positional_arg(repos); +} + +void RepoEnableDisableCommand::run() { + auto & ctx = get_context(); + + std::vector patterns; + if (!patterns_options->empty()) { + patterns.reserve(patterns_options->size()); + for (auto & pattern : *patterns_options) { + auto option = dynamic_cast(pattern.get()); + patterns.emplace_back(option->get_value()); + } + } + + ctx.session_proxy->callMethod(command) + .onInterface(dnfdaemon::INTERFACE_REPO) + .withTimeout(static_cast(-1)) + .withArguments(patterns); +} + +} // namespace dnfdaemon::client diff --git a/dnf5daemon-client/commands/repo/repo_enable_disable.hpp b/dnf5daemon-client/commands/repo/repo_enable_disable.hpp new file mode 100644 index 000000000..377be3f44 --- /dev/null +++ b/dnf5daemon-client/commands/repo/repo_enable_disable.hpp @@ -0,0 +1,42 @@ +/* +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 . +*/ + +#ifndef DNF5DAEMON_CLIENT_COMMANDS_REPO_ENABLE_DISABLE_HPP +#define DNF5DAEMON_CLIENT_COMMANDS_REPO_ENABLE_DISABLE_HPP + +#include "commands/command.hpp" + +namespace dnfdaemon::client { + +class RepoEnableDisableCommand : public DaemonCommand { +public: + explicit RepoEnableDisableCommand(Context & context, const std::string & command) + : DaemonCommand(context, command), + command(command) {} + void set_argument_parser() override; + void run() override; + +private: + std::vector> * patterns_options{nullptr}; + const std::string command; +}; + +} // namespace dnfdaemon::client + +#endif // DNF5DAEMON_CLIENT_COMMANDS_REPO_ENABLE_DISABLE_HPP diff --git a/dnf5daemon-client/main.cpp b/dnf5daemon-client/main.cpp index a839c7fe7..c679f0acc 100644 --- a/dnf5daemon-client/main.cpp +++ b/dnf5daemon-client/main.cpp @@ -24,6 +24,7 @@ along with libdnf. If not, see . #include "commands/install/install.hpp" #include "commands/reinstall/reinstall.hpp" #include "commands/remove/remove.hpp" +#include "commands/repo/repo.hpp" #include "commands/repolist/repolist.hpp" #include "commands/repoquery/repoquery.hpp" #include "commands/upgrade/upgrade.hpp" @@ -202,6 +203,8 @@ static void add_commands(Context & context) { context.add_and_initialize_command(std::make_unique(context)); + context.add_and_initialize_command(std::make_unique(context)); + context.add_and_initialize_command(std::make_unique(context, "repolist")); context.add_and_initialize_command(std::make_unique(context, "repoinfo"));