5

我试图在我自己的项目中利用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 repo进行了更深入的研究,似乎该项目不使用 go 模块;而是使用较旧的vendordir 方法。

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

4

1 回答 1

2

默认情况下,该go命令将仅添加模块的最新标记版本,以解决缺少的依赖项。

根据https://github.com/docker/swarmkit/tags的最新(也是唯一)标记版本github.com/docker/swarmkitwas v1.12.0,早在 2016 年(!),当时api/defaultsapi/genericresource包还不存在。

您可以go通过显式传递版本的提交哈希或分支名称来向命令询问依赖项的更新版本。例如,截至我撰写此评论时的最新提交是2dcf70aafdc9ea55af3aaaeca440638cde0ecda6,因此您可以将该提交传递给go get,并且从那时起它似乎可以正确构建:

$ 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/...

$
于 2021-06-28T18:52:49.000 回答