Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cli/command/plugin/completion.go
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion cli/command/plugin/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/plugin/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func newDisableCommand(dockerCLI command.Cli) *cobra.Command {
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
return nil
},
ValidArgsFunction: completeNames(dockerCLI, stateEnabled),
DisableFlagsInUseLine: true,
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/plugin/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func newEnableCommand(dockerCLI command.Cli) *cobra.Command {
_, _ = fmt.Fprintln(dockerCLI.Out(), name)
return nil
},
ValidArgsFunction: completeNames(dockerCLI, stateDisabled),
DisableFlagsInUseLine: true,
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/plugin/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/plugin/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func newInstallCommand(dockerCLI command.Cli) *cobra.Command {
}
return runInstall(cmd.Context(), dockerCLI, options)
},
ValidArgsFunction: cobra.NoFileCompletions,
DisableFlagsInUseLine: true,
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/plugin/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/plugin/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
1 change: 1 addition & 0 deletions cli/command/plugin/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
1 change: 1 addition & 0 deletions cli/command/plugin/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
Loading