From 0d82ff4ae1e40dd158427e6598fccf89fea20062 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 16 May 2025 23:20:18 +0200 Subject: [PATCH 1/3] cli/command: move WithInitializeClient to other options Signed-off-by: Sebastiaan van Stijn --- cli/command/cli.go | 9 --------- cli/command/cli_options.go | 10 ++++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index 7b2bb63b58..b78525bc82 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -222,15 +222,6 @@ func (cli *DockerCli) HooksEnabled() bool { return false } -// WithInitializeClient is passed to DockerCli.Initialize by callers who wish to set a particular API Client for use by the CLI. -func WithInitializeClient(makeClient func(dockerCli *DockerCli) (client.APIClient, error)) CLIOption { - return func(dockerCli *DockerCli) error { - var err error - dockerCli.client, err = makeClient(dockerCli) - return err - } -} - // Initialize the dockerCli runs initialization that must happen after command // line flags are parsed. func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption) error { diff --git a/cli/command/cli_options.go b/cli/command/cli_options.go index 5d0a8ee3a6..0ea56533b1 100644 --- a/cli/command/cli_options.go +++ b/cli/command/cli_options.go @@ -114,6 +114,16 @@ func WithAPIClient(c client.APIClient) CLIOption { } } +// WithInitializeClient is passed to [DockerCli.Initialize] to initialize +// an API Client for use by the CLI. +func WithInitializeClient(makeClient func(*DockerCli) (client.APIClient, error)) CLIOption { + return func(cli *DockerCli) error { + var err error + cli.client, err = makeClient(cli) + return err + } +} + // envOverrideHTTPHeaders is the name of the environment-variable that can be // used to set custom HTTP headers to be sent by the client. This environment // variable is the equivalent to the HttpHeaders field in the configuration From 7b1f889074d0f478562053fe3870dddf557b3eac Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 16 May 2025 23:31:53 +0200 Subject: [PATCH 2/3] cli/command: make WithInitializeClient a wrapper for WithAPIClient Signed-off-by: Sebastiaan van Stijn --- cli/command/cli_options.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cli/command/cli_options.go b/cli/command/cli_options.go index 0ea56533b1..dd3c947336 100644 --- a/cli/command/cli_options.go +++ b/cli/command/cli_options.go @@ -118,9 +118,11 @@ func WithAPIClient(c client.APIClient) CLIOption { // an API Client for use by the CLI. func WithInitializeClient(makeClient func(*DockerCli) (client.APIClient, error)) CLIOption { return func(cli *DockerCli) error { - var err error - cli.client, err = makeClient(cli) - return err + c, err := makeClient(cli) + if err != nil { + return err + } + return WithAPIClient(c)(cli) } } From 240b06991b52f259aa1a9a9ba5389037e00dae79 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 2 Apr 2025 14:05:36 +0200 Subject: [PATCH 3/3] cli-plugins/plugin: rewrite withPluginClientConn w/ WithAPIClient The WithInitializeClient looks redundant altogether, so let's rewrite this function to not depend on it. Signed-off-by: Sebastiaan van Stijn --- cli-plugins/plugin/plugin.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cli-plugins/plugin/plugin.go b/cli-plugins/plugin/plugin.go index 08bdf73614..39d6d86943 100644 --- a/cli-plugins/plugin/plugin.go +++ b/cli-plugins/plugin/plugin.go @@ -109,7 +109,7 @@ func Run(makeCmd func(command.Cli) *cobra.Command, meta metadata.Metadata) { } func withPluginClientConn(name string) command.CLIOption { - return command.WithInitializeClient(func(dockerCli *command.DockerCli) (client.APIClient, error) { + return func(cli *command.DockerCli) error { cmd := "docker" if x := os.Getenv(metadata.ReexecEnvvar); x != "" { cmd = x @@ -133,11 +133,14 @@ func withPluginClientConn(name string) command.CLIOption { helper, err := connhelper.GetCommandConnectionHelper(cmd, flags...) if err != nil { - return nil, err + return err } - - return client.NewClientWithOpts(client.WithDialContext(helper.Dialer)) - }) + apiClient, err := client.NewClientWithOpts(client.WithDialContext(helper.Dialer)) + if err != nil { + return err + } + return command.WithAPIClient(apiClient)(cli) + } } func newPluginCommand(dockerCli *command.DockerCli, plugin *cobra.Command, meta metadata.Metadata) *cli.TopLevelCommand {