管理通过非标准端口访问的自定义Go模块

huangapple go评论95阅读模式
英文:

Managing custom Go modules accessed via non-standard ports

问题

背景

在我们公司,我们使用 Bit Bucket 来托管我们的 git 仓库。所有流向服务器的流量都通过一个自定义的非标准端口。从我们的仓库克隆的命令看起来像是 git clone ssh://git@stash.company.com:9999/repo/path/name.git

问题

我想要在这个服务器上创建由 go mod 管理的 Go 模块,然而,流量必须通过端口 9999 流动,这使得这个过程非常困难。这是因为 go mod 在标准端口上运行,并且似乎没有提供一种为不同的模块自定义端口的方法。

我的问题

是否可以使用 go mod 来管理托管在私有 git 服务器上、使用非标准端口的 Go 模块?

尝试的解决方案

Vendoring

这似乎是最接近提供解决方案的方法。首先,我使用 go mod vendor 命令为想要使用这些 Go 模块的 Go 应用程序生成供应商目录,然后我使用 git submodule 命令将 Go 模块添加到 vendor/ 目录中。这在更新或添加模块之前都能完美运行。然而,当需要下载或更新其他 Go 模块时,go mod tidy 命令将无法访问自定义 Go 模块的 "git URL",即使设置了 -e 标志也是如此。

编辑 .gitconfig

编辑 .gitconfig 文件,将不带端口的 URL 替换为带端口的 URL 是一个可行的解决方案,但这是一个非常不好的 hack。首先,这些编辑必须针对任何新模块进行,并且对每个开发人员都要进行。其次,在处理这些仓库时,这可能会破坏其他 git 进程。

英文:

Background

At my company, we use Bit Bucket to host our git repos. All traffic to the server flows through a custom, non-standard port. Cloning from our repos looks something like git clone ssh://git@stash.company.com:9999/repo/path/name.git.

The problem

I would like to create Go modules hosted on this server and managed by go mod, however, the fact that traffic has to flow through port 9999 makes this very difficult. This is because go mod operates on the standard ports and doesn't seem to provide a way to customise ports for different modules.

My question

Is it possible to use go mod to manage Go modules hosted on a private git server with a non-standard port?

Attempted solutions

Vendoring

This seems to be the closest to offering a solution. First I go mod vendor the Go application that wants to use these Go modules, then I git submodule the Go module in the vendor/ directory. This works perfectly up to the point that a module needs to be updated or added. go mod tidy will keep failing to download or update the other Go modules because it cannot access the "git URL" of the custom Go module. Even when the -e flag is set.

Editing .gitconfig

Editing the .gitconfig to replace the URL without the port with the URL with the port is a solution that will work but is a very dirty hack. Firstly, these edits will have to be done for any new modules, and for every individual developer. Secondly, this might brake other git processes when working on these repositories.

答案1

得分: 4

go工具在内部使用git,所以您需要在环境中配置git以使用替代的URL。类似于:

git config --global url."ssh://git@stash.company.com:9999/".insteadOf "https://stash.company.com"

不过我记得bitbucket/stash有时会提供额外的后缀,原因我不记得了,所以您可能需要这样做:

git config --global url."ssh://git@stash.company.com:9999/".insteadOf "https://stash.company.com/scm/"

附加编辑

用户bcmills在下面提到,您还可以通过HTTPS提供go-import元数据,并使用您喜欢的任何自定义URL,前提是您控制域名解析。这可以通过各种复杂程度的方式实现,从简单的nginx规则静态内容生成器专用的自定义服务甚至使用Athens运行自己的模块代理。

然而,这仍然无法完全解决构建环境配置的问题,因为您希望用户根据您的配置设置GOPRIVATEGOPROXY或两者兼有。

另外,如果您选择的域名可能在全球范围内解析,请考虑注册它,以防止被潜在的恶意第三方注册。

英文:

The go tool uses git under the hood, so you'd want to configure git in your environment to use an alternate url. Something like

git config --global url."ssh://git@stash.company.com:9999/".insteadOf "https://stash.company.com"

Though I recall that bitbucket/stash sometimes provides an extra suffix for reasons I don't recall, so you might need to do something like this:

git config --global url."ssh://git@stash.company.com:9999/".insteadOf "https://stash.company.com/scm/"

ADDITIONAL EDIT

user bcmills mentioned below that you can also serve the go-import metadata over HTTPS, and use whatever vanity URL you like, provided you control the domain resolution. This can be done with varying degrees of sophistication, from a simple nginx rule to static content generators, dedicated vanity services or even running your own module proxy with Athens

This still doesn't completely solve the problem of build environment configuration, however, since you'll want the user to set GOPRIVATE or GOPROXY or both, depending on your configuration.

Also, if your chosen domain is potentially globally resolvable, you might want to consider registering it anyway to keep it from being registered by a potentially-malicious third party.

huangapple
  • 本文由 发表于 2021年6月22日 18:41:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/68082073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定