【问题标题】:What's the Go (mod) equivalent of npm-outdated?npm-outdated 的 Go (mod) 等价物是什么?
【发布时间】:2022-05-08 05:38:37
【问题描述】:

我想让我的 go.mod 依赖项保持最新。使用 Node.js,我运行 npm outdated(以及后来的 npm update)。

Go mod 最接近的是什么?

理想情况下,我会看到我的项目的过时依赖项的报告(不是全部递归)。谢谢

【问题讨论】:

    标签: go go-modules


    【解决方案1】:

    列出直接和间接依赖项

    这在Go 1.11 Modules: How to Upgrade and Downgrade Dependencieswiki中有详细说明:

    要查看所有直接和间接依赖项的可用次要和补丁升级,请运行 go list -u -m all

    为当前模块的所有直接和间接依赖项升级到最新版本:

    • 运行 go get -u 以使用最新的次要版本或补丁版本
    • 运行 go get -u=patch 以使用最新的补丁版本

    您可以在此处阅读更多详细信息:Command go: List packages or modules

    还有一个 3rd 方应用:https://github.com/psampaz/go-mod-outdated

    一种查找 Go 项目过时依赖项的简单方法。 go-mod-outdated 提供了 go list -u -m -json all 命令的表格视图,其中列出了 Go 项目的所有依赖项及其可用的次要和补丁更新。它还提供了一种过滤间接依赖和不更新依赖的方法。

    仅列出直接依赖项

    如果您对间接依赖不感兴趣,我们可以将它们过滤掉。没有标志可以过滤掉间接依赖,但我们可以使用自定义输出格式来做到这一点。

    -f 标志使用包模板的语法为列表指定另一种格式。

    因此您可以指定格式为模板文档,符合text/template

    在列出模块时,-f 标志仍然指定应用于 Go 结构的格式模板,但现在是 Module 结构:

    type Module struct {
        Path     string       // module path
        Version  string       // module version
        Versions []string     // available module versions (with -versions)
        Replace  *Module      // replaced by this module
        Time     *time.Time   // time version was created
        Update   *Module      // available update, if any (with -u)
        Main     bool         // is this the main module?
        Indirect bool         // is this module only an indirect dependency of main module?
        Dir      string       // directory holding files for this module, if any
        GoMod    string       // path to go.mod file for this module, if any
        Error    *ModuleError // error loading module
    }
    
    type ModuleError struct {
        Err string // the error itself
    }
    

    注意:这个Module struct 定义在命令go的内部包中:https://godoc.org/cmd/go/internal/modinfo

    例如,像以前一样列出直接和间接依赖项,但现在在间接依赖项之后附加一个IAMINDIRECT 字,可以这样做:

    go list -u -m -f '{{.}}{{if .Indirect}} IAMINDIRECT{{end}}' all
    

    否定逻辑,列出直接和间接依赖关系,但这次只用IAMDIRECT“标记”直接依赖关系:

    go list -u -m -f '{{.}}{{if not .Indirect}} IAMDIRECT{{end}}' all
    

    我们快到了。我们现在只需要过滤掉不包含IAMDIRECT 字的行:

    go list -u -m -f '{{.}}{{if not .Indirect}} IAMDIRECT{{end}}' all | grep IAMDIRECT
    

    另类

    上述解决方案基于grep 命令。但事实上我们不需要那个。如果指定的模板生成一个空文档,则从输出中跳过该行。

    所以我们可以这样实现:

    go list -u -m -f '{{if not .Indirect}}{{.}}{{end}}' all
    

    基本上,如果不是间接的,我们只会调用Module.String()(我们只包含一个依赖项)。作为额外收获,此解决方案也适用于 Windows。

    仅列出有更新的依赖项

    与我们过滤间接依赖的方式类似,这也是“小菜一碟”,因为 Module 结构包含一个 Update 字段,用于包含有更新的包/模块:

    go list -u -m -f '{{if .Update}}{{.}}{{end}}' all
    

    另见相关问题:How to list installed go packages

    【讨论】:

    • 问题在于它递归地列出了所有依赖项。我只需要知道第一级;我在go.mod 中提到的那些
    • 你的模块依赖的依赖不是你的模块的依赖吗?它是间接的,但仍然如此。
    • @LuísSoares 查看已编辑的答案以过滤掉间接依赖关系。
    • 太棒了!如果我只想过滤过时的内容怎么办?如npm outdated
    • @LuísSoares 这也很容易实现。查看更新的答案。
    【解决方案2】:

    内置go list 的问题在于它不会向您显示依赖项的新主要版本。见:https://github.com/golang/go/issues/40323

    这是一个显示过时的直接依赖项的工具,包括新的主要版本。

    https://github.com/icholy/gomajor

    在 Hashicorp 的 Vault 上运行它的示例:

    $ gomajor list
    cloud.google.com/go: v0.65.0 [latest v0.100.2]
    cloud.google.com/go/spanner: v1.5.1 [latest v1.29.0]
    cloud.google.com/go/storage: v1.10.0 [latest v1.20.0]
    github.com/Azure/go-autorest/autorest: v0.11.21 [latest v0.11.24]
    github.com/Azure/go-autorest/autorest/adal: v0.9.14 [latest v0.9.18]
    github.com/SAP/go-hdb: v0.14.1 [latest v0.105.5]
    github.com/aerospike/aerospike-client-go/v5: v5.6.0 [latest v5.7.0]
    github.com/aliyun/alibaba-cloud-sdk-go: v0.0.0-20190620160927-9418d7b0cd0f [latest v1.61.1479]
    github.com/aliyun/aliyun-oss-go-sdk: v0.0.0-20190307165228-86c17b95fcd5 [latest v2.2.0+incompatible]
    github.com/aws/aws-sdk-go: v1.37.19 [latest v1.42.49]
    github.com/cenkalti/backoff/v3: v3.0.0 [latest v4.1.2]
    github.com/cockroachdb/cockroach-go: v0.0.0-20181001143604-e0a95dfd547c [latest v2.2.8]
    github.com/docker/docker: v20.10.10+incompatible [latest v20.10.12+incompatible]
    github.com/go-errors/errors: v1.4.1 [latest v1.4.2]
    github.com/go-sql-driver/mysql: v1.5.0 [latest v1.6.0]
    github.com/google/go-cmp: v0.5.6 [latest v0.5.7]
    github.com/google/go-github: v17.0.0+incompatible [latest v42.0.0]
    github.com/google/go-metrics-stackdriver: v0.2.0 [latest v0.4.0]
    github.com/hashicorp/consul-template: v0.27.2-0.20211014231529-4ff55381f1c4 [latest v0.27.2]
    github.com/hashicorp/consul/api: v1.11.0 [latest v1.12.0]
    github.com/hashicorp/go-secure-stdlib/awsutil: v0.1.5 [latest v0.1.6]
    github.com/hashicorp/go-secure-stdlib/mlock: v0.1.1 [latest v0.1.2]
    github.com/hashicorp/go-secure-stdlib/strutil: v0.1.1 [latest v0.1.2]
    github.com/hashicorp/hcl: v1.0.1-vault-3 [latest v2.11.1]
    github.com/hashicorp/raft: v1.3.3 [latest v1.3.4]
    github.com/hashicorp/raft-autopilot: v0.1.3 [latest v0.1.5]
    github.com/hashicorp/raft-boltdb/v2: v2.0.0-20210421194847-a7e34179d62c [latest v2.2.1]
    github.com/hashicorp/vault-plugin-auth-kubernetes: v0.7.1-0.20220107030939-d289258274b7 [latest v0.11.5]
    github.com/hashicorp/vault-plugin-mock: v0.16.1 [latest v0.19.13]
    github.com/hashicorp/vault-plugin-secrets-kv: v0.5.7-0.20220112155832-c2eb38b5f5b6 [latest v0.10.1]
    github.com/hashicorp/vault/api/auth/approle: v0.1.0 [latest v0.1.1]
    github.com/jefferai/jsonx: v1.0.0 [latest v1.0.1]
    github.com/lib/pq: v1.10.3 [latest v1.10.4]
    github.com/michaelklishin/rabbit-hole/v2: v2.11.0 [latest v2.12.0]
    github.com/mitchellh/copystructure: v1.0.0 [latest v1.2.0]
    github.com/mitchellh/go-testing-interface: v1.14.0 [latest v1.14.1]
    github.com/mitchellh/go-wordwrap: v1.0.0 [latest v1.0.1]
    github.com/mitchellh/mapstructure: v1.4.2 [latest v1.4.3]
    github.com/mongodb/go-client-mongodb-atlas: v0.1.2 [latest v0.14.0]
    github.com/natefinch/atomic: v0.0.0-20150920032501-a62ce929ffcc [latest v1.0.1]
    github.com/ncw/swift: v1.0.47 [latest v2.0.1]
    github.com/oklog/run: v1.0.0 [latest v1.1.0]
    github.com/okta/okta-sdk-golang/v2: v2.9.1 [latest v2.10.1]
    github.com/oracle/oci-go-sdk: v13.1.0+incompatible [latest v57.0.0]
    github.com/ory/dockertest: v3.3.5+incompatible [latest v3.8.1]
    github.com/ory/dockertest/v3: v3.8.0 [latest v3.8.1]
    github.com/pquerna/otp: v1.2.1-0.20191009055518-468c2dd2b58d [latest v1.3.0]
    github.com/prometheus/client_golang: v1.11.0 [latest v1.12.1]
    github.com/prometheus/common: v0.26.0 [latest v0.32.1]
    github.com/ryanuber/columnize: v2.1.0+incompatible [latest v2.1.2+incompatible]
    github.com/sasha-s/go-deadlock: v0.2.0 [latest v0.3.1]
    github.com/sethvargo/go-limiter: v0.7.1 [latest v0.7.2]
    github.com/shirou/gopsutil: v3.21.5+incompatible [latest v3.22.1]
    go.etcd.io/etcd/client/pkg/v3: v3.5.0 [latest v3.5.2]
    go.etcd.io/etcd/client/v2: v2.305.0 [latest v3.5.2]
    go.etcd.io/etcd/client/v3: v3.5.0 [latest v3.5.2]
    go.mongodb.org/mongo-driver: v1.7.3 [latest v1.8.3]
    go.opentelemetry.io/otel: v0.20.0 [latest v1.3.0]
    go.opentelemetry.io/otel/sdk: v0.20.0 [latest v1.3.0]
    go.opentelemetry.io/otel/trace: v0.20.0 [latest v1.3.0]
    go.uber.org/goleak: v1.1.11-0.20210813005559-691160354723 [latest v1.1.12]
    golang.org/x/tools: v0.1.5 [latest v0.1.9]
    google.golang.org/api: v0.30.0 [latest v0.68.0]
    google.golang.org/grpc: v1.41.0 [latest v1.44.0]
    google.golang.org/grpc/cmd/protoc-gen-go-grpc: v1.1.0 [latest v1.2.0]
    gopkg.in/ory-am/dockertest.v3: v3.3.4 [latest v3.8.1]
    mvdan.cc/gofumpt: v0.1.1 [latest v0.2.1]
    

    【讨论】:

      【解决方案3】:

      使用iczaanswer中的所有提及go list -u -m,您可以将其与psampaz/go-mod-outdated结合使用,来自psampaz

      go-mod-outdated 提供了go list -u -m -json all 命令的表格视图,其中列出了 Go 项目的所有依赖项及其可用的次要和补丁更新。

      它还提供了一种过滤间接依赖和不更新依赖的方法。

      简而言之就是这样:

      {
          "Path": "github.com/BurntSushi/locker",
          "Version": "v0.0.0-20171006230638-a6e239ea1c69",
          "Time": "2017-10-06T23:06:38Z",
          "GoMod": "/home/mojo/go/pkg/mod/cache/download/github.com/!burnt!sushi/locker/@v/v0.0.0-20171006230638-a6e239ea1c69.mod"
      }
      {
          "Path": "github.com/BurntSushi/toml",
          "Version": "v0.0.0-20170626110600-a368813c5e64",
          "Time": "2017-06-26T11:06:00Z",
          "Update": {
              "Path": "github.com/BurntSushi/toml",
              "Version": "v0.3.1",
              "Time": "2018-08-15T10:47:33Z"
          },
          "GoMod": "/home/mojo/go/pkg/mod/cache/download/github.com/!burnt!sushi/toml/@v/v0.0.0-20170626110600-a368813c5e64.mod"
      }
      

      进入这个:

      +--------------------------------+---------------+-------------+--------+------------------+
      |             MODULE             | VERSION       | NEW VERSION | DIRECT | VALID TIMESTAMPS |
      +--------------------------------+---------------+-------------+--------+------------------+
      | github.com/BurntSushi/locker   | v0.0.0-20...  |             | true   | true             |
      | github.com/BurntSushi/toml     | v0.0.0-20..   | v0.3.1      | true   | true             |
      +--------------------------------+---------------+-------------+--------+------------------+
      

      【讨论】:

        猜你喜欢
        • 2019-09-14
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        • 2022-01-20
        • 2015-07-06
        • 1970-01-01
        • 2017-03-01
        • 2020-11-21
        相关资源
        最近更新 更多