Skip to content

Commit 7b26aa2

Browse files
authored
fix(cli): enable autocompletion for zsh (#3433)
Signed-off-by: Brandt Keller <brandt.keller@defenseunicorns.com>
1 parent 70459ff commit 7b26aa2

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

cmd/grype/cli/commands/completion.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,13 @@ func listLocalDockerImages(prefix string) ([]string, error) {
9696
}
9797

9898
func dockerImageValidArgsFunction(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
99-
// Since we use ValidArgsFunction, Cobra will call this AFTER having parsed all flags and arguments provided
99+
// Since we use ValidArgsFunction, Cobra will call this AFTER having parsed all flags and arguments provided.
100+
// When the docker daemon is unavailable (or has no images), fall through to the shell's default behavior so
101+
// that subcommand completions and filename completion still work — returning ShellCompDirectiveError here
102+
// causes shells (notably zsh) to discard all completions, including the auto-generated subcommand list.
100103
dockerImageRepoTags, err := listLocalDockerImages(toComplete)
101-
if err != nil {
102-
// Indicates that an error occurred and completions should be ignored
103-
return []string{"completion failed"}, cobra.ShellCompDirectiveError
104-
}
105-
if len(dockerImageRepoTags) == 0 {
106-
return []string{"no docker images found"}, cobra.ShellCompDirectiveError
104+
if err != nil || len(dockerImageRepoTags) == 0 {
105+
return nil, cobra.ShellCompDirectiveDefault
107106
}
108-
// ShellCompDirectiveDefault indicates that the shell will perform its default behavior after completions have
109-
// been provided (without implying other possible directives)
110107
return dockerImageRepoTags, cobra.ShellCompDirectiveDefault
111108
}

0 commit comments

Comments
 (0)