Files
docker-cli/cli/command/plugin/completion.go
Sebastiaan van Stijn 467fcfe4bd cli/command/plugin: add completion for plugin subcommands
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-10 11:49:16 +02:00

46 lines
1.1 KiB
Go

package plugin
import (
"github.com/docker/cli/cli/command/completion"
"github.com/moby/moby/api/types/filters"
"github.com/spf13/cobra"
)
type pluginState string
const (
stateAny pluginState = ""
stateEnabled pluginState = "enabled"
stateDisabled pluginState = "disabled"
)
// completeNames offers completion for plugin names in the given state.
// The state argument can be one of:
//
// - "all": all plugins
// - "enabled": all enabled plugins
// - "disabled": all disabled plugins
func completeNames(dockerCLI completion.APIClientProvider, state pluginState) cobra.CompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
f := filters.NewArgs()
switch state {
case stateEnabled:
f.Add("enabled", "true")
case stateDisabled:
f.Add("enabled", "false")
case stateAny:
// no filter
}
list, err := dockerCLI.Client().PluginList(cmd.Context(), f)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, v := range list {
names = append(names, v.Name)
}
return names, cobra.ShellCompDirectiveNoFileComp
}
}