From 9752fa5500d443b5cac283fe4cb71ff994f54921 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 24 Sep 2025 12:49:05 +0200 Subject: [PATCH] pluginMain: remove uses of DockerCLI.Apply The Apply method was added when CLI options for constructing the CLI were rewritten into functional options in [cli@7f207f3]. There was no mention in the pull request of this method specifically, and we want to remove or reduce functions that mutate the CLI configuration after initialization if possible (and likely remove the `Apply` method). This patch removes the use of the `Apply` function as an intermediate step; improvements will be made in the CLI itself for a more solid implementation. [cli@7f207f3]: https://github.com/docker/cli/commit/7f207f3f957ed3f5129aeb22bef2a429c14caf22 Signed-off-by: Sebastiaan van Stijn --- cmd/main.go | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 3c3e2290e..53bbc09d6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -47,8 +47,15 @@ func pluginMain() { if err := plugin.PersistentPreRunE(cmd, args); err != nil { return err } - // compose-specific initialization - dockerCliPostInitialize(dockerCli) + // compose-specific initialization. This must be called AFTER + // plugin.PersistentPreRunE. + // + // FIXME(milas): remove once https://github.com/docker/cli/pull/4574 is merged; for now, + // set it in a rather roundabout way by grabbing the underlying + // concrete client and manually invoking an option on it + if mobyClient, ok := dockerCli.Client().(*client.Client); ok { + _ = client.WithUserAgent("compose/" + internal.Version)(mobyClient) + } if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil { logrus.Debugf("failed to enable tracing: %v", err) @@ -75,22 +82,6 @@ func pluginMain() { }) } -// dockerCliPostInitialize performs Compose-specific configuration for the -// command.Cli instance provided by the plugin.Run() initialization. -// -// NOTE: This must be called AFTER plugin.PersistentPreRunE. -func dockerCliPostInitialize(dockerCli command.Cli) { - // HACK(milas): remove once docker/cli#4574 is merged; for now, - // set it in a rather roundabout way by grabbing the underlying - // concrete client and manually invoking an option on it - _ = dockerCli.Apply(func(cli *command.DockerCli) error { - if mobyClient, ok := cli.Client().(*client.Client); ok { - _ = client.WithUserAgent("compose/" + internal.Version)(mobyClient) - } - return nil - }) -} - func main() { if plugin.RunningStandalone() { os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)