cli/command/plugin: add completion for plugin subcommands
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
45
cli/command/plugin/completion.go
Normal file
45
cli/command/plugin/completion.go
Normal file
@ -0,0 +1,45 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@ -76,7 +76,7 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
options.context = args[1]
|
||||
return runCreate(cmd.Context(), dockerCLI, options)
|
||||
},
|
||||
ValidArgsFunction: cobra.NoFileCompletions,
|
||||
ValidArgsFunction: cobra.NoFileCompletions, // TODO(thaJeztah): should provide "directory" completion for the second arg
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
@ -24,6 +24,7 @@ func newDisableCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
|
||||
return nil
|
||||
},
|
||||
ValidArgsFunction: completeNames(dockerCLI, stateEnabled),
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
@ -25,6 +25,7 @@ func newEnableCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
|
||||
return nil
|
||||
},
|
||||
ValidArgsFunction: completeNames(dockerCLI, stateDisabled),
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
@ -29,6 +29,7 @@ func newInspectCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
opts.pluginNames = args
|
||||
return runInspect(cmd.Context(), dockerCLI, opts)
|
||||
},
|
||||
ValidArgsFunction: completeNames(dockerCLI, stateAny),
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
@ -36,6 +36,7 @@ func newInstallCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
}
|
||||
return runInstall(cmd.Context(), dockerCLI, options)
|
||||
},
|
||||
ValidArgsFunction: cobra.NoFileCompletions,
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
@ -20,6 +20,7 @@ func newPushCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
name := args[0]
|
||||
return runPush(cmd.Context(), dockerCLI, name)
|
||||
},
|
||||
ValidArgsFunction: completeNames(dockerCLI, stateAny),
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
@ -29,6 +29,7 @@ func newRemoveCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
opts.plugins = args
|
||||
return runRemove(cmd.Context(), dockerCLI, &opts)
|
||||
},
|
||||
ValidArgsFunction: completeNames(dockerCLI, stateAny),
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ func newSetCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return dockerCLI.Client().PluginSet(cmd.Context(), args[0], args[1:])
|
||||
},
|
||||
ValidArgsFunction: completeNames(dockerCLI, stateAny), // TODO(thaJeztah): should only complete for the first arg
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,6 +27,7 @@ func newUpgradeCommand(dockerCLI command.Cli) *cobra.Command {
|
||||
return runUpgrade(cmd.Context(), dockerCLI, options)
|
||||
},
|
||||
Annotations: map[string]string{"version": "1.26"},
|
||||
ValidArgsFunction: completeNames(dockerCLI, stateAny), // TODO(thaJeztah): should only complete for the first arg
|
||||
DisableFlagsInUseLine: true,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user