cmd/docker: print command error before running plugin hooks - #7228
Open
adisivaprasad wants to merge 1 commit into
Open
cmd/docker: print command error before running plugin hooks#7228adisivaprasad wants to merge 1 commit into
adisivaprasad wants to merge 1 commit into
Conversation
When a CLI command fails and an error-hook (or hook) produces a "What's next" hint, the hint was printed before the command's error message. The command's error is only printed by main() after runDocker() returns, but plugin hooks run inside runDocker() right after cmd.ExecuteContext(). Print the pending command error to stderr before invoking the hooks, and replace the returned error with a silent cli.StatusError that only carries the exit code, so that main() does not print it a second time. Canceled and signal-terminated errors (which print nothing) are left untouched, and exit codes are unchanged. Signed-off-by: adisivaprasad <adisivaprasad@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
- What I did
Fixes #6973 — when a command fails and a plugin
error-hook(or hook) produces a "What's next" hint, the hint was printed before the command's error message:- How I did it
The cobra tree runs with
SilenceErrors: true, so the error is only printed bymain()afterrunDocker()returns, while plugin hooks run insiderunDocker()right aftercmd.ExecuteContext()— hence the inverted order.In
runDocker(), before invoking the hooks I now print the pending (non-canceled, non-silent) command error to stderr, and replace the returned error with a barecli.StatusErrorcarrying the exit code only, somain()doesn't print it a second time. Canceled and signal-terminated errors (which print nothing) pass through untouched, and exit codes are unchanged.- How to verify it
Repro for that area is in the issue. I verified end-to-end with a stub hook plugin (
docker-demoCLI plugin answeringdocker-cli-plugin-hookswith a fixed next-steps template) configured with"error-hooks": "run",DOCKER_CLI_HOOKS=true, under a pty:Before (current
masterbuild):After (this patch):
hooks: contextconfig ondocker context ls).go test ./cmd/docker/... ./cli-plugins/...passes.