docker cli swarm package import issues(Docker CLI Swarm 包导入问题)

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

docker cli swarm package import issues

问题

我正在尝试在自己的项目中利用docker/cli客户端库来创建/管理堆栈。

然而,我在构建项目时遇到了问题。


以下是代码:

package main

import (
	"fmt"
	"log"

	"github.com/docker/cli/cli/command"
	"github.com/docker/cli/cli/command/stack"
	"github.com/docker/cli/cli/flags"
)

func main() {

	cli, err := command.NewDockerCli(command.WithStandardStreams())
	if err != nil {
		log.Fatal(err)
	}

	cli.Initialize(flags.NewClientOptions())

	cmd := stack.NewStackCommand(cli)
	cmd.SetArgs([]string{"deploy", "--compose-file", "docker-compose.yml", "test"})

	err = cmd.Execute()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("success!")
}

我已经设置了一个最小的go.mod文件:

module github.com/dev/test

go 1.16

当我运行go get时,我得到以下错误:

github.com/dev/test imports
        github.com/docker/cli/cli/command/stack imports
        github.com/docker/cli/cli/command/service imports
        github.com/docker/swarmkit/api/defaults: cannot find module providing package github.com/docker/swarmkit/api/defaults
github.com/dev/test imports
        github.com/docker/cli/cli/command/stack imports
        github.com/docker/cli/cli/command/service imports
        github.com/docker/swarmkit/api/genericresource: cannot find module providing package github.com/docker/swarmkit/api/genericresource
github.com/dev/test imports
        github.com/docker/cli/cli/command/stack imports
        github.com/docker/cli/cli/command/service imports
        github.com/docker/swarmkit/api imports
        google.golang.org/grpc/transport: cannot find module providing package google.golang.org/grpc/transport

我仔细研究了docker/cli存储库,发现该项目没有使用go模块,而是使用了较旧的vendor目录方法。

我想知道如何使项目编译。是否有办法让go get自动引用导入的docker/cli项目中vendor目录中的包?

英文:

Im trying to utilise docker/cli client library in my own project to create/manage stacks.

I am however facing issues building the project.


Code below:

package main

import (
	"fmt"
	"log"

	"github.com/docker/cli/cli/command"
	"github.com/docker/cli/cli/command/stack"
	"github.com/docker/cli/cli/flags"
)

func main() {

	cli, err := command.NewDockerCli(command.WithStandardStreams())
	if err != nil {
		log.Fatal(err)
	}

	cli.Initialize(flags.NewClientOptions())

	cmd := stack.NewStackCommand(cli)
	cmd.SetArgs([]string{"deploy", "--compose-file", "docker-compose.yml", "test"})

	err = cmd.Execute()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("success!")
}

I have setup a minimal go.mod:

module github.com/dev/test

go 1.16

When i run go get; i get the following errors:

github.com/dev/test imports
        github.com/docker/cli/cli/command/stack imports
        github.com/docker/cli/cli/command/service imports
        github.com/docker/swarmkit/api/defaults: cannot find module providing package github.com/docker/swarmkit/api/defaults
github.com/dev/test imports
        github.com/docker/cli/cli/command/stack imports
        github.com/docker/cli/cli/command/service imports
        github.com/docker/swarmkit/api/genericresource: cannot find module providing package github.com/docker/swarmkit/api/genericresource
github.com/dev/test imports
        github.com/docker/cli/cli/command/stack imports
        github.com/docker/cli/cli/command/service imports
        github.com/docker/swarmkit/api imports
        google.golang.org/grpc/transport: cannot find module providing package google.golang.org/grpc/transport

I have had a deeper look into the docker/cli repo and it appears that the project does not use go modules; instead its using the older vendor dir approach.

I was wondering how i can get the project to compile. Is there a way for go get to automatically reference the packages in the vendor directory of the imported docker/cli project?

答案1

得分: 2

默认情况下,go命令只会添加最新的已标记版本来解决缺失的依赖关系。

根据https://github.com/docker/swarmkit/tags,github.com/docker/swarmkit的最新(也是唯一)已标记版本是v1.12.0,时间是2016年,而在那个时候,api/defaultsapi/genericresource包还不存在。

你可以通过显式地传递提交哈希或分支名称来要求go命令获取一个比已标记版本更新的依赖版本。例如,我撰写此评论时的最新提交是2dcf70aafdc9ea55af3aaaeca440638cde0ecda6,所以你可以将该提交传递给go get命令,从那个点开始构建:

$ go get -d github.com/docker/swarmkit/api/...@2dcf70aafdc9ea55af3aaaeca440638cde0ecda6
go: 正在下载 github.com/docker/swarmkit v1.12.1-0.20210611195518-2dcf70aafdc9
go: 正在下载 github.com/coreos/etcd v0.5.0-alpha.5
go: 正在下载 github.com/gogo/protobuf v1.3.2
go: 正在下载 github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c
go: 正在下载 github.com/coreos/etcd v3.3.25+incompatible
go: 正在下载 google.golang.org/grpc v1.38.0
go: 正在下载 github.com/golang/protobuf v1.4.2
go: 正在下载 golang.org/x/net v0.0.0-20201021035429-f5854403a974
go: 正在下载 google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go: 正在下载 golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
go: 正在下载 google.golang.org/protobuf v1.25.0
go: 正在下载 golang.org/x/text v0.3.3
go: 正在下载 github.com/sirupsen/logrus v1.8.1
go get: 已添加 github.com/coreos/etcd v3.3.25+incompatible
go get: 已添加 github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c
go get: 已添加 github.com/docker/swarmkit v1.12.1-0.20210611195518-2dcf70aafdc9
go get: 已添加 github.com/gogo/protobuf v1.3.2
go get: 已添加 github.com/golang/protobuf v1.4.2
go get: 已添加 github.com/sirupsen/logrus v1.8.1
go get: 已添加 golang.org/x/net v0.0.0-20201021035429-f5854403a974
go get: 已添加 golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
go get: 已添加 golang.org/x/text v0.3.3
go get: 已添加 google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go get: 已添加 google.golang.org/grpc v1.38.0
go get: 已添加 google.golang.org/protobuf v1.25.0

$ go build github.com/docker/swarmkit/api/...

$
英文:

By default, the go command will only add the newest tagged release of a module in order to resolve a missing dependency.

According to https://github.com/docker/swarmkit/tags, the latest (and only) tagged release of github.com/docker/swarmkit was v1.12.0, back in 2016(!), and at that point in time the api/defaults and api/genericresource packages did not yet exist.

You can ask the go command for a newer-than-tagged version of a dependency by passing the commit hash or branch name explicitly for the version. For example, the latest commit as of when I am writing this comment is 2dcf70aafdc9ea55af3aaaeca440638cde0ecda6, so you could pass that commit to go get, and it seems to build properly from that point:

$ go get -d github.com/docker/swarmkit/api/...@2dcf70aafdc9ea55af3aaaeca440638cde0ecda6
go: downloading github.com/docker/swarmkit v1.12.1-0.20210611195518-2dcf70aafdc9
go: downloading github.com/coreos/etcd v0.5.0-alpha.5
go: downloading github.com/gogo/protobuf v1.3.2
go: downloading github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c
go: downloading github.com/coreos/etcd v3.3.25+incompatible
go: downloading google.golang.org/grpc v1.38.0
go: downloading github.com/golang/protobuf v1.4.2
go: downloading golang.org/x/net v0.0.0-20201021035429-f5854403a974
go: downloading google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go: downloading golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
go: downloading google.golang.org/protobuf v1.25.0
go: downloading golang.org/x/text v0.3.3
go: downloading github.com/sirupsen/logrus v1.8.1
go get: added github.com/coreos/etcd v3.3.25+incompatible
go get: added github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c
go get: added github.com/docker/swarmkit v1.12.1-0.20210611195518-2dcf70aafdc9
go get: added github.com/gogo/protobuf v1.3.2
go get: added github.com/golang/protobuf v1.4.2
go get: added github.com/sirupsen/logrus v1.8.1
go get: added golang.org/x/net v0.0.0-20201021035429-f5854403a974
go get: added golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
go get: added golang.org/x/text v0.3.3
go get: added google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go get: added google.golang.org/grpc v1.38.0
go get: added google.golang.org/protobuf v1.25.0

$ go build github.com/docker/swarmkit/api/...

$

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

发表评论

匿名网友

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

确定