mirror of
https://github.com/spf13/cobra
synced 2024-11-16 18:57:08 +00:00
added persistent pre/post commands.
This commit is contained in:
parent
50142bdfe4
commit
caba65771b
1 changed files with 28 additions and 2 deletions
30
command.go
30
command.go
|
@ -53,6 +53,10 @@ type Command struct {
|
||||||
// PostRun runs the command after run.
|
// PostRun runs the command after run.
|
||||||
// The args are the arguments after the command name.
|
// The args are the arguments after the command name.
|
||||||
PostRun func(cmd *Command, args []string)
|
PostRun func(cmd *Command, args []string)
|
||||||
|
// PreRun which children of this command will inherit.
|
||||||
|
PersistentPreRun func(cmd *Command, args []string)
|
||||||
|
// PostRun which children of this command will inherit.
|
||||||
|
PersistentPostRun func(cmd *Command, args []string)
|
||||||
// Commands is the list of commands supported by this program.
|
// Commands is the list of commands supported by this program.
|
||||||
commands []*Command
|
commands []*Command
|
||||||
// Parent Command for this command
|
// Parent Command for this command
|
||||||
|
@ -387,6 +391,9 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
c.preRun()
|
c.preRun()
|
||||||
argWoFlags := c.Flags().Args()
|
argWoFlags := c.Flags().Args()
|
||||||
|
|
||||||
|
if c.PersistentPreRun != nil {
|
||||||
|
c.PersistentPreRun(c, argWoFlags)
|
||||||
|
}
|
||||||
if c.PreRun != nil {
|
if c.PreRun != nil {
|
||||||
c.PreRun(c, argWoFlags)
|
c.PreRun(c, argWoFlags)
|
||||||
}
|
}
|
||||||
|
@ -396,6 +403,9 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
if c.PostRun != nil {
|
if c.PostRun != nil {
|
||||||
c.PostRun(c, argWoFlags)
|
c.PostRun(c, argWoFlags)
|
||||||
}
|
}
|
||||||
|
if c.PersistentPostRun != nil {
|
||||||
|
c.PersistentPostRun(c, argWoFlags)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -482,19 +492,25 @@ func (c *Command) Execute() (err error) {
|
||||||
// print the usage
|
// print the usage
|
||||||
c.Usage()
|
c.Usage()
|
||||||
} else {
|
} else {
|
||||||
// Only flags left... Call root.Run
|
|
||||||
c.preRun()
|
c.preRun()
|
||||||
|
|
||||||
|
if c.PersistentPreRun != nil {
|
||||||
|
c.PersistentPreRun(c, argWoFlags)
|
||||||
|
}
|
||||||
if c.PreRun != nil {
|
if c.PreRun != nil {
|
||||||
c.PreRun(c, argWoFlags)
|
c.PreRun(c, argWoFlags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Only flags left... Call root.Run
|
||||||
c.Run(c, argWoFlags)
|
c.Run(c, argWoFlags)
|
||||||
err = nil
|
err = nil
|
||||||
|
|
||||||
if c.PostRun != nil {
|
if c.PostRun != nil {
|
||||||
c.PostRun(c, argWoFlags)
|
c.PostRun(c, argWoFlags)
|
||||||
}
|
}
|
||||||
|
if c.PersistentPostRun != nil {
|
||||||
|
c.PersistentPostRun(c, argWoFlags)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -519,7 +535,9 @@ func (c *Command) initHelp() {
|
||||||
Short: "Help about any command",
|
Short: "Help about any command",
|
||||||
Long: `Help provides help for any command in the application.
|
Long: `Help provides help for any command in the application.
|
||||||
Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
||||||
Run: c.HelpFunc(),
|
Run: c.HelpFunc(),
|
||||||
|
PersistentPreRun: func(cmd *Command, args []string) {},
|
||||||
|
PersistentPostRun: func(cmd *Command, args []string) {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.AddCommand(c.helpCommand)
|
c.AddCommand(c.helpCommand)
|
||||||
|
@ -555,6 +573,14 @@ func (c *Command) AddCommand(cmds ...*Command) {
|
||||||
c.commandsMaxCommandPathLen = commandPathLen
|
c.commandsMaxCommandPathLen = commandPathLen
|
||||||
}
|
}
|
||||||
c.commands = append(c.commands, x)
|
c.commands = append(c.commands, x)
|
||||||
|
|
||||||
|
// Pass on peristent pre/post functions to children
|
||||||
|
if x.PersistentPreRun == nil {
|
||||||
|
x.PersistentPreRun = c.PersistentPreRun
|
||||||
|
}
|
||||||
|
if x.PersistentPostRun == nil {
|
||||||
|
x.PersistentPostRun = c.PersistentPostRun
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue