feat: scheme-aware shell completion for scan targets#3437
Open
ChrisJr404 wants to merge 1 commit into
Open
Conversation
The root command's ValidArgsFunction has only ever offered local Docker images; every other documented target scheme (registry:, oci-archive:, dir:, sbom:, purl:, ...) fell through to the shell with no hint of what was even legal. Now we suggest the documented scheme prefixes on an empty input or a partial match, dispatch to the docker daemon for docker:/podman: prefixes, and defer to the shell's file completion when a file/dir-based scheme is in play. The scheme list mirrors the prefixes documented in the root command's long-help text. NoSpace is set on the prefix completions so the user can keep typing after the colon without the shell treating ":" as a token boundary. Closes anchore#3432 Signed-off-by: ChrisJr404 <chris@hacknow.com>
kzantow
reviewed
May 11, 2026
| // targetSchemePrefixes is the set of scheme prefixes documented in the root command's long help text. | ||
| // Each entry includes the trailing ":" so that completion produces a value the user can keep typing | ||
| // after without having to add the separator themselves. | ||
| var targetSchemePrefixes = []string{ |
Contributor
There was a problem hiding this comment.
Thanks for this PR. I think we will need to experiment with it to see if it "feels good" for real world use, but I would note that we have a --from flag which is also intended to replace the scheme. I'm not sure if autocompleting the scheme is ideal. We would also want to make sure the behavior is the same with --from specified.
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.
Closes #3432.
Problem
The root command's
ValidArgsFunction(dockerImageValidArgsFunctionincmd/grype/cli/commands/completion.go) has only ever enumerated local Docker daemon images. Every other documented target scheme (registry:,oci-archive:,oci-dir:,singularity:,dir:,file:,sbom:,purl:,cpes:,podman:,docker-archive:) silently fell through to the shell, so a user typing<TAB>with no input sees nothing useful and a user typingoci-<TAB>gets no hint thatoci-archive:oroci-dir:is even a legal prefix.Change
dockerImageValidArgsFunctionis now scheme-aware::and the directive includesShellCompDirectiveNoSpaceso the user can keep typing the rest of the target (e.g.dir:./project) without the shell jumping to a fresh token.docker:andpodman:inputs, the local docker daemon is queried with the scheme stripped, and the scheme is re-prepended onto each tag so the completion is a complete, valid grype argument.sbom:,dir:,oci-archive:, etc.), the function returnsShellCompDirectiveDefaultso the shell does its own filename completion. That preserves the historical fallback rather than trying to second-guess what file extensions or directory shapes are valid for each scheme.The scheme list lives in a single
targetSchemePrefixesslice next to the function, and a coverage test pins it to every prefix documented in the root command's long-help text so the two won't drift.Verification
go test ./cmd/grype/cli/commands/passes.grype __complete "":ShellCompDirectiveNoSpaceoci-narrows tooci-archive:andoci-dir:dir:/tmp/returns no suggestions andShellCompDirectiveDefault, so the shell does path completiondocker:with no daemon falls through to the shell's defaultgo vet ./...is clean.Notes for reviewers
I kept the function name
dockerImageValidArgsFunctionto minimize diff; happy to rename it to something likescanTargetValidArgsFunctionif you'd prefer. I also resisted the urge to add per-scheme file-extension filters (sbom:to.json,oci-archive:to.tar) since several of those formats are accepted by extension and bare-name both, and a wrong filter would hurt UX more than it'd help.