-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds Persistent{Pre,Post}Run hook chaining #1253
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,6 +102,21 @@ type Command struct { | |
// * PersistentPostRun() | ||
// All functions get the same args, the arguments after the command name. | ||
// | ||
// When TraverseChildrenHooks is set, PersistentPreRun and | ||
// PersistentPostRun are chained together so that each child | ||
// PersistentPreRun is ran, and the PersistentPostRun are ran in reverse | ||
// order. For example: | ||
// | ||
// Commands: root -> subcommand-a -> subcommand-b | ||
// | ||
// root - PersistentPreRun | ||
// subcommand-a - PersistentPreRun | ||
// subcommand-b - PersistentPreRun | ||
// subcommand-b - Run | ||
// subcommand-b - PersistentPostRun | ||
// subcommand-a - PersistentPostRun | ||
// root - PersistentPostRun | ||
// | ||
// PersistentPreRun: children of this command will inherit and execute. | ||
PersistentPreRun func(cmd *Command, args []string) | ||
// PersistentPreRunE: PersistentPreRun but returns an error. | ||
|
@@ -193,6 +208,11 @@ type Command struct { | |
// TraverseChildren parses flags on all parents before executing child command. | ||
TraverseChildren bool | ||
|
||
// TraverseChildrenHooks will have each subcommand's PersistentPreRun and | ||
// PersistentPostRun instead of overriding. It should be set on the root | ||
// command. | ||
TraverseChildrenHooks bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's worth adding some checks to ensure that this is set in the root command only (because |
||
|
||
// Hidden defines, if this command is hidden and should NOT show up in the list of available commands. | ||
Hidden bool | ||
|
||
|
@@ -829,55 +849,130 @@ func (c *Command) execute(a []string) (err error) { | |
return err | ||
} | ||
|
||
for p := c; p != nil; p = p.Parent() { | ||
if p.PersistentPreRunE != nil { | ||
if err := p.PersistentPreRunE(c, argWoFlags); err != nil { | ||
return err | ||
// Look to see if TraverseChildrenHooks is set on the root command. | ||
if _, err := c.runTree(c, argWoFlags, c.traverseChildrenHooks()); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Command) traverseChildrenHooks() bool { | ||
if c.HasParent() { | ||
return c.Parent().traverseChildrenHooks() | ||
} | ||
|
||
return c.TraverseChildrenHooks | ||
} | ||
|
||
func (c *Command) runTree( | ||
cmd *Command, | ||
args []string, | ||
traverseChildrenHooks bool, | ||
) ( | ||
persistentPostRunEs []func(cmd *Command, args []string) error, | ||
err error, | ||
) { | ||
if c == nil { | ||
return nil, nil | ||
} | ||
|
||
// Traverse command tree and save the PersistentPostRun{,E} functions. | ||
persistentPostRunEs, err = c.Parent().runTree(cmd, args, traverseChildrenHooks) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if traverseChildrenHooks || c == cmd { | ||
// PersistentPreRun/PersistentPreRunE | ||
switch { | ||
case c.PersistentPreRun != nil: | ||
c.PersistentPreRun(cmd, args) | ||
case c.PersistentPreRunE != nil: | ||
if err := c.PersistentPreRunE(cmd, args); err != nil { | ||
return nil, err | ||
} | ||
break | ||
} else if p.PersistentPreRun != nil { | ||
p.PersistentPreRun(c, argWoFlags) | ||
break | ||
default: | ||
// Doesn't have a registered PersistentPreRun{,E}. Move on... | ||
} | ||
|
||
// PersistentPostRun/PersistentPostRunE | ||
switch { | ||
case c.PersistentPostRun != nil: | ||
persistentPostRunEs = append( | ||
persistentPostRunEs, | ||
func(cmd *Command, args []string) error { | ||
c.PersistentPostRun(cmd, args) | ||
return nil | ||
}, | ||
) | ||
case c.PersistentPostRunE != nil: | ||
persistentPostRunEs = append( | ||
persistentPostRunEs, | ||
c.PersistentPostRunE, | ||
) | ||
default: | ||
// Doesn't have a registered PersistentPostRun{,E}. Move on... | ||
} | ||
} | ||
if c.PreRunE != nil { | ||
if err := c.PreRunE(c, argWoFlags); err != nil { | ||
return err | ||
|
||
if c != cmd { | ||
// Don't run a parent command. | ||
return persistentPostRunEs, nil | ||
} | ||
|
||
// PreRun/PreRunE | ||
switch { | ||
case c.PreRun != nil: | ||
c.PreRun(cmd, args) | ||
case c.PreRunE != nil: | ||
if err := c.PreRunE(cmd, args); err != nil { | ||
return nil, err | ||
} | ||
} else if c.PreRun != nil { | ||
c.PreRun(c, argWoFlags) | ||
default: | ||
// Doesn't have a registered PreRun{,E}. Move on... | ||
} | ||
|
||
if err := c.validateRequiredFlags(); err != nil { | ||
return err | ||
return nil, err | ||
} | ||
if c.RunE != nil { | ||
if err := c.RunE(c, argWoFlags); err != nil { | ||
return err | ||
|
||
// Run/RunE | ||
switch { | ||
case c.RunE != nil: | ||
if err := c.RunE(cmd, args); err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
c.Run(c, argWoFlags) | ||
} | ||
if c.PostRunE != nil { | ||
if err := c.PostRunE(c, argWoFlags); err != nil { | ||
return err | ||
case c.Run != nil: | ||
c.Run(cmd, args) | ||
default: | ||
// Both RunE and Run are nil... | ||
panic(fmt.Sprintf("command %q does not have a non-nil RunE or Run function", c.Use)) | ||
} | ||
|
||
// PostRun/PostRunE | ||
switch { | ||
case c.PostRun != nil: | ||
c.PostRun(cmd, args) | ||
case c.PostRunE != nil: | ||
if err := c.PostRunE(cmd, args); err != nil { | ||
return nil, err | ||
} | ||
} else if c.PostRun != nil { | ||
c.PostRun(c, argWoFlags) | ||
default: | ||
// Doesn't have a registered PostRun{,E}. Move on... | ||
} | ||
for p := c; p != nil; p = p.Parent() { | ||
if p.PersistentPostRunE != nil { | ||
if err := p.PersistentPostRunE(c, argWoFlags); err != nil { | ||
return err | ||
} | ||
break | ||
} else if p.PersistentPostRun != nil { | ||
p.PersistentPostRun(c, argWoFlags) | ||
break | ||
|
||
// PersistentPostRun/PersistentPostRunE | ||
// Iterate through the list in reverse order. Similar to a defer, allow | ||
// the topmost commands to cleanup first. | ||
for i := range persistentPostRunEs { | ||
r := persistentPostRunEs[len(persistentPostRunEs)-1-i] | ||
if err := r(cmd, args); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return nil | ||
return nil, nil | ||
} | ||
|
||
func (c *Command) preRun() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth adding something like
By default, if PersistentPreRun or PersistentPostRun is set, the parent's Pre/Post run hooks are not executed
?