Skip to content

Commit d1857de

Browse files
authored
Merge pull request #6081 from thaJeztah/unify_cli_opts
cli/command, cli-plugins/plugin: some cleanups in WithInitializeClient, withPluginClientConn
2 parents 8e5fb5b + 240b069 commit d1857de

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

cli-plugins/plugin/plugin.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func Run(makeCmd func(command.Cli) *cobra.Command, meta metadata.Metadata) {
109109
}
110110

111111
func withPluginClientConn(name string) command.CLIOption {
112-
return command.WithInitializeClient(func(dockerCli *command.DockerCli) (client.APIClient, error) {
112+
return func(cli *command.DockerCli) error {
113113
cmd := "docker"
114114
if x := os.Getenv(metadata.ReexecEnvvar); x != "" {
115115
cmd = x
@@ -133,11 +133,14 @@ func withPluginClientConn(name string) command.CLIOption {
133133

134134
helper, err := connhelper.GetCommandConnectionHelper(cmd, flags...)
135135
if err != nil {
136-
return nil, err
136+
return err
137137
}
138-
139-
return client.NewClientWithOpts(client.WithDialContext(helper.Dialer))
140-
})
138+
apiClient, err := client.NewClientWithOpts(client.WithDialContext(helper.Dialer))
139+
if err != nil {
140+
return err
141+
}
142+
return command.WithAPIClient(apiClient)(cli)
143+
}
141144
}
142145

143146
func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta metadata.Metadata) *cli.TopLevelCommand {

cli/command/cli.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,6 @@ func (cli *DockerCli) HooksEnabled() bool {
222222
return false
223223
}
224224

225-
// WithInitializeClient is passed to DockerCli.Initialize by callers who wish to set a particular API Client for use by the CLI.
226-
func WithInitializeClient(makeClient func(dockerCli *DockerCli) (client.APIClient, error)) CLIOption {
227-
return func(dockerCli *DockerCli) error {
228-
var err error
229-
dockerCli.client, err = makeClient(dockerCli)
230-
return err
231-
}
232-
}
233-
234225
// Initialize the dockerCli runs initialization that must happen after command
235226
// line flags are parsed.
236227
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption) error {

cli/command/cli_options.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ func WithAPIClient(c client.APIClient) CLIOption {
114114
}
115115
}
116116

117+
// WithInitializeClient is passed to [DockerCli.Initialize] to initialize
118+
// an API Client for use by the CLI.
119+
func WithInitializeClient(makeClient func(*DockerCli) (client.APIClient, error)) CLIOption {
120+
return func(cli *DockerCli) error {
121+
c, err := makeClient(cli)
122+
if err != nil {
123+
return err
124+
}
125+
return WithAPIClient(c)(cli)
126+
}
127+
}
128+
117129
// envOverrideHTTPHeaders is the name of the environment-variable that can be
118130
// used to set custom HTTP headers to be sent by the client. This environment
119131
// variable is the equivalent to the HttpHeaders field in the configuration

0 commit comments

Comments
 (0)