Skip to content

Commit

Permalink
Read .netrc in go_repository (#1090)
Browse files Browse the repository at this point in the history
Fixes #847
  • Loading branch information
linzhp committed Aug 10, 2021
1 parent 0709f36 commit d7082fb
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,29 @@

load("@io_bazel_rules_go//go/private:common.bzl", "env_execute", "executable_extension")
load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc")

# We can't disable timeouts on Bazel, but we can set them to large values.
_GO_REPOSITORY_TIMEOUT = 86400

# Inspired by @bazel_tools//tools/build_defs/repo:http.bzl
def _get_auth(ctx, urls):
"""Given the list of URLs obtain the correct auth dict."""
netrcfile = ""
if "NETRC" in ctx.os.environ:
netrcfile = ctx.os.environ["NETRC"]
elif ctx.os.name.startswith("windows"):
if "USERPROFILE" in ctx.os.environ:
netrcfile = "%s/_netrc" % (ctx.os.environ["USERPROFILE"])
elif "HOME" in ctx.os.environ:
netrcfile = "%s/.netrc" % (ctx.os.environ["HOME"])

if netrcfile and ctx.path(netrcfile).exists:
netrc = read_netrc(ctx, netrcfile)
return use_netrc(netrc, urls, {})

return {}

def _go_repository_impl(ctx):
# TODO(#549): vcs repositories are not cached and still need to be fetched.
# Download the repository or module.
Expand All @@ -33,6 +52,7 @@ def _go_repository_impl(ctx):
sha256 = ctx.attr.sha256,
stripPrefix = ctx.attr.strip_prefix,
type = ctx.attr.type,
auth = _get_auth(ctx, ctx.attr.urls),
)
elif ctx.attr.commit or ctx.attr.tag:
# repository mode
Expand Down

0 comments on commit d7082fb

Please sign in to comment.