Skip to content

Commit b621948

Browse files
committed
feat(cli): report more useful User-Agent on engine API requests
When using the Moby/Docker Engine API client, we do not have a useful user agent value being reported. Ideally, in the future, the Docker CLI will set this appropriately for plugins when it initializes the client. For now, manually set it, which is a bit hacky because it requires some casting & manually invoking an option function that's technically meant for initialization. In practice, this is pretty safe - the cast is checked defensively and we ignore any errors (which shouldn't be possible anyway). Signed-off-by: Milas Bowman <[email protected]>
1 parent 1cfeda7 commit b621948

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cmd/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/docker/cli/cli-plugins/plugin"
2525
"github.com/docker/cli/cli/command"
2626
"github.com/docker/compose/v2/cmd/cmdtrace"
27+
"github.com/docker/docker/client"
2728
"github.com/spf13/cobra"
2829

2930
"github.com/docker/compose/v2/cmd/compatibility"
@@ -42,6 +43,9 @@ func pluginMain() {
4243
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
4344
return err
4445
}
46+
// compose-specific initialization
47+
dockerCliPostInitialize(dockerCli)
48+
4549
// TODO(milas): add an env var to enable logging from the
4650
// OTel components for debugging purposes
4751
_ = cmdtrace.Setup(cmd, dockerCli, os.Args[1:])
@@ -67,6 +71,22 @@ func pluginMain() {
6771
})
6872
}
6973

74+
// dockerCliPostInitialize performs Compose-specific configuration for the
75+
// command.Cli instance provided by the plugin.Run() initialization.
76+
//
77+
// NOTE: This must be called AFTER plugin.PersistentPreRunE.
78+
func dockerCliPostInitialize(dockerCli command.Cli) {
79+
// HACK(milas): remove once docker/cli#4574 is merged; for now,
80+
// set it in a rather roundabout way by grabbing the underlying
81+
// concrete client and manually invoking an option on it
82+
_ = dockerCli.Apply(func(cli *command.DockerCli) error {
83+
if mobyClient, ok := cli.Client().(*client.Client); ok {
84+
_ = client.WithUserAgent("compose/" + internal.Version)(mobyClient)
85+
}
86+
return nil
87+
})
88+
}
89+
7090
func main() {
7191
if plugin.RunningStandalone() {
7292
os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)

0 commit comments

Comments
 (0)