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 { 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..dd3c947336 100644 --- a/cli/command/cli_options.go +++ b/cli/command/cli_options.go @@ -114,6 +114,18 @@ 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 { + c, err := makeClient(cli) + if err != nil { + return err + } + return WithAPIClient(c)(cli) + } +} + // 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