-
Notifications
You must be signed in to change notification settings - Fork 95
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
[Event] Add EVENT_CLI_SUCCESSFUL_EXECUTE #224
Conversation
knack/cli.py
Outdated
@@ -219,6 +219,7 @@ def invoke(self, args, initial_invocation_data=None, out_file=None): | |||
if cmd_result and cmd_result.result is not None: | |||
formatter = self.output.get_formatter(output_type) | |||
self.output.out(cmd_result, formatter=formatter, out_file=out_file) | |||
self.raise_event(EVENT_CLI_SUCCESSFUL_EXECUTE) |
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.
two concerns:
- could the new event bring benefit to CLI if exit code = 0 could represent successful execution?
- should SUCCESSFUL_EXECUTE be raised after cmd_result is returned? It seems that in current code output format failure would block the event.
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.
could the new event bring benefit to CLI if exit code = 0 could represent successful execution?
I couldn't really get your question. Could you elaborate?
should SUCCESSFUL_EXECUTE be raised after cmd_result is returned?
No. If self.output.out
failed, this is considered a failure and SUCCESSFUL_EXECUTE
shouldn't be raised by design.
@@ -219,6 +219,7 @@ def invoke(self, args, initial_invocation_data=None, out_file=None): | |||
if cmd_result and cmd_result.result is not None: | |||
formatter = self.output.get_formatter(output_type) | |||
self.output.out(cmd_result, formatter=formatter, out_file=out_file) | |||
self.raise_event(EVENT_CLI_SUCCESSFUL_EXECUTE, result=cmd_result.result) |
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.
Should we raise the event only when exit_code is 0?
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.
Yes. On the other hand, EVENT_CLI_POST_EXECUTE
is raised unconditionally.
Lines 233 to 234 in 61057a3
finally: | |
self.raise_event(EVENT_CLI_POST_EXECUTE) |
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.
Then should you check the exit_code
from
Line 217 in 61057a3
exit_code = self.result.exit_code |
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.
Not really necessary.
Line 215 in 61057a3
cmd_result = self.invocation.execute(args) |
will raise an exception first if exit_code
is not 0
.
Also,
Line 221 in 61057a3
self.output.out(cmd_result, formatter=formatter, out_file=out_file) |
already assumes the command is successfully executed and exit_code
is 0
.
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.
got it.
Context
In the current implementation, the command handler is invoked at
L215
:knack/knack/cli.py
Line 215 in 0d0308b
The JSON is printed at
L221
:knack/knack/cli.py
Line 221 in 8b3757d
Therefore, anything printed by the command handler will be shown before the JSON output.
Change
Add a new event
EVENT_CLI_SUCCESSFUL_EXECUTE
. It is raised after a command is successfully executed, allowing a callbackfunc
to be registered and called after JSON is printed.This enables post-output hint to be displayed after the JSON output.