Skip to content

Instantly share code, notes, and snippets.

@algrebe
Created January 13, 2016 17:42
Show Gist options
  • Save algrebe/cae234e77dae809bfe18 to your computer and use it in GitHub Desktop.
Save algrebe/cae234e77dae809bfe18 to your computer and use it in GitHub Desktop.
gist for github.com/spf13/cobra describing the use of persistent chaining
package main
import (
"fmt"
"github.com/spf13/cobra"
)
func getFunc(msg string) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
fmt.Printf("%s with args %v\n", msg, args)
}
}
func getSubCmd1() *cobra.Command {
cmd := &cobra.Command{
Use: "cmd1",
Short: "cmd1",
PersistentPreRun: getFunc("inside cmd1 PersistentPreRun"),
PreRun: getFunc("inside cmd1 PreRun"),
Run: getFunc("inside cmd1 Run"),
PostRun: getFunc("inside cmd1 PostRun"),
PersistentPostRun: getFunc("inside cmd1 PersistentPostRun"),
}
return cmd
}
func getSubCmd2() *cobra.Command {
cmd := &cobra.Command{
Use: "cmd2",
Short: "cmd2",
PreRun: getFunc("inside cmd2 PreRun"),
Run: getFunc("inside cmd2 Run"),
PostRun: getFunc("inside cmd2 PostRun"),
PersistentPostRun: getFunc("inside cmd2 PersistentPostRun"),
}
return cmd
}
func getRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "test-cobra-chain",
Short: "test-cobra-chain",
PersistentPreRun: getFunc("inside root PersistentPreRun"),
PreRun: getFunc("inside root PreRun"),
Run: getFunc("inside root Run"),
PostRun: getFunc("inside root PostRun"),
PersistentPostRun: getFunc("inside root PersistentPostRun"),
}
return cmd
}
func main() {
rootCmd := getRootCmd()
subCmd1 := getSubCmd1()
subCmd2 := getSubCmd2()
subCmd1.AddCommand(subCmd2)
rootCmd.AddCommand(subCmd1)
rootCmd.Execute()
}
func init() {
// nothing done
}
@algrebe
Copy link
Author

algrebe commented Jan 13, 2016

After a slight modification to the gist, i.e.

func init() {
    if err := cobra.EnableChainHooks(); err != nil {
        panic(err)
    }
}
Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ # added EnableChainHooks function to init()
Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ go build
Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ ls
cobrat  main.go
Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ ./cobrat
inside root PersistentPreRun with args []
inside root PreRun with args []
inside root Run with args []
inside root PostRun with args []
inside root PersistentPostRun with args []
Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ ./cobrat cmd1
inside root PersistentPreRun with args []
inside cmd1 PersistentPreRun with args []
inside cmd1 PreRun with args []
inside cmd1 Run with args []
inside cmd1 PostRun with args []
inside cmd1 PersistentPostRun with args []
inside root PersistentPostRun with args []
Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ ./cobrat cmd1 cmd2
inside root PersistentPreRun with args []
inside cmd1 PersistentPreRun with args []
inside cmd2 PreRun with args []
inside cmd2 Run with args []
inside cmd2 PostRun with args []
inside cmd2 PersistentPostRun with args []
inside cmd1 PersistentPostRun with args []
inside root PersistentPostRun with args []
Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ 

@algrebe
Copy link
Author

algrebe commented Jan 13, 2016

On adding an extra DisableChainHooks somewhere in main or wherever, you'll get the following

Dell-n411z:~/golang/default_ws/src/github.com/algrebe/cobrat$ ./cobrat cmd1 cmd2 arg1 arg2
panic: Toggle useChainHooks can be done only once !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment