Skip to content

Commit

Permalink
Add repository_url option for buf dowloads to allow mirroring (#40)
Browse files Browse the repository at this point in the history
Closes #39 
Allow configuring a mirror for `buf` binary downloads
  • Loading branch information
Andrius-B authored Nov 1, 2023
1 parent a61c186 commit ad80bdc
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions buf/internal/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def _detect_host_platform(ctx):

def _buf_download_releases_impl(ctx):
version = ctx.attr.version
repository_url = ctx.attr.repository_url
sha256 = ctx.attr.sha256
if not version:
ctx.report_progress("Finding latest buf version")
Expand All @@ -159,7 +160,7 @@ def _buf_download_releases_impl(ctx):
ctx.report_progress("Downloading buf release hash")
sha256 = ctx.download(
url = [
"https://github.com/bufbuild/buf/releases/download/{}/sha256.txt".format(version),
"{}/{}/sha256.txt".format(repository_url, version),
],
sha256 = sha256,
output = "sha256.txt",
Expand All @@ -182,7 +183,7 @@ def _buf_download_releases_impl(ctx):

ctx.report_progress("Downloading " + bin)
download_info = ctx.download(
url = "https://github.com/bufbuild/buf/releases/download/{}/{}".format(version, bin),
url = "{}/{}/{}".format(repository_url, version, bin),
sha256 = sum,
executable = True,
output = output,
Expand All @@ -207,22 +208,28 @@ _buf_download_releases = repository_rule(
"version": attr.string(
doc = "Buf release version",
),
"repository_url": attr.string(
doc = "Repository url base used for downloads",
default = "https://github.com/bufbuild/buf/releases/download",
),
"sha256": attr.string(
doc = "Buf release sha256.txt checksum",
),
},
)

# buildifier: disable=unnamed-macro
def rules_buf_toolchains(name = _TOOLCHAINS_REPO, version = None, sha256 = None):
def rules_buf_toolchains(name = _TOOLCHAINS_REPO, version = None, sha256 = None, repository_url = None):
"""rules_buf_toolchains sets up toolchains for buf, protoc-gen-buf-lint, and protoc-gen-buf-breaking
Args:
name: The name of the toolchains repository. Defaults to "buf_toolchains"
version: Release version, eg: `v.1.0.0-rc12`. If `None` defaults to latest
sha256: The checksum sha256.txt file.
repository_url: The repository url base used for downloads. Defaults to "https://github.com/bufbuild/buf/releases/download"
"""

_buf_download_releases(name = name, version = version, sha256 = sha256)
_buf_download_releases(name = name, version = version, sha256 = sha256, repository_url = repository_url)

_register_toolchains(name, "buf")
_register_toolchains(name, "protoc-gen-buf-breaking")
Expand Down

0 comments on commit ad80bdc

Please sign in to comment.