Comment updates within the hooks code

This commit is contained in:
Bart de Boer 2020-06-30 16:10:22 +02:00
parent ceb71b9980
commit 45ac9307d4

View file

@ -833,28 +833,28 @@ func (c *Command) execute(a []string) (err error) {
postRunHooks := c.postRunHooks postRunHooks := c.postRunHooks
var persistentPostRunHooks []func(cmd *Command, args []string) error var persistentPostRunHooks []func(cmd *Command, args []string) error
// Merge the PreRun functions into the preRunHooks slice // Merge the PreRun* functions into the preRunHooks array
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 {
preRunHooks = append(preRunHooks, wrapVoidHook(c.PreRun)) preRunHooks = append(preRunHooks, wrapVoidHook(c.PreRun))
} }
// Merge the Run functions into the runHooks slice // Merge the Run* functions into the runHooks array
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 {
runHooks = append(runHooks, wrapVoidHook(c.Run)) runHooks = append(runHooks, wrapVoidHook(c.Run))
} }
// Merge the PostRun functions into the runHooks slice // Merge the PostRun* functions into the runHooks array
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 {
postRunHooks = append(postRunHooks, wrapVoidHook(c.PostRun)) postRunHooks = append(postRunHooks, wrapVoidHook(c.PostRun))
} }
// Find and merge the Persistent*Run functions into the persistent*Run slices. // Find and merge the Persistent*Run functions into the persistent*Run array.
// If EnablePersistentRunOverride is set Persistent*Run from childs will override their parents. // If EnablePersistentRunOverride is set Persistent*Run from childs will override their parents.
// Any hooks registered through OnPersistent*Run will always be executed and cannot be overriden. // Any hooks registered through OnPersistent*Run will always be executed and cannot be overriden.
hasPersistentPreRunFromStruct := false hasPersistentPreRunFromStruct := false
@ -912,7 +912,7 @@ func (c *Command) preRun() {
} }
} }
// executeHooks executes a slice of hooks // executeHooks executes the hooks
func (c *Command) executeHooks(hooks *[]func(cmd *Command, args []string) error, args []string) error { func (c *Command) executeHooks(hooks *[]func(cmd *Command, args []string) error, args []string) error {
for _, x := range *hooks { for _, x := range *hooks {
if err := x(c, args); err != nil { if err := x(c, args); err != nil {
@ -922,11 +922,12 @@ func (c *Command) executeHooks(hooks *[]func(cmd *Command, args []string) error,
return nil return nil
} }
// prepend a hook onto the slice of hooks // prependHook prepends a hook onto the array of hooks
func prependHook(hooks *[]func(cmd *Command, args []string) error, hook ...func(cmd *Command, args []string) error) []func(cmd *Command, args []string) error { func prependHook(hooks *[]func(cmd *Command, args []string) error, hook ...func(cmd *Command, args []string) error) []func(cmd *Command, args []string) error {
return append(hook, *hooks...) return append(hook, *hooks...)
} }
// wrapVoidHook wraps a void hook into a function having the return error signature
func wrapVoidHook(hook func(cmd *Command, args []string)) func(cmd *Command, args []string) error { func wrapVoidHook(hook func(cmd *Command, args []string)) func(cmd *Command, args []string) error {
return func(cmd *Command, args []string) error { return func(cmd *Command, args []string) error {
hook(cmd, args) hook(cmd, args)