mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +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 {
|
if c.PreRunE != nil {
|
||||||
preRunHooks = append(preRunHooks, c.PreRunE)
|
preRunHooks = append(preRunHooks, c.PreRunE)
|
||||||
} else if c.PreRun != nil {
|
} else if c.PreRun != nil {
|
||||||
preRunHook := c.PreRun
|
preRunHooks = append(preRunHooks, wrapVoidHook(c.PreRun))
|
||||||
preRunHooks = append(preRunHooks, func(cmd *Command, args []string) error {
|
|
||||||
preRunHook(cmd, args)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge the Run functions into the runHooks slice
|
// Merge the Run functions into the runHooks slice
|
||||||
if c.RunE != nil {
|
if c.RunE != nil {
|
||||||
runHooks = append(runHooks, c.RunE)
|
runHooks = append(runHooks, c.RunE)
|
||||||
} else if c.Run != nil {
|
} else if c.Run != nil {
|
||||||
runHook := c.Run
|
runHooks = append(runHooks, wrapVoidHook(c.Run))
|
||||||
runHooks = append(runHooks, func(cmd *Command, args []string) error {
|
|
||||||
runHook(cmd, args)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge the PostRun functions into the runHooks slice
|
// Merge the PostRun functions into the runHooks slice
|
||||||
if c.PostRunE != nil {
|
if c.PostRunE != nil {
|
||||||
postRunHooks = append(postRunHooks, c.PostRunE)
|
postRunHooks = append(postRunHooks, c.PostRunE)
|
||||||
} else if c.PostRun != nil {
|
} else if c.PostRun != nil {
|
||||||
postRunHook := c.PostRun
|
postRunHooks = append(postRunHooks, wrapVoidHook(c.PostRun))
|
||||||
postRunHooks = append(postRunHooks, func(cmd *Command, args []string) error {
|
|
||||||
postRunHook(cmd, args)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find and merge the Persistent*Run functions into the persistent*Run slices.
|
// 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)
|
persistentPreRunHooks = prependHook(&persistentPreRunHooks, p.PersistentPreRunE)
|
||||||
hasPersistentPreRunFromStruct = true
|
hasPersistentPreRunFromStruct = true
|
||||||
} else if p.PersistentPreRun != nil {
|
} else if p.PersistentPreRun != nil {
|
||||||
persistentPreRunHook := p.PersistentPreRun
|
persistentPreRunHooks = prependHook(&persistentPreRunHooks, wrapVoidHook(p.PersistentPreRun))
|
||||||
persistentPreRunHooks = prependHook(&persistentPreRunHooks, func(cmd *Command, args []string) error {
|
|
||||||
persistentPreRunHook(cmd, args)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
hasPersistentPreRunFromStruct = true
|
hasPersistentPreRunFromStruct = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -890,11 +874,7 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
persistentPostRunHooks = append(persistentPostRunHooks, p.PersistentPostRunE)
|
persistentPostRunHooks = append(persistentPostRunHooks, p.PersistentPostRunE)
|
||||||
hasPersistentPostRunFromStruct = true
|
hasPersistentPostRunFromStruct = true
|
||||||
} else if p.PersistentPostRun != nil {
|
} else if p.PersistentPostRun != nil {
|
||||||
persistentPostRunHook := p.PersistentPostRun
|
persistentPostRunHooks = append(persistentPostRunHooks, wrapVoidHook(p.PersistentPostRun))
|
||||||
persistentPostRunHooks = append(persistentPostRunHooks, func(cmd *Command, args []string) error {
|
|
||||||
persistentPostRunHook(cmd, args)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
hasPersistentPostRunFromStruct = true
|
hasPersistentPostRunFromStruct = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -947,6 +927,13 @@ func prependHook(hooks *[]func(cmd *Command, args []string) error, hook ...func(
|
||||||
return append(hook, *hooks...)
|
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
|
// OnPersistentPreRun registers one or more hooks on the command to be executed
|
||||||
// before the command or one of its children are executed
|
// before the command or one of its children are executed
|
||||||
func (c *Command) OnPersistentPreRun(f ...func(cmd *Command, args []string) error) {
|
func (c *Command) OnPersistentPreRun(f ...func(cmd *Command, args []string) error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue