[Event] Add EVENT_CLI_SUCCESSFUL_EXECUTE#224
Conversation
knack/cli.py
Outdated
| 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.
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.
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.
| 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.
Should we raise the event only when exit_code is 0?
There was a problem hiding this comment.
Yes. On the other hand, EVENT_CLI_POST_EXECUTE is raised unconditionally.
Lines 233 to 234 in 61057a3
There was a problem hiding this comment.
Then should you check the exit_code from
Line 217 in 61057a3
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 callbackfuncto be registered and called after JSON is printed.This enables post-output hint to be displayed after the JSON output.