Skip to content

Commit

Permalink
Added urlprotocol
Browse files Browse the repository at this point in the history
  • Loading branch information
derickdiaz committed Jan 21, 2024
1 parent 70521e0 commit 31411c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 32 additions & 1 deletion dnf5/commands/download/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <libdnf5/rpm/package_set.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>

#include <algorithm>
#include <format>
#include <iostream>
#include <map>

Expand Down Expand Up @@ -86,11 +88,25 @@ void DownloadCommand::set_argument_parser() {
url->set_const_value("true");
url->link_value(url_option);

auto urlprotocol = parser.add_new_named_arg("urlprotocol");
urlprotocol->set_long_name("urlprotocol");
urlprotocol->set_description("When running with --url, limit to specific protocols");
urlprotocol->set_parse_hook_func(
[&ctx](
[[maybe_unused]] ArgumentParser::NamedArg * arg, [[maybe_unused]] const char * option, const char * value) {
if (urlprotocol_valid_options.find(value) == urlprotocol_valid_options.end()) {
throw libdnf5::cli::ArgumentParserInvalidValueError(
_M(std::format("Invalid urlprotocol option: {}", value)))
}
urlprotocol_option.emplace_back(value);
});

cmd.register_named_arg(alldeps);
create_destdir_option(*this);
cmd.register_named_arg(resolve);
cmd.register_positional_arg(keys);
cmd.register_named_arg(url);
cmd.register_named_arg(urlprotocol);
}

void DownloadCommand::configure() {
Expand Down Expand Up @@ -161,10 +177,25 @@ void DownloadCommand::run() {
}

if (url_option->get_value()) {
// If no urlprotocols are specified, then any urlprotocol is acceptable
if (urlprotocol_option.empty()) {
urlprotocol_option = urlprotocol_valid_options
}
for (auto & [nerva, pkg] : download_pkgs) {
auto urls = pkg.get_remote_locations();
libdnf_assert(!urls.empty(), "Failed to get mirror for package: \"{}\"", pkg.get_name());
std::cout << urls[0] << std::endl;
auto valid_url = std::find_if(urls.begin(), urls.end(), [&](std::string url) {
for (auto protocol : urlprotocols) {
if (url.starts_with(protocol)) {
return true;
}
}
return false;
});
if (valid_url == urls.end()) {
libdnf_assert(true, "Failed to get mirror for package: \"{}\"", pkg.get_name());
}
std::cout << *valid_url << std::endl;
}
return;
}
Expand Down
3 changes: 3 additions & 0 deletions dnf5/commands/download/download.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <libdnf5/conf/option.hpp>

#include <memory>
#include <set>
#include <vector>


Expand All @@ -40,6 +41,8 @@ class DownloadCommand : public Command {
void run() override;

private:
std::set<std::string> urlprotocol_valid_options = {"http", "https", "rsync", "ftp"};
std::set<std::string> urlprotocol_option = {};
libdnf5::OptionBool * resolve_option{nullptr};
libdnf5::OptionBool * alldeps_option{nullptr};
libdnf5::OptionBool * url_option{nullptr};
Expand Down

0 comments on commit 31411c2

Please sign in to comment.