Add OnFinalize method (#1788)
This method is the OnInitialize counterpart. Like OnInitialize which allows loading the configuration before each command is executed, OnFinalize allows saving the configuration after each command has been executed.
This commit is contained in:
@ -834,6 +834,8 @@ func (c *Command) execute(a []string) (err error) {
|
||||
|
||||
c.preRun()
|
||||
|
||||
defer c.postRun()
|
||||
|
||||
argWoFlags := c.Flags().Args()
|
||||
if c.DisableFlagParsing {
|
||||
argWoFlags = a
|
||||
@ -904,6 +906,12 @@ func (c *Command) preRun() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Command) postRun() {
|
||||
for _, x := range finalizers {
|
||||
x()
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteContext is the same as Execute(), but sets the ctx on the command.
|
||||
// Retrieve ctx by calling cmd.Context() inside your *Run lifecycle or ValidArgs
|
||||
// functions.
|
||||
|
Reference in New Issue
Block a user