Skip to content

Commit d391d0f

Browse files
authored
Merge pull request #6445 from thaJeztah/add_plugin_completions
cli/command/plugin: add completion for plugin subcommands
2 parents 2f6abcf + 467fcfe commit d391d0f

File tree

10 files changed

+54
-1
lines changed

10 files changed

+54
-1
lines changed

cli/command/plugin/completion.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package plugin
2+
3+
import (
4+
"github.com/docker/cli/cli/command/completion"
5+
"github.com/moby/moby/api/types/filters"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
type pluginState string
10+
11+
const (
12+
stateAny pluginState = ""
13+
stateEnabled pluginState = "enabled"
14+
stateDisabled pluginState = "disabled"
15+
)
16+
17+
// completeNames offers completion for plugin names in the given state.
18+
// The state argument can be one of:
19+
//
20+
// - "all": all plugins
21+
// - "enabled": all enabled plugins
22+
// - "disabled": all disabled plugins
23+
func completeNames(dockerCLI completion.APIClientProvider, state pluginState) cobra.CompletionFunc {
24+
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
25+
f := filters.NewArgs()
26+
switch state {
27+
case stateEnabled:
28+
f.Add("enabled", "true")
29+
case stateDisabled:
30+
f.Add("enabled", "false")
31+
case stateAny:
32+
// no filter
33+
}
34+
35+
list, err := dockerCLI.Client().PluginList(cmd.Context(), f)
36+
if err != nil {
37+
return nil, cobra.ShellCompDirectiveError
38+
}
39+
var names []string
40+
for _, v := range list {
41+
names = append(names, v.Name)
42+
}
43+
return names, cobra.ShellCompDirectiveNoFileComp
44+
}
45+
}

cli/command/plugin/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
7676
options.context = args[1]
7777
return runCreate(cmd.Context(), dockerCLI, options)
7878
},
79-
ValidArgsFunction: cobra.NoFileCompletions,
79+
ValidArgsFunction: cobra.NoFileCompletions, // TODO(thaJeztah): should provide "directory" completion for the second arg
8080
DisableFlagsInUseLine: true,
8181
}
8282

cli/command/plugin/disable.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func newDisableCommand(dockerCLI command.Cli) *cobra.Command {
2424
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
2525
return nil
2626
},
27+
ValidArgsFunction: completeNames(dockerCLI, stateEnabled),
2728
DisableFlagsInUseLine: true,
2829
}
2930

cli/command/plugin/enable.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func newEnableCommand(dockerCLI command.Cli) *cobra.Command {
2525
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
2626
return nil
2727
},
28+
ValidArgsFunction: completeNames(dockerCLI, stateDisabled),
2829
DisableFlagsInUseLine: true,
2930
}
3031

cli/command/plugin/inspect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func newInspectCommand(dockerCLI command.Cli) *cobra.Command {
2929
opts.pluginNames = args
3030
return runInspect(cmd.Context(), dockerCLI, opts)
3131
},
32+
ValidArgsFunction: completeNames(dockerCLI, stateAny),
3233
DisableFlagsInUseLine: true,
3334
}
3435

cli/command/plugin/install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func newInstallCommand(dockerCLI command.Cli) *cobra.Command {
3636
}
3737
return runInstall(cmd.Context(), dockerCLI, options)
3838
},
39+
ValidArgsFunction: cobra.NoFileCompletions,
3940
DisableFlagsInUseLine: true,
4041
}
4142

cli/command/plugin/push.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func newPushCommand(dockerCLI command.Cli) *cobra.Command {
2020
name := args[0]
2121
return runPush(cmd.Context(), dockerCLI, name)
2222
},
23+
ValidArgsFunction: completeNames(dockerCLI, stateAny),
2324
DisableFlagsInUseLine: true,
2425
}
2526

cli/command/plugin/remove.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func newRemoveCommand(dockerCLI command.Cli) *cobra.Command {
2929
opts.plugins = args
3030
return runRemove(cmd.Context(), dockerCLI, &opts)
3131
},
32+
ValidArgsFunction: completeNames(dockerCLI, stateAny),
3233
DisableFlagsInUseLine: true,
3334
}
3435

cli/command/plugin/set.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func newSetCommand(dockerCLI command.Cli) *cobra.Command {
1414
RunE: func(cmd *cobra.Command, args []string) error {
1515
return dockerCLI.Client().PluginSet(cmd.Context(), args[0], args[1:])
1616
},
17+
ValidArgsFunction: completeNames(dockerCLI, stateAny), // TODO(thaJeztah): should only complete for the first arg
1718
DisableFlagsInUseLine: true,
1819
}
1920
}

cli/command/plugin/upgrade.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func newUpgradeCommand(dockerCLI command.Cli) *cobra.Command {
2727
return runUpgrade(cmd.Context(), dockerCLI, options)
2828
},
2929
Annotations: map[string]string{"version": "1.26"},
30+
ValidArgsFunction: completeNames(dockerCLI, stateAny), // TODO(thaJeztah): should only complete for the first arg
3031
DisableFlagsInUseLine: true,
3132
}
3233

0 commit comments

Comments
 (0)