什么是npm过时的Go(mod)等效项? [英] What's the Go (mod) equivalent of npm-outdated?

查看:66
本文介绍了什么是npm过时的Go(mod)等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to keep my go.mod dependencies up to date. With Node.js, I run the npm outdated (and later npm update).

Go mod最接近什么?

What's the closest for Go mod?

理想情况下,我会看到有关项目的过时依赖关系的报告(并非全部都是递归的).谢谢

Ideally, I'd see a report of outdated dependencies of my project (not all recursively). Thanks

推荐答案

列出直接和间接依赖项

这在转到1.11模块中进行了详细说明:如何升级和降级依赖项 Wiki:

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

要针对当前模块的所有直接和间接依赖关系升级到最新版本,请执行以下操作:

To upgrade to the latest version for all direct and indirect dependencies of the current module:

  • 运行go get -u以使用最新的次要版本或修补程序版本
  • 运行go get -u=patch以使用最新的修补程序版本
  • run go get -u to use the latest minor or patch releases
  • run go get -u=patch to use the latest patch releases

您可以在此处阅读更多详细信息:命令转到:列出软件包或模块.

You can read more details here: Command go: List packages or modules.

还有一个第三方应用程序: https://github.com/psampaz/go-mod -过时:

There's also a 3rd party app: https://github.com/psampaz/go-mod-outdated:

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

An easy way to find outdated dependencies of your Go projects. go-mod-outdated provides a table view of the go list -u -m -json all command which lists all dependencies of a Go project and their available minor and patch updates. It also provides a way to filter indirect dependencies and dependencies without updates.

仅列出直接依赖项

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

Listing only direct dependencies

If you are not interested in indirect dependencies, we can filter them out. There is no flag to filter out indirect dependencies, but we can do that with custom output format.

-f标志使用软件包模板的语法为列表指定备用格式.

The -f flag specifies an alternate format for the list, using the syntax of package template.

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

So you may specify a format being a template document, conforming with text/template.

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

When listing modules, the -f flag still specifies a format template applied to a Go struct, but now a Module struct:

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结构在go命令的内部包中定义: https://godoc.org/cmd/go/internal/modinfo

Note: this Module struct is defined in an internal package of the command go: https://godoc.org/cmd/go/internal/modinfo

例如,像以前一样列出直接和间接依赖关系,但现在在间接依赖关系之后还添加一个IAMINDIRECT单词,可以用以下方式完成:

So for example to list direct and indirect dependencies as before, but now also append an IAMINDIRECT word after indirect dependencies, it could be done with:

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

否定逻辑,以列出直接和间接依赖关系,但是这次仅用IAMDIRECT标记"直接依赖关系:

Negating the logic, to list direct and indirect dependencies, but this time "mark" only the direct dependencies with IAMDIRECT:

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

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

And we're almost there. We now just have to filter out rows that do not contain the IAMDIRECT word:

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

替代

以上解决方案基于grep命令.但实际上我们不需要.如果指定的模板导致空文档,则从输出中跳过该行.

Alternative

The above solution builds on the grep command. But in fact we don't need that. If the specified template results in an empty document, the line is skipped from the output.

所以我们可以达到以下目的:

So we can achieve the same like this:

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

基本上,如果不是间接的,我们仅调用Module.String()(我们仅包括依赖项).作为一项额外收益,该解决方案也可以在Windows上使用.

Basically we only call Module.String() (we only include a dependency) if it is not indirect. As an extra gain, this solution also works on Windows too.

类似地,我们过滤掉间接依赖关系,这也是小菜一碟",因为Module结构包含用于更新的包/模块的Update字段:

Similarly how we filtered out indirect dependencies, this is also a "piece of cake" since the Module structure contains an Update field for packages / modules that have updates:

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


另请参阅相关问题:如何列出已安装的go软件包

这篇关于什么是npm过时的Go(mod)等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆