Published on

cobra 终于移除了 viper 依赖

Authors
  • avatar
    Name
    ttyS3
    Twitter

cobra 可能是 Golang 里面最好用的 cli 命令行类库了. 没有之一.

其它也有一些功能类似的, 但是做到像 cobra 这样功能强大又简单易用的, 着实不多.

但是长期以来, cobra 依赖重量级的 viper module, 这两个东西都是同一个作者开发的, 因此其实也可以理解.

老灯最近突然发现, 随着社区力量的参与, cobra 的 viper 依赖居然在 1.4.0 版本被移除了!

老灯不是唯一一个不喜欢 viper 这种全家桶依赖类型的包的.

甚至有人 fork 出一份专门移除了 viper 依赖: https://github.com/muesli/coral

这个作者专门在 readme 说明了缘由:

I love Cobra and I love Viper. They are great projects, incredibly useful and outstandingly important for the Go community. But sometimes, just sometimes, don't you wish you could use Cobra without the entire dependency chain Viper drags in?

This is what Coral is: a Cobra fork without any of the Viper dependencies and features. This will hopefully be a soft fork with only the minimal changes required to decouple Viper from Cobra. The aim is to follow upstream Cobra development as closely as possible.

事实上由于 viper 依赖了问题, 老灯已经取消了 cobra star, 最近也是从 coral 的 readme 看到的消息, 作者甚至还贴心地给出了迁移命令:

Recent Cobra versions, v1.4.0 and onward, removed the dependency on Viper as well. You might switch back to Cobra using gofmt:

gofmt -w -r '"github.com/muesli/coral" -> "github.com/spf13/cobra"' .
gofmt -w -r '"github.com/muesli/coral/doc" -> "github.com/spf13/cobra/doc"' .
gofmt -w -r 'coral -> cobra' .

go get -u github.com/spf13/cobra
go mod tidy

其实 cobra 的 viper 依赖主要是其 cli 工具引入的, 因此 1.4.0 版本将这个 cli 工具放到单独的包里了.

go install github.com/spf13/cobra-cli@latest