Suppress "Succeeded" line after vendir completion output - #457
Open
pujitha24 wants to merge 1 commit into
Open
Conversation
Motivation: `vendir completion bash` (and zsh/fish/powershell) prints a shell script to stdout that is meant to be sourced or eval'd directly, e.g. `source <(vendir completion bash)`. main() unconditionally printed a trailing "Succeeded" line after every command, including completion, so the sourced script ended with a bare `Succeeded` token, which the shell then tried to execute as a command. Approach: main() now captures the actually-executed *cobra.Command via command.ExecuteC() (Execute() is just ExecuteC() with the command discarded, so this is behavior-preserving for every other command) and skips the "Succeeded" line when the executed command is cobra's built-in "completion" command or one of its bash/zsh/fish/powershell subcommands. vendir does not define its own completion command, so this only affects cobra's default completion tree. An initial attempt mirrored the fix already applied in carvel-dev/kapp (cmd/kapp/kapp.go), which guards the same print with cobrautil.IsCobraManagedCommand(os.Args). Building and testing that approach against this issue showed it does not actually suppress the line for `completion bash`: IsCobraManagedCommand only special-cases "help", "__complete" and "__completeNoDesc", not "completion" itself, so it does not resolve this report. Validation: - go build ./... - ./hack/test.sh (repo's documented unit test script) - all packages pass; this code path has no existing unit tests. - ./hack/build.sh (repo's documented build script: go mod vendor/tidy, go fmt, real build, `./vendir version`, compile-only `go test --exec=echo ./...`) - all steps pass. - Manually built the binary before and after the change and compared: `vendir completion bash|zsh|fish|powershell | tail -n 3` no longer ends with "Succeeded" (it did before), while `vendir version` and an error path (`vendir sync -f /nonexistent.yml`, exit 1) are unaffected and still print/omit "Succeeded" as before. - `bash -c 'source <(vendir completion bash) && echo SOURCE_OK'` succeeds after the fix, reproducing and resolving the exact failure mode from the report (it fails without the fix, since the shell tries to run the stray `Succeeded` token as a command). User-visible behavior for every non-completion command is unchanged. Report: carvel-dev#405 Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes vendir completion <shell> output so it can be safely sourced/eval’d by suppressing the trailing Succeeded line after completion script generation, addressing the reported Succeeded token execution error (Fixes #405).
Changes:
- Switches from
command.Execute()tocommand.ExecuteC()to capture the executed Cobra command. - Skips printing
Succeededwhen the executed command is Cobra’scompletioncommand (or its shell subcommands). - Adds a small helper (
isCompletionCmd) to detect when the executed command is part of the completion command tree.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+46
to
48
| func isCompletionCmd(c *cobra.Command) bool { | ||
| return c.Name() == "completion" || (c.HasParent() && c.Parent().Name() == "completion") | ||
| } |
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.
Motivation:
vendir completion bash(and zsh/fish/powershell) prints a shellscript to stdout that is meant to be sourced or eval'd directly, e.g.
source <(vendir completion bash). main() unconditionally printed atrailing "Succeeded" line after every command, including completion,
so the sourced script ended with a bare
Succeededtoken, which theshell then tried to execute as a command.
Approach:
main() now captures the actually-executed *cobra.Command via
command.ExecuteC() (Execute() is just ExecuteC() with the command
discarded, so this is behavior-preserving for every other command)
and skips the "Succeeded" line when the executed command is cobra's
built-in "completion" command or one of its bash/zsh/fish/powershell
subcommands. vendir does not define its own completion command, so
this only affects cobra's default completion tree.
An initial attempt mirrored the fix already applied in carvel-dev/kapp
(cmd/kapp/kapp.go), which guards the same print with
cobrautil.IsCobraManagedCommand(os.Args). Building and testing that
approach against this issue showed it does not actually suppress the
line for
completion bash: IsCobraManagedCommand only special-cases"help", "__complete" and "__completeNoDesc", not "completion" itself,
so it does not resolve this report.
Validation:
pass; this code path has no existing unit tests.
vendor/tidy, go fmt, real build,
./vendir version, compile-onlygo test --exec=echo ./...) - all steps pass.vendir completion bash|zsh|fish|powershell | tail -n 3no longerends with "Succeeded" (it did before), while
vendir versionandan error path (
vendir sync -f /nonexistent.yml, exit 1) areunaffected and still print/omit "Succeeded" as before.
bash -c 'source <(vendir completion bash) && echo SOURCE_OK'succeeds after the fix, reproducing and resolving the exact failure
mode from the report (it fails without the fix, since the shell
tries to run the stray
Succeededtoken as a command).User-visible behavior for every non-completion command is unchanged.
Report: #405
Signed-off-by: Pujitha Paladugu 10557236+pujitha24@users.noreply.github.com
Fixes #405