Skip to content

Commit 242422b

Browse files
committed
cli/command/service: un-export CompletionFn
It's only used internally, and has no external consumers. Un-export it, rename it to something more descriptive, and move it to a separate file to align with other packages. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 3fe40e5 commit 242422b

9 files changed

Lines changed: 42 additions & 49 deletions

File tree

cli/command/service/cmd.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package service
22

33
import (
4-
"os"
5-
64
"github.com/docker/cli/cli"
75
"github.com/docker/cli/cli/command"
8-
"github.com/docker/cli/cli/command/completion"
9-
"github.com/docker/docker/api/types"
106
"github.com/spf13/cobra"
117
)
128

@@ -35,27 +31,3 @@ func NewServiceCommand(dockerCli command.Cli) *cobra.Command {
3531
)
3632
return cmd
3733
}
38-
39-
// CompletionFn offers completion for swarm service names and optional IDs.
40-
// By default, only names are returned.
41-
// Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs.
42-
func CompletionFn(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
43-
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
44-
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes"
45-
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
46-
list, err := dockerCLI.Client().ServiceList(cmd.Context(), types.ServiceListOptions{})
47-
if err != nil {
48-
return nil, cobra.ShellCompDirectiveError
49-
}
50-
51-
names := make([]string, 0, len(list))
52-
for _, service := range list {
53-
if showIDs {
54-
names = append(names, service.Spec.Name, service.ID)
55-
} else {
56-
names = append(names, service.Spec.Name)
57-
}
58-
}
59-
return names, cobra.ShellCompDirectiveNoFileComp
60-
}
61-
}

cli/command/service/completion.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package service
2+
3+
import (
4+
"os"
5+
6+
"github.com/docker/cli/cli/command/completion"
7+
"github.com/docker/docker/api/types"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// completeServiceNames offers completion for swarm service names and optional IDs.
12+
// By default, only names are returned.
13+
// Set DOCKER_COMPLETION_SHOW_SERVICE_IDS=yes to also complete IDs.
14+
func completeServiceNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
15+
// https://github.com/docker/cli/blob/f9ced58158d5e0b358052432244b483774a1983d/contrib/completion/bash/docker#L41-L43
16+
showIDs := os.Getenv("DOCKER_COMPLETION_SHOW_SERVICE_IDS") == "yes"
17+
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
18+
list, err := dockerCLI.Client().ServiceList(cmd.Context(), types.ServiceListOptions{})
19+
if err != nil {
20+
return nil, cobra.ShellCompDirectiveError
21+
}
22+
23+
names := make([]string, 0, len(list))
24+
for _, service := range list {
25+
if showIDs {
26+
names = append(names, service.Spec.Name, service.ID)
27+
} else {
28+
names = append(names, service.Spec.Name)
29+
}
30+
}
31+
return names, cobra.ShellCompDirectiveNoFileComp
32+
}
33+
}

cli/command/service/inspect.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
4141
}
4242
return runInspect(cmd.Context(), dockerCli, opts)
4343
},
44-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
45-
return CompletionFn(dockerCli)(cmd, args, toComplete)
46-
},
44+
ValidArgsFunction: completeServiceNames(dockerCli),
4745
}
4846

4947
flags := cmd.Flags()

cli/command/service/logs.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ func newLogsCommand(dockerCli command.Cli) *cobra.Command {
5151
opts.target = args[0]
5252
return runLogs(cmd.Context(), dockerCli, &opts)
5353
},
54-
Annotations: map[string]string{"version": "1.29"},
55-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
56-
return CompletionFn(dockerCli)(cmd, args, toComplete)
57-
},
54+
Annotations: map[string]string{"version": "1.29"},
55+
ValidArgsFunction: completeServiceNames(dockerCli),
5856
}
5957

6058
flags := cmd.Flags()

cli/command/service/ps.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
3939
options.services = args
4040
return runPS(cmd.Context(), dockerCli, options)
4141
},
42-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
43-
return CompletionFn(dockerCli)(cmd, args, toComplete)
44-
},
42+
ValidArgsFunction: completeServiceNames(dockerCli),
4543
}
4644
flags := cmd.Flags()
4745
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")

cli/command/service/remove.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
1919
RunE: func(cmd *cobra.Command, args []string) error {
2020
return runRemove(cmd.Context(), dockerCli, args)
2121
},
22-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
23-
return CompletionFn(dockerCli)(cmd, args, toComplete)
24-
},
22+
ValidArgsFunction: completeServiceNames(dockerCli),
2523
}
2624
cmd.Flags()
2725

cli/command/service/rollback.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ func newRollbackCommand(dockerCli command.Cli) *cobra.Command {
2323
RunE: func(cmd *cobra.Command, args []string) error {
2424
return runRollback(cmd.Context(), dockerCli, options, args[0])
2525
},
26-
Annotations: map[string]string{"version": "1.31"},
27-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
28-
return CompletionFn(dockerCli)(cmd, args, toComplete)
29-
},
26+
Annotations: map[string]string{"version": "1.31"},
27+
ValidArgsFunction: completeServiceNames(dockerCli),
3028
}
3129

3230
flags := cmd.Flags()

cli/command/service/scale.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func runServiceScale(ctx context.Context, apiClient client.ServiceAPIClient, ser
121121
func completeScaleArgs(dockerCli command.Cli) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
122122
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
123123
// reuse the existing logic for configurable completion of service names and IDs.
124-
completions, directive := CompletionFn(dockerCli)(cmd, args, toComplete)
124+
completions, directive := completeServiceNames(dockerCli)(cmd, args, toComplete)
125125
if directive == cobra.ShellCompDirectiveError {
126126
return completions, directive
127127
}

cli/command/service/update.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ func newUpdateCommand(dockerCLI command.Cli) *cobra.Command {
3535
RunE: func(cmd *cobra.Command, args []string) error {
3636
return runUpdate(cmd.Context(), dockerCLI, cmd.Flags(), options, args[0])
3737
},
38-
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
39-
return CompletionFn(dockerCLI)(cmd, args, toComplete)
40-
},
38+
ValidArgsFunction: completeServiceNames(dockerCLI),
4139
}
4240

4341
flags := cmd.Flags()

0 commit comments

Comments
 (0)