如何在cobra-cli中将命令状态传递给Postrun



Cobra CLI支持在执行命令后调用PostRun。

https://github.com/spf13/cobra#prerun-和运行后挂钩

如何将命令状态传递给PostRun调用?我要求将命令执行后的状态发布到服务器

更干净的方法是利用cobra命令中提供的注释

package main
import (
"fmt"
"github.com/spf13/cobra"
)

func main() {
var rootCmd = &cobra.Command{
Use:   "root [sub]",
Short: "My root command",
Run: func(cmd *cobra.Command, args []string) {
// Do your processing here
// Set the command annotations
cmd.Annotations = make(map[string]string)
cmd.Annotations["status"] = "status_goes_here"
cmd.Annotations["error"] = "error_goes_here"
},
PostRun: func(cmd *cobra.Command, args []string) {
// Retrieve the annotations
fmt.Println(cmd.Annotations["status"])
fmt.Println(cmd.Annotations["error"])
},
}
rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})
rootCmd.Execute()
}

真的很喜欢@Bracken在这里采取的方法,尽管有一些调整会使其工作

package main
import (
"fmt"
"errors"
"github.com/spf13/cobra"
)
type wrapper struct {
err error
}
// RunE fails to proceed further in case of error resulting in not executing PostRun actions
func (w *wrapper) Run(f func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
err := f(cmd, args)
w.err = err
}
}
func (w *wrapper) PostRun(f func(cmd *cobra.Command, args []string, cmdErr error)) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
f(cmd, args, w.err)
}
}
func main() {
cmdWrap := wrapper{}
var rootCmd = &cobra.Command{
Use:   "root [sub]",
Short: "My root command",
Run: cmdWrap.Run(func(cmd *cobra.Command, args []string) error {
return errors.New("i'm not in the book, you know")
}),
PostRun: cmdWrap.PostRun(func(cmd *cobra.Command, args []string, cmdErr error) {
fmt.Printf("error was %vn", cmdErr)
}),
}
rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})
rootCmd.Execute()
}

-----老答案----

如果我理解正确的话,cmd需要传递一些状态。执行到PostRun。

您可以使用ExecuteContext(ctx context.Context)方法而不是Execute(),并设置上下文中需要设置的任何键值。

ctx := context.WithValue(context.Background(), "status", "statusValue")
rootCmd.ExecuteContext(ctx)

使用cmd.Context()可以在PostRun中检索相同的值

PostRun: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
status := ctx.Value("status")
}

我会使用高阶函数来包装Run(或RunE(函数和PostRun函数,以捕获错误或恐慌,然后将它们传递到PostRun:

package main
import (
"fmt"
"github.com/spf13/cobra"
)
type wrapper struct {
err error
}
func (w *wrapper) RunE(f func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
w.err = err
}
}()
err = f(cmd, args)
w.err = err
return
}
}
func (w *wrapper) PostRun(f func(cmd *cobra.Command, args []string, cmdErr error)) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
f(cmd, args, w.err)
}
}
func main() {
cmdWrap := wrapper{}
var cmdFail = &cobra.Command{
Use:   "fail",
Short: "Doesn't work",
RunE: cmdWrap.RunE(func(cmd *cobra.Command, args []string) error {
panic("i'm not in the book, you know")
}),
PostRun: cmdWrap.PostRun(func(cmd *cobra.Command, args []string, cmdErr error) {
fmt.Printf("error was %vn", cmdErr)
}),
}
var rootCmd = &cobra.Command{}
rootCmd.AddCommand(cmdFail)
rootCmd.Execute()
}

相关内容

  • 没有找到相关文章

最新更新