2 回答
TA贡献1946条经验 获得超4个赞
更简洁的方法是利用 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 %v\n", cmdErr)
}),
}
rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})
rootCmd.Execute()
}
----- 旧答案-----
如果我理解正确,有一些状态需要从cmd.Execute传递到PostRun。
您可以使用方法代替并设置需要在上下文中设置的任何键值。ExecuteContext(ctx context.Context)Execute()
ctx := context.WithValue(context.Background(), "status", "statusValue")
rootCmd.ExecuteContext(ctx)
可以使用在 PostRun 中检索相同的值cmd.Context()
PostRun: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
status := ctx.Value("status")
}
TA贡献1848条经验 获得超10个赞
我会使用高阶函数来包装你的(或)函数和你的函数来捕获错误或恐慌,然后将它们传递到你的:RunRunEPostRunPostRun
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 %v\n", cmdErr)
}),
}
var rootCmd = &cobra.Command{}
rootCmd.AddCommand(cmdFail)
rootCmd.Execute()
}
- 2 回答
- 0 关注
- 119 浏览
添加回答
举报