Skip to content
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

Pre Run and Post Run chain functions #219

Closed
algrebe opened this issue Jan 8, 2016 · 2 comments
Closed

Pre Run and Post Run chain functions #219

algrebe opened this issue Jan 8, 2016 · 2 comments

Comments

@algrebe
Copy link

algrebe commented Jan 8, 2016

W.r.t #216

Persitent* functions execute only if the child has not overridden the parent.

Consider the following scenario:
Root command has a PersistentPreRun in which it defines a logger, sets the log level and starts a profiler.
Root command has a PersistentPostRun function in which it stops the profiler safely, closes file descriptors etc, whatever is needed for a clean and safe exit.

Child command also has a PersistentPreRun and PersistentPostRun for its own reasons - for its own child commands.
Since the child command has defined its own Persistent_, the root command Persistent_ are not called.

To solve this, there need to be hooks that get fired irrespective of the child having its own hook - these are chain hooks.

PreRunChain gets called in the order of parent to child

PostRunChain gets called in the order of child to parent
i.e. root PreRunChain -> child PreRunChain

And PostRunChain gets called in the order of child to parent
i.e. child PostRunChain -> root PostRunChain

So for the previous scenario

root PreRunChain sets logger and profiler
child PreRunChain does its own initialization etc

/* the usual stuff that existed before - the overriding logic
PersistentPreRun
PreRun
Run
PostRun
PersistentPostRun
*/

child PostRunChain frees up its own things, for its safe exit
root PostRunChain stops the profiler

algrebe pushed a commit to algrebe/cobra that referenced this issue Jan 8, 2016
PreRunChain functions run in the order of root to child.
PostRunChain functions run in the order of child to root.

The overall exection order for a command is
PreRunChain
PersistentPreRun
PreRun
Run
PostRun
PersistentPostRun
PostRunChain

The following gist describes the usage and functionality
https://gist.github.com/algrebe/23cc8bf4739a129a6d0d
algrebe added a commit to algrebe/cobra that referenced this issue Jan 8, 2016
PreRunChain functions run in the order of root to child.
PostRunChain functions run in the order of child to root.

The overall exection order for a command is
PreRunChain
PersistentPreRun
PreRun
Run
PostRun
PersistentPostRun
PostRunChain

The following gist describes the usage and functionality
https://gist.github.com/algrebe/23cc8bf4739a129a6d0d
algrebe added a commit to algrebe/cobra that referenced this issue Jan 13, 2016
This reverts commit 3dbb5be.
As part of spf13#220
PreRunChain and PostRunChain aren't required - the PersistentRun functions
need to be modified
algrebe added a commit to algrebe/cobra that referenced this issue Jan 13, 2016
Added the functionality for persistent functions to chain
from root to child in the case of PersistentPreRun and
from child to root in the case of PersistentPostRun.

Added a global internal variable useChainHooks which decides
whether to use chain feature, or use the previous logic of nearest
defined ancestor.
This variable useChainHooks is disabled by default in this commit.
Developers importing the cobra package can enable it using the
function EnableChainHooks() or disable it using DisableChainHooks().
However, these functions may be called only once externally, preferably
in the init of the developers main program.
@henvic
Copy link

henvic commented Dec 27, 2016

When you have no Run for a given command and you end up printing the help for that given command: pre/post are silently skipped. This is the related [and not as intended], correct?

@n10v
Copy link
Collaborator

n10v commented Oct 30, 2017

Duplicate of #252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants