@@ -3,23 +3,33 @@ package completion
3
3
import (
4
4
"os"
5
5
6
- "github.com/docker/cli/cli/command"
7
6
"github.com/docker/cli/cli/command/formatter"
8
7
"github.com/docker/docker/api/types"
9
8
"github.com/docker/docker/api/types/container"
10
9
"github.com/docker/docker/api/types/image"
11
10
"github.com/docker/docker/api/types/network"
12
11
"github.com/docker/docker/api/types/volume"
12
+ "github.com/docker/docker/client"
13
13
"github.com/spf13/cobra"
14
14
)
15
15
16
16
// ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion
17
17
type ValidArgsFn func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective )
18
18
19
+ // APIClientProvider provides a method to get an [client.APIClient], initializing
20
+ // it if needed.
21
+ //
22
+ // It's a smaller interface than [command.Cli], and used in situations where an
23
+ // APIClient is needed, but we want to postpone initializing the client until
24
+ // it's used.
25
+ type APIClientProvider interface {
26
+ Client () client.APIClient
27
+ }
28
+
19
29
// ImageNames offers completion for images present within the local store
20
- func ImageNames (dockerCli command. Cli ) ValidArgsFn {
30
+ func ImageNames (dockerCLI APIClientProvider ) ValidArgsFn {
21
31
return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
22
- list , err := dockerCli .Client ().ImageList (cmd .Context (), image.ListOptions {})
32
+ list , err := dockerCLI .Client ().ImageList (cmd .Context (), image.ListOptions {})
23
33
if err != nil {
24
34
return nil , cobra .ShellCompDirectiveError
25
35
}
@@ -34,9 +44,9 @@ func ImageNames(dockerCli command.Cli) ValidArgsFn {
34
44
// ContainerNames offers completion for container names and IDs
35
45
// By default, only names are returned.
36
46
// Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs.
37
- func ContainerNames (dockerCli command. Cli , all bool , filters ... func (types.Container ) bool ) ValidArgsFn {
47
+ func ContainerNames (dockerCLI APIClientProvider , all bool , filters ... func (types.Container ) bool ) ValidArgsFn {
38
48
return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
39
- list , err := dockerCli .Client ().ContainerList (cmd .Context (), container.ListOptions {
49
+ list , err := dockerCLI .Client ().ContainerList (cmd .Context (), container.ListOptions {
40
50
All : all ,
41
51
})
42
52
if err != nil {
@@ -67,9 +77,9 @@ func ContainerNames(dockerCli command.Cli, all bool, filters ...func(types.Conta
67
77
}
68
78
69
79
// VolumeNames offers completion for volumes
70
- func VolumeNames (dockerCli command. Cli ) ValidArgsFn {
80
+ func VolumeNames (dockerCLI APIClientProvider ) ValidArgsFn {
71
81
return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
72
- list , err := dockerCli .Client ().VolumeList (cmd .Context (), volume.ListOptions {})
82
+ list , err := dockerCLI .Client ().VolumeList (cmd .Context (), volume.ListOptions {})
73
83
if err != nil {
74
84
return nil , cobra .ShellCompDirectiveError
75
85
}
@@ -82,9 +92,9 @@ func VolumeNames(dockerCli command.Cli) ValidArgsFn {
82
92
}
83
93
84
94
// NetworkNames offers completion for networks
85
- func NetworkNames (dockerCli command. Cli ) ValidArgsFn {
95
+ func NetworkNames (dockerCLI APIClientProvider ) ValidArgsFn {
86
96
return func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
87
- list , err := dockerCli .Client ().NetworkList (cmd .Context (), network.ListOptions {})
97
+ list , err := dockerCLI .Client ().NetworkList (cmd .Context (), network.ListOptions {})
88
98
if err != nil {
89
99
return nil , cobra .ShellCompDirectiveError
90
100
}
0 commit comments