cli/command/completion: change Platforms to return a cobra.CompletionFunc

It's adding a slight indirection by constructing a function when called,
but makes the completion functions more consistent, the signature easier
to read, and making the return type a [cobra.CompletionFunc] makes it
more transparent what it's intended for, and helps discovery of functions
that provide completion.

[cobra.CompletionFunc]: https://pkg.go.dev/github.com/spf13/cobra#CompletionFunc

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-30 11:26:54 +02:00
parent d18af47d0f
commit 810be9fbe9
2 changed files with 5 additions and 3 deletions

View File

@ -177,6 +177,8 @@ var commonPlatforms = []string{
// - we currently exclude architectures that may have unofficial builds,
// but don't have wide adoption (and no support), such as loong64, mipsXXX,
// ppc64 (non-le) to prevent confusion.
func Platforms(_ *cobra.Command, _ []string, _ string) (platforms []string, _ cobra.ShellCompDirective) {
return commonPlatforms, cobra.ShellCompDirectiveNoFileComp
func Platforms() cobra.CompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return commonPlatforms, cobra.ShellCompDirectiveNoFileComp
}
}

View File

@ -304,7 +304,7 @@ func TestCompleteNetworkNames(t *testing.T) {
}
func TestCompletePlatforms(t *testing.T) {
values, directives := Platforms(nil, nil, "")
values, directives := Platforms()(nil, nil, "")
assert.Check(t, is.Equal(directives&cobra.ShellCompDirectiveNoFileComp, cobra.ShellCompDirectiveNoFileComp), "Should not perform file completion")
assert.Check(t, is.DeepEqual(values, commonPlatforms))
}