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 authored and ppisar committed Jan 25, 2024
1 parent 6e51bf2 commit 7e2b964
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
32 changes: 29 additions & 3 deletions dnf5/commands/download/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,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", "file"};
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|ftp|file},...");
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,9 +180,16 @@ 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());
auto urls = pkg.get_remote_locations(urlprotocol_option);
if (urls.empty()) {
ctx.base.get_logger()->warning("Failed to get mirror for package: \"{}\"", pkg.get_name());
continue;
}
std::cout << urls[0] << 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 protocols: ``http``, ``https``, ``ftp``, or ``file``. This option can be used multiple times.


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
4 changes: 2 additions & 2 deletions libdnf5/rpm/package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ std::vector<std::string> Package::get_remote_locations(const std::set<std::strin
auto repo_mirrors = get_repo()->get_mirrors();
for (const auto & mirror : repo_mirrors) {
for (const auto & protocol : protocols) {
if (utils::string::starts_with(mirror, protocol)) {
if (utils::string::starts_with(mirror, protocol + ":")) {
auto path = lr_pathconcat(mirror.c_str(), location.c_str(), NULL);
remote_locations.emplace_back(path);
lr_free(path);
Expand All @@ -341,7 +341,7 @@ std::vector<std::string> Package::get_remote_locations(const std::set<std::strin
auto repo_baseurls = get_repo()->get_config().get_baseurl_option().get_value();
for (const auto & baseurl : repo_baseurls) {
for (const auto & protocol : protocols) {
if (utils::string::starts_with(baseurl, protocol)) {
if (utils::string::starts_with(baseurl, protocol + ":")) {
auto path = lr_pathconcat(baseurl.c_str(), location.c_str(), NULL);
remote_locations.emplace_back(path);
lr_free(path);
Expand Down

0 comments on commit 7e2b964

Please sign in to comment.