mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +00:00
Tidy up the persistent hooks code a bit
This commit is contained in:
parent
5a812a617c
commit
849c34a6f0
1 changed files with 18 additions and 17 deletions
35
command.go
35
command.go
|
@ -869,37 +869,33 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
// Find and merge the Persistent*Run functions into the persistent*Run slices.
|
// Find and merge the Persistent*Run functions into the persistent*Run slices.
|
||||||
// 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.
|
||||||
hasLegacyPersistentPreRun := false
|
hasPersistentPreRunFromStruct := false
|
||||||
hasLegacyPersistentPostRun := false
|
hasPersistentPostRunFromStruct := false
|
||||||
for p := c; p != nil; p = p.Parent() {
|
for p := c; p != nil; p = p.Parent() {
|
||||||
if !hasLegacyPersistentPreRun || !EnablePersistentRunOverride {
|
if !hasPersistentPreRunFromStruct || !EnablePersistentRunOverride {
|
||||||
if p.PersistentPreRunE != nil {
|
if p.PersistentPreRunE != nil {
|
||||||
persistentPreRunHooks = append([]func(cmd *Command, args []string) error{
|
persistentPreRunHooks = prependHook(&persistentPreRunHooks, p.PersistentPreRunE)
|
||||||
p.PersistentPreRunE,
|
hasPersistentPreRunFromStruct = true
|
||||||
}, persistentPreRunHooks...)
|
|
||||||
hasLegacyPersistentPreRun = true
|
|
||||||
} else if p.PersistentPreRun != nil {
|
} else if p.PersistentPreRun != nil {
|
||||||
persistentPreRunHook := p.PersistentPreRun
|
persistentPreRunHook := p.PersistentPreRun
|
||||||
persistentPreRunHooks = append([]func(cmd *Command, args []string) error{
|
persistentPreRunHooks = prependHook(&persistentPreRunHooks, func(cmd *Command, args []string) error {
|
||||||
func(cmd *Command, args []string) error {
|
persistentPreRunHook(cmd, args)
|
||||||
persistentPreRunHook(cmd, args)
|
return nil
|
||||||
return nil
|
})
|
||||||
},
|
hasPersistentPreRunFromStruct = true
|
||||||
}, persistentPreRunHooks...)
|
|
||||||
hasLegacyPersistentPreRun = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !hasLegacyPersistentPostRun || !EnablePersistentRunOverride {
|
if !hasPersistentPostRunFromStruct || !EnablePersistentRunOverride {
|
||||||
if p.PersistentPostRunE != nil {
|
if p.PersistentPostRunE != nil {
|
||||||
persistentPostRunHooks = append(persistentPostRunHooks, p.PersistentPostRunE)
|
persistentPostRunHooks = append(persistentPostRunHooks, p.PersistentPostRunE)
|
||||||
hasLegacyPersistentPostRun = true
|
hasPersistentPostRunFromStruct = true
|
||||||
} else if p.PersistentPostRun != nil {
|
} else if p.PersistentPostRun != nil {
|
||||||
persistentPostRunHook := p.PersistentPostRun
|
persistentPostRunHook := p.PersistentPostRun
|
||||||
persistentPostRunHooks = append(persistentPostRunHooks, func(cmd *Command, args []string) error {
|
persistentPostRunHooks = append(persistentPostRunHooks, func(cmd *Command, args []string) error {
|
||||||
persistentPostRunHook(cmd, args)
|
persistentPostRunHook(cmd, args)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
hasLegacyPersistentPostRun = true
|
hasPersistentPostRunFromStruct = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -946,6 +942,11 @@ func (c *Command) executeHooks(hooks *[]func(cmd *Command, args []string) error,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepend a hook onto the slice of hooks
|
||||||
|
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...)
|
||||||
|
}
|
||||||
|
|
||||||
// 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