Merge pull request #5970 from thaJeztah/swarm_completion_cleanup
cli/command/service: un-export CompletionFn
This commit is contained in:
commit
fc817a1367
@ -1,12 +1,8 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/docker/cli/cli"
|
"github.com/docker/cli/cli"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/cli/command/completion"
|
|
||||||
"github.com/docker/docker/api/types"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,27 +31,3 @@ func NewServiceCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
)
|
)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompletionFn offers completion for swarm service names and optional IDs.
|
|
||||||
// By default, only names are returned.
|
|
||||||
// Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs.
|
|
||||||
func CompletionFn(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
|
|
||||||
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
|
|
||||||
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes"
|
|
||||||
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
||||||
list, err := dockerCLI.Client().ServiceList(cmd.Context(), types.ServiceListOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return nil, cobra.ShellCompDirectiveError
|
|
||||||
}
|
|
||||||
|
|
||||||
names := make([]string, 0, len(list))
|
|
||||||
for _, service := range list {
|
|
||||||
if showIDs {
|
|
||||||
names = append(names, service.Spec.Name, service.ID)
|
|
||||||
} else {
|
|
||||||
names = append(names, service.Spec.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return names, cobra.ShellCompDirectiveNoFileComp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
33
cli/command/service/completion.go
Normal file
33
cli/command/service/completion.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/docker/cli/cli/command/completion"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// completeServiceNames offers completion for swarm service names and optional IDs.
|
||||||
|
// By default, only names are returned.
|
||||||
|
// Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs.
|
||||||
|
func completeServiceNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
|
||||||
|
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
|
||||||
|
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes"
|
||||||
|
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
list, err := dockerCLI.Client().ServiceList(cmd.Context(), types.ServiceListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveError
|
||||||
|
}
|
||||||
|
|
||||||
|
names := make([]string, 0, len(list))
|
||||||
|
for _, service := range list {
|
||||||
|
if showIDs {
|
||||||
|
names = append(names, service.Spec.Name, service.ID)
|
||||||
|
} else {
|
||||||
|
names = append(names, service.Spec.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return names, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
}
|
@ -41,9 +41,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
}
|
}
|
||||||
return runInspect(cmd.Context(), dockerCli, opts)
|
return runInspect(cmd.Context(), dockerCli, opts)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: completeServiceNames(dockerCli),
|
||||||
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -51,10 +51,8 @@ func newLogsCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
opts.target = args[0]
|
opts.target = args[0]
|
||||||
return runLogs(cmd.Context(), dockerCli, &opts)
|
return runLogs(cmd.Context(), dockerCli, &opts)
|
||||||
},
|
},
|
||||||
Annotations: map[string]string{"version": "1.29"},
|
Annotations: map[string]string{"version": "1.29"},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: completeServiceNames(dockerCli),
|
||||||
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -39,9 +39,7 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
options.services = args
|
options.services = args
|
||||||
return runPS(cmd.Context(), dockerCli, options)
|
return runPS(cmd.Context(), dockerCli, options)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: completeServiceNames(dockerCli),
|
||||||
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
|
||||||
|
@ -19,9 +19,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runRemove(cmd.Context(), dockerCli, args)
|
return runRemove(cmd.Context(), dockerCli, args)
|
||||||
},
|
},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: completeServiceNames(dockerCli),
|
||||||
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
cmd.Flags()
|
cmd.Flags()
|
||||||
|
|
||||||
|
@ -23,10 +23,8 @@ func newRollbackCommand(dockerCli command.Cli) *cobra.Command {
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runRollback(cmd.Context(), dockerCli, options, args[0])
|
return runRollback(cmd.Context(), dockerCli, options, args[0])
|
||||||
},
|
},
|
||||||
Annotations: map[string]string{"version": "1.31"},
|
Annotations: map[string]string{"version": "1.31"},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: completeServiceNames(dockerCli),
|
||||||
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
@ -121,7 +121,7 @@ func runServiceScale(ctx context.Context, apiClient client.ServiceAPIClient, ser
|
|||||||
func completeScaleArgs(dockerCli command.Cli) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
func completeScaleArgs(dockerCli command.Cli) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
// reuse the existing logic for configurable completion of service names and IDs.
|
// reuse the existing logic for configurable completion of service names and IDs.
|
||||||
completions, directive := CompletionFn(dockerCli)(cmd, args, toComplete)
|
completions, directive := completeServiceNames(dockerCli)(cmd, args, toComplete)
|
||||||
if directive == cobra.ShellCompDirectiveError {
|
if directive == cobra.ShellCompDirectiveError {
|
||||||
return completions, directive
|
return completions, directive
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,7 @@ func newUpdateCommand(dockerCLI command.Cli) *cobra.Command {
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runUpdate(cmd.Context(), dockerCLI, cmd.Flags(), options, args[0])
|
return runUpdate(cmd.Context(), dockerCLI, cmd.Flags(), options, args[0])
|
||||||
},
|
},
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: completeServiceNames(dockerCLI),
|
||||||
return CompletionFn(dockerCLI)(cmd, args, toComplete)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user