我使用 Cobra 和 Viper 编写了一个简单的 CLI 工具。我最近一直在重构它以避免包全局变量,很大程度上是因为事实证明很难使用例如建议的布局进行测试。
cobra init
。
而不是...
var rootCmd = &cobra.Command{
...
}
func main() {
rootCmd.Execute()
}
我有更多类似的东西:
func NewCmdRoot() *cobra.Command {
cmd := &cobra.Command{
...
}
return cmd
}
func main() {
rootCmd := NewCmdRoot()
rootCmd.Execute()
}
这实际上效果很好,并且使测试变得更加容易 从一组干净的 CLI 选项开始。我遇到了一些 将 Viper 集成到新方案中遇到困难。如果我只关心 关于 root 命令,我可以在
PersistentPreRun
中进行设置
命令,像这样:
func initConfig(cmd *cobra.Command) {
config := viper.New()
rootCmd := cmd.Root()
config.BindPFlag("my-nifty-option", rootCmd.Flag("my-nifty-option")); err != nil {
// ...stuff happens here...
config.ReadInConfig()
// What happens next?
}
func NewCmdRoot() *cobra.Command {
cmd := &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initConfig(cmd)
},
}
这种工作方式:只要我只对配置选项感兴趣 对应于 Cobra 命令行选项,事情的工作原理如下 预期的。但是如果我想访问
config
变量本身怎么办?
我不确定如何在外部公开
config
变量
initConfig
方法,无需将其转换为包全局。我想要
实例化多个命令树的可能性,每个命令树都有
它是自己独立的 Viper 配置对象,但我不清楚该放在哪里
它。
cobra 团队最近做到了这一点,请参阅 https://github.com/spf13/cobra/pull/1551
func TestFoo(t *testing.T){
root := &Command{
Use: "root",
PreRun: func(cmd *Command, args []string) {
ctx := context.WithValue(cmd.Context(), key{}, val)
cmd.SetContext(ctx)
},
Run: func(cmd *Command, args []string) {
fmt.Println(cmd.Context().Value(key{}))
},
}
ctx := context.WithValue(cmd.Context(), "key", "default")
root.ExecuteContext(ctx)
}