mirror of
https://github.com/spf13/cobra
synced 2025-05-07 05:47:26 +00:00
Cleanup hooks that return void
This commit is contained in:
parent
849c34a6f0
commit
ceb71b9980
1 changed files with 12 additions and 25 deletions
37
command.go
37
command.go
|
@ -837,33 +837,21 @@ func (c *Command) execute(a []string) (err error) {
|
|||
if c.PreRunE != nil {
|
||||
preRunHooks = append(preRunHooks, c.PreRunE)
|
||||
} else if c.PreRun != nil {
|
||||
preRunHook := c.PreRun
|
||||
preRunHooks = append(preRunHooks, func(cmd *Command, args []string) error {
|
||||
preRunHook(cmd, args)
|
||||
return nil
|
||||
})
|
||||
preRunHooks = append(preRunHooks, wrapVoidHook(c.PreRun))
|
||||
}
|
||||
|
||||
// Merge the Run functions into the runHooks slice
|
||||
if c.RunE != nil {
|
||||
runHooks = append(runHooks, c.RunE)
|
||||
} else if c.Run != nil {
|
||||
runHook := c.Run
|
||||
runHooks = append(runHooks, func(cmd *Command, args []string) error {
|
||||
runHook(cmd, args)
|
||||
return nil
|
||||
})
|
||||
runHooks = append(runHooks, wrapVoidHook(c.Run))
|
||||
}
|
||||
|
||||
// Merge the PostRun functions into the runHooks slice
|
||||
if c.PostRunE != nil {
|
||||
postRunHooks = append(postRunHooks, c.PostRunE)
|
||||
} else if c.PostRun != nil {
|
||||
postRunHook := c.PostRun
|
||||
postRunHooks = append(postRunHooks, func(cmd *Command, args []string) error {
|
||||
postRunHook(cmd, args)
|
||||
return nil
|
||||
})
|
||||
postRunHooks = append(postRunHooks, wrapVoidHook(c.PostRun))
|
||||
}
|
||||
|
||||
// Find and merge the Persistent*Run functions into the persistent*Run slices.
|
||||
|
@ -877,11 +865,7 @@ func (c *Command) execute(a []string) (err error) {
|
|||
persistentPreRunHooks = prependHook(&persistentPreRunHooks, p.PersistentPreRunE)
|
||||
hasPersistentPreRunFromStruct = true
|
||||
} else if p.PersistentPreRun != nil {
|
||||
persistentPreRunHook := p.PersistentPreRun
|
||||
persistentPreRunHooks = prependHook(&persistentPreRunHooks, func(cmd *Command, args []string) error {
|
||||
persistentPreRunHook(cmd, args)
|
||||
return nil
|
||||
})
|
||||
persistentPreRunHooks = prependHook(&persistentPreRunHooks, wrapVoidHook(p.PersistentPreRun))
|
||||
hasPersistentPreRunFromStruct = true
|
||||
}
|
||||
}
|
||||
|
@ -890,11 +874,7 @@ func (c *Command) execute(a []string) (err error) {
|
|||
persistentPostRunHooks = append(persistentPostRunHooks, p.PersistentPostRunE)
|
||||
hasPersistentPostRunFromStruct = true
|
||||
} else if p.PersistentPostRun != nil {
|
||||
persistentPostRunHook := p.PersistentPostRun
|
||||
persistentPostRunHooks = append(persistentPostRunHooks, func(cmd *Command, args []string) error {
|
||||
persistentPostRunHook(cmd, args)
|
||||
return nil
|
||||
})
|
||||
persistentPostRunHooks = append(persistentPostRunHooks, wrapVoidHook(p.PersistentPostRun))
|
||||
hasPersistentPostRunFromStruct = true
|
||||
}
|
||||
}
|
||||
|
@ -947,6 +927,13 @@ func prependHook(hooks *[]func(cmd *Command, args []string) error, hook ...func(
|
|||
return append(hook, *hooks...)
|
||||
}
|
||||
|
||||
func wrapVoidHook(hook func(cmd *Command, args []string)) func(cmd *Command, args []string) error {
|
||||
return func(cmd *Command, args []string) error {
|
||||
hook(cmd, args)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// OnPersistentPreRun registers one or more hooks on the command to be executed
|
||||
// before the command or one of its children are executed
|
||||
func (c *Command) OnPersistentPreRun(f ...func(cmd *Command, args []string) error) {
|
||||
|
|
Loading…
Add table
Reference in a new issue