如何使用gocenter远程Artifactory回购解决本地依赖问题?

时间:2019-04-17 19:58:52

标签: go artifactory

我是新手,尝试使用gocenter远程存储库(Artifactory 6.8)进行依赖关系解析。尽管设置了GOPROXY env var,但我的gocenter-cache存储库仍然为空。

这是我的代码。

package main

import (
        "fmt"
        "github.com/naoina/go-stringutil"
)

func main() {
        var str string = "hello_world_go"
        fmt.Println(stringutil.ToUpperCamelCase(str)) //prints HelloWorldGo
}

我要解决的依赖性在这里:https://search.gocenter.io/github.com~2Fnaoina~2Fgo-stringutil/versions

这是我的GOPROXY env var:

$ echo $GOPROXY
https://<<my Artifactory URL>>/api/go/gocenter

这是gocenter存储库定义的截短版本。我使用jfrog docs进行了设置:

{
  "key" : "gocenter",
  "packageType" : "go",
  "url" : "https://gocenter.io/",
  "rclass" : "remote"
}

当我运行“ go get”时,依赖关系会解决...

$ go get -v github.com/naoina/go-stringutil
github.com/naoina/go-stringutil (download)

但是gocenter-cache为空,告诉我它没有被使用。

{
  "repo" : "gocenter-cache",
  "path" : "/",
  "created" : "2019-04-17T16:35:37.586Z",
  "lastModified" : "2019-04-17T16:35:37.586Z",
  "lastUpdated" : "2019-04-17T16:35:37.586Z",
  "children" : [ ],
  "uri" : "https://<<REDACTED>>/api/storage/gocenter-cache"
}

也许我的“获得”应该有不同的目标?我只是在使用gocenter中存在的内容:https://search.gocenter.io/github.com~2Fnaoina~2Fgo-stringutil/versions

任何指出我做错事情的帮助将不胜感激。我还没有进入模块之类的领域,因为我是人为因素的管理员,而不是go开发人员。我只是在尝试对该功能进行原型设计,以便我们可以为用户提供建议。

-更新:我找到了https://golang.org/cmd/go/#hdr-Module_proxy_protocol,然后尝试了此操作:

$ echo $GOPROXY; go get $GOPROXY/github.com/naoina/go-stringutil
https://<<REDACTED>>/api/go/gocenter
package https:/<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil: https:/<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil: invalid import path: malformed import path "https:/<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil": invalid char ':'

那么我的协议和URL之间的冒号是无效字符?为何还要删除我的一个正斜杠?

-UPDATE2: 我可以进行“ go mod init”工作,

$ go mod init
go: creating new go.mod: module example/hello
$ ls
go.mod  hello.go
$ go build
build example/hello: cannot load github.com/naoina/go-stringutil: cannot find module providing package github.com/naoina/go-stringutil
$ cat go.mod
module example/hello

go 1.12
$ echo $GOPROXY
https://<<REDACTED>>/api/go/gocenter

-更新3:

$ cat go.mod
module example/hello

go 1.12

require github.com/naoina/go-stringutil v0.1.0

$ go build
hello.go:6:2: cannot find package "github.com/naoina/go-stringutil" in any of:
        C:\Go\src\github.com\naoina\go-stringutil (from $GOROOT)
        C:\Users\samuelm\go\src\github.com\naoina\go-stringutil (from $GOPATH)

$ echo $GOPROXY
https://<<REDACTED>>/api/go/gocenter

-更新4:看来我还在使用模块吗?

$ go list -m all
go list -m: not using modules

-更新5,回复:retgits 您的步骤有所帮助,但我仍未完全解决。

$ find .
.
./bin
./src
./src/example.com
./src/example.com/hello
./src/example.com/hello/hello.go

$ cd src/

$ go mod init example.com/hello
go: creating new go.mod: module example.com/hello

$ cat go.mod
module example.com/hello

go 1.12

$ go get -v github.com/naoina/go-stringutil
Fetching https://<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil/@v/list
Fetching https://<<REDACTED>>/api/go/gocenter/github.com/naoina/@v/list
Fetching https://<<REDACTED>>/api/go/gocenter/github.com/@v/list
go get github.com/naoina/go-stringutil: unexpected status (https://<<REDACTED>>/api/go/gocenter/github.com/naoina/go-stringutil/@v/list): 404 Not Found

$ go build example.com/hello
can't load package: package example.com/hello: unknown import path "example.com/hello": cannot find module providing package example.com/hello

$ cd example.com/hello/

$ go build
build example.com/hello/example.com/hello: cannot load github.com/naoina/go-stringutil: cannot find module providing package github.com/naoina/go-stringutil

我没有在GOPROXY中提供凭据,因为我们的最终用户没有帐户,我们位于防火墙保护的环境中,并允许完全的匿名读取访问。如果我们必须提供用户身份,则我们不支持Go。

-最终更新: 删除我的本地代理可以解决404问题,retgits解决方案可以工作。

1 个答案:

答案 0 :(得分:1)

您提到您不是Go开发人员,所以让我引导您完成所有步骤。我意识到这可能对这里的一些开发人员来说有些过大,但这可能会对您有所帮助。

根据放置源代码的位置,您需要设置环境变量GO111MODULE。从1.11版开始,建议不要再将源代码放入$ GOPATH中。如果将代码放在此处并想使用Go模块,则必须将GO111MODULE设置为true(我个人将所有Go代码都保留在$ GOPATH之外)。在Windows上,您必须首先创建环境变量并进行相应的设置(然后重新启动终端)。

要创建Go模块,您必须运行命令go mod init <name of your module>。就我而言,我运行了命令go mod init github.com/retgits/bla,该命令创建了一个go.mod文件,

module github.com/retgits/bla

go 1.12

添加模块现在就像运行go get一样简单。如果您想使用GoCenter或Artifactory来帮助您解决模块。

为帮助解析模块,您可能要查看两个选项:

  • 使用名为goc的实用程序
  • 设置远程和虚拟代理

使用goc

goc实用程序会自动将GOPROXY设置为GoCenter,绕过Artifactory等其他代理。 Go客户端的当前行为是查看一个代理,如果不是从那里解析所有模块,则构建失败。 goc将首先查看GoCenter,如果该模块不在GoCenter中,它将从其原始位置(如GitHub)获取该模块

使用人工工厂

如果要使用Artifactory解析和缓存Go模块,则必须使用GoCenter的设置(在您提到的文档中)创建一个远程存储库。您必须将该远程存储库包含到虚拟存储库中才能工作。

将GoCenter设置为GOPROXY

第三个选择是仅使用GoCenter(由JFrog创建的公共注册表),但这可能会打败您的原始问题:

export GOPROXY="https://gocenter.io"
go get -v github.com/naoina/go-stringutil

无论您选择哪个选项,都会将go.mod更新为

module github.com/retgits/bla

go 1.12

require github.com/naoina/go-stringutil v0.1.0 // indirect

indirect是因为我尚未使用使用此导入的代码创建.go文件。

如果我现在使用创建一个main.go样本

package main

import (
    "fmt"

    "github.com/naoina/go-stringutil"
)

func main() {
    str := stringutil.ToSnakeCase("HelloWorld")
    fmt.Println(str)
}
// Running go run main.go would result in
// hello_world

它将从go.mod文件中删除indirect,因为我现在有依赖于模块的代码。