Skip to content

Commit

Permalink
Add --urlprotocol option to download command
Browse files Browse the repository at this point in the history
  • Loading branch information
derickdiaz committed Jan 24, 2024
1 parent 1461a37 commit 88664a0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
41 changes: 39 additions & 2 deletions dnf5/commands/download/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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 <iostream>
#include <map>

Expand Down Expand Up @@ -82,15 +83,34 @@ void DownloadCommand::set_argument_parser() {

auto url = parser.add_new_named_arg("url");
url->set_long_name("url");
url->set_description("Print the list of urls where the rpms can be downloaded instead of downloading");
url->set_description("Print a url where the rpms can be downloaded instead of downloading");
url->set_const_value("true");
url->link_value(url_option);

urlprotocol_valid_options = {"http", "https", "ftp"};
urlprotocol_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_has_value(true);
urlprotocol->set_arg_value_help("{http, https, rsync, ftp},...");
urlprotocol->set_parse_hook_func(
[this](
[[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_("Invalid urlprotocol option: {}"), std::string(value));
}
urlprotocol_option.emplace(value);
return true;
});

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 +181,27 @@ void DownloadCommand::run() {
}

if (url_option->get_value()) {
// If no urlprotocols are specified, all values within the urlprotocol_valid_options will be used
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(), [this](std::string url) {
for (auto protocol : urlprotocol_option) {
// Ensure the urls matches the protocol specified by concating the colon.
// i.e https: or http: not httpnextgen:
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;
std::set<std::string> urlprotocol_option;
libdnf5::OptionBool * resolve_option{nullptr};
libdnf5::OptionBool * alldeps_option{nullptr};
libdnf5::OptionBool * url_option{nullptr};
Expand Down
13 changes: 12 additions & 1 deletion doc/commands/download.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ Options
| To be used together with ``--resolve``, it downloads all dependencies, not skipping the already installed ones.
``--destdir=<path>``
Set directory used for downloading packages to. Default location is to the current working directory.
| Set directory used for downloading packages to. Default location is to the current working directory.
``--url``
| Prints the list of urls where the rpms can be downloaded instead of downloading.
``--urlprotocol``
| To be used together with ``--url``, it filters out the urls to the specified url protocols: {http, https, ftp}


Examples
Expand All @@ -63,6 +70,10 @@ Examples
``dnf5 download --destdir /tmp/my_packages maven-compiler-plugin``
| Download the ``maven-compiler-plugin`` package to ``/tmp/my_packages`` directory.
``dnf5 download --url --urlprotocol http python``
| List the http url to download the python package

See Also
========

Expand Down

0 comments on commit 88664a0

Please sign in to comment.