What's the Go (mod) equivalent of npm-outdated?

Luí*_*res 0 go go-modules

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

What's the closest for Go mod?

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

icz*_*cza 8

列出直接和间接依赖

Go 1.11模块:如何升级和降级依赖性 Wiki中对此进行了详细说明:

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

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

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

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

还有一个第三方应用程序: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
}
Run Code Online (Sandbox Code Playgroud)

注意:此Module结构是在go命令的内部包中定义的:https : //godoc.org/cmd/go/internal/modinfo

因此,例如,像以前一样列出直接和间接依赖关系,但现在也可以IAMINDIRECT在间接依赖关系后面附加一个单词,可以这样完成:

go list -u -m -f '{{.}}{{if .Indirect}} IAMINDIRECT{{end}}' all
Run Code Online (Sandbox Code Playgroud)

否定逻辑,以列出直接和间接依赖关系,但是这次仅用以下方式“标记”直接依赖关系IAMDIRECT

go list -u -m -f '{{.}}{{if not .Indirect}} IAMDIRECT{{end}}' all
Run Code Online (Sandbox Code Playgroud)

而且我们快到了。现在,我们只需要过滤掉不包含IAMDIRECT单词的行:

go list -u -m -f '{{.}}{{if not .Indirect}} IAMDIRECT{{end}}' all | grep IAMDIRECT
Run Code Online (Sandbox Code Playgroud)

另类

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

因此,我们可以达到以下目的:

go list -u -m -f '{{if not .Indirect}}{{.}}{{end}}' all
Run Code Online (Sandbox Code Playgroud)

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

仅列出具有更新的依赖项

同样,我们如何过滤掉间接依赖关系,这也是“小菜一碟”,因为该Module结构包含Update具有更新的包/模块的字段:

go list -u -m -f '{{if .Update}}{{.}}{{end}}' all
Run Code Online (Sandbox Code Playgroud)

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