-
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
Pre Run and Post Run chain functions #219
Comments
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
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.
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? |
Duplicate of #252 |
9 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
child PostRunChain frees up its own things, for its safe exit
root PostRunChain stops the profiler
The text was updated successfully, but these errors were encountered: