mirror of
https://github.com/spf13/cobra
synced 2024-12-28 07:17:06 +00:00
Fixed Persistent-Run function propagation
This commit is contained in:
parent
3ee9552eeb
commit
42e6ce397f
1 changed files with 10 additions and 12 deletions
22
command.go
22
command.go
|
@ -463,8 +463,11 @@ func (c *Command) execute(a []string) (err error) {
|
|||
c.preRun()
|
||||
argWoFlags := c.Flags().Args()
|
||||
|
||||
if c.PersistentPreRun != nil {
|
||||
c.PersistentPreRun(c, argWoFlags)
|
||||
for p := c; p != nil; p = p.Parent() {
|
||||
if p.PersistentPreRun != nil {
|
||||
p.PersistentPreRun(c, argWoFlags)
|
||||
break
|
||||
}
|
||||
}
|
||||
if c.PreRun != nil {
|
||||
c.PreRun(c, argWoFlags)
|
||||
|
@ -475,8 +478,11 @@ func (c *Command) execute(a []string) (err error) {
|
|||
if c.PostRun != nil {
|
||||
c.PostRun(c, argWoFlags)
|
||||
}
|
||||
if c.PersistentPostRun != nil {
|
||||
c.PersistentPostRun(c, argWoFlags)
|
||||
for p := c; p != nil; p = p.Parent() {
|
||||
if p.PersistentPostRun != nil {
|
||||
p.PersistentPostRun(c, argWoFlags)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -601,14 +607,6 @@ func (c *Command) AddCommand(cmds ...*Command) {
|
|||
c.commandsMaxNameLen = nameLen
|
||||
}
|
||||
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