From a5efb0efd19eb25e3f780b17e1bcb4d8444d6757 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 29 Aug 2023 11:43:45 -0700 Subject: [PATCH] fix: no-progress-meter only on newer curl (#344) --- oci/private/BUILD.bazel | 5 ++--- oci/private/download.bzl | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/oci/private/BUILD.bazel b/oci/private/BUILD.bazel index b2b68ecb..f7329ec2 100644 --- a/oci/private/BUILD.bazel +++ b/oci/private/BUILD.bazel @@ -82,9 +82,8 @@ bzl_library( bzl_library( name = "download", srcs = ["download.bzl"], - visibility = [ - "//oci:__subpackages__", - ], + visibility = ["//oci:__subpackages__"], + deps = ["@bazel_skylib//lib:versions"], ) bzl_library( diff --git a/oci/private/download.bzl b/oci/private/download.bzl index 2a1162bf..9f111ae7 100644 --- a/oci/private/download.bzl +++ b/oci/private/download.bzl @@ -1,6 +1,7 @@ "Downloader functions " load("@aspect_bazel_lib//lib:base64.bzl", "base64") +load("@bazel_skylib//lib:versions.bzl", "versions") def _auth_to_header(url, auth): for auth_url in auth: @@ -70,6 +71,16 @@ def _download( _debug("{} is in cache".format(url)) return cache_result + version_result = rctx.execute(["curl", "--version"]) + if version_result.return_code != 0: + fail("Failed to execute curl --version:\n{}".format(version_result.stderr)) + + # parse from + # curl 8.1.2 (x86_64-apple-darwin22.0) libcurl/8.1.2 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.11 nghttp2/1.51.0 + # Release-Date: 2023-05-30 + # ... + curl_version = version_result.stdout.split(" ")[1] + headers_output_path = str(rctx.path(".output/header.txt")) output_path = str(rctx.path(".output/{}".format(output))) command = [ @@ -78,7 +89,6 @@ def _download( "--write-out", "%{http_code}", "--location", - "--no-progress-meter", "--request", method, "--create-dirs", @@ -87,6 +97,12 @@ def _download( "--dump-header", headers_output_path, ] + + # Detect more flags which may be supported based on changelog: + # https://curl.se/changes.html + if versions.is_at_least("7.67.0", curl_version): + command.append("--no-progress-meter") + for (name, value) in headers.items(): command.append("--header") command.append("{}: {}".format(name, value)) @@ -101,7 +117,7 @@ def _download( if allow_fail: return struct(success = False) else: - fail("Failed to execute curl {} {}".format(url, result.stderr)) + fail("Failed to execute curl {} (version {}): {}".format(url, curl_version, result.stderr)) status_code = int(result.stdout.strip()) if status_code >= 400: