Created
January 8, 2016 06:54
-
-
Save algrebe/23cc8bf4739a129a6d0d to your computer and use it in GitHub Desktop.
golang example to test chaining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/algrebe/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", | |
PreRunChain: getFunc("inside cmd1 PreRunChain"), | |
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"), | |
PostRunChain: getFunc("inside cmd1 PostRunChain"), | |
} | |
return cmd | |
} | |
func getSubCmd2() *cobra.Command { | |
cmd := &cobra.Command{ | |
Use: "cmd2", | |
Short: "cmd2", | |
PreRunChain: getFunc("inside cmd2 PreRunChain"), | |
PersistentPreRun: getFunc("inside cmd2 PersistentPreRun"), | |
PreRun: getFunc("inside cmd2 PreRun"), | |
Run: getFunc("inside cmd2 Run"), | |
PostRun: getFunc("inside cmd2 PostRun"), | |
PersistentPostRun: getFunc("inside cmd2 PersistentPostRun"), | |
PostRunChain: getFunc("inside cmd2 PostRunChain"), | |
} | |
return cmd | |
} | |
func getRootCmd() *cobra.Command { | |
cmd := &cobra.Command{ | |
Use: "test-cobra-chain", | |
Short: "test-cobra-chain", | |
PreRunChain: getFunc("inside root PreRunChain"), | |
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"), | |
PostRunChain: getFunc("inside root PostRunChain"), | |
} | |
return cmd | |
} | |
func main() { | |
rootCmd := getRootCmd() | |
subCmd1 := getSubCmd1() | |
subCmd2 := getSubCmd2() | |
subCmd1.AddCommand(subCmd2) | |
rootCmd.AddCommand(subCmd1) | |
rootCmd.Execute() | |
} |
Author
algrebe
commented
Jan 8, 2016
$> ./test-cobra-chain cmd1 cmd2 arg1 arg2
inside root PreRunChain with args [arg1 arg2]
inside cmd1 PreRunChain with args [arg1 arg2]
inside cmd2 PreRunChain with args [arg1 arg2]
inside cmd2 PersistentPreRun with args [arg1 arg2]
inside cmd2 PreRun with args [arg1 arg2]
inside cmd2 Run with args [arg1 arg2]
inside cmd2 PostRun with args [arg1 arg2]
inside cmd2 PersistentPostRun with args [arg1 arg2]
inside cmd2 PostRunChain with args [arg1 arg2]
inside cmd1 PostRunChain with args [arg1 arg2]
inside root PostRunChain with args [arg1 arg2]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment