Skip to content

Commit 032e030

Browse files
thaJeztahndeloof
authored andcommitted
cmd: pluginMain: use WithUserAgent option
Rewrite the custom user agent to use the new options that were added in the cli: - plugin.Run now accepts custom CLI options to allow customizing the CLI's - cli/command now has a WithUserAgent option to customize the CLI's user- agent, allowing it to be overridden from the default. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 38ba35e commit 032e030

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

cmd/main.go

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ 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"
2827
"github.com/sirupsen/logrus"
2928
"github.com/spf13/cobra"
3029

@@ -35,51 +34,44 @@ import (
3534
)
3635

3736
func pluginMain() {
38-
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
39-
// TODO(milas): this cast is safe but we should not need to do this,
40-
// we should expose the concrete service type so that we do not need
41-
// to rely on the `api.Service` interface internally
42-
backend := compose.NewComposeService(dockerCli).(commands.Backend)
43-
cmd := commands.RootCommand(dockerCli, backend)
44-
originalPreRunE := cmd.PersistentPreRunE
45-
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
46-
// initialize the dockerCli instance
47-
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
48-
return err
49-
}
50-
// compose-specific initialization. This must be called AFTER
51-
// plugin.PersistentPreRunE.
52-
//
53-
// FIXME(milas): remove once https://github.com/docker/cli/pull/4574 is merged; for now,
54-
// set it in a rather roundabout way by grabbing the underlying
55-
// concrete client and manually invoking an option on it
56-
if mobyClient, ok := dockerCli.Client().(*client.Client); ok {
57-
_ = client.WithUserAgent("compose/" + internal.Version)(mobyClient)
58-
}
37+
plugin.Run(
38+
func(dockerCli command.Cli) *cobra.Command {
39+
// TODO(milas): this cast is safe but we should not need to do this,
40+
// we should expose the concrete service type so that we do not need
41+
// to rely on the `api.Service` interface internally
42+
backend := compose.NewComposeService(dockerCli).(commands.Backend)
43+
cmd := commands.RootCommand(dockerCli, backend)
44+
originalPreRunE := cmd.PersistentPreRunE
45+
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
46+
// initialize the dockerCli instance
47+
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
48+
return err
49+
}
50+
if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
51+
logrus.Debugf("failed to enable tracing: %v", err)
52+
}
5953

60-
if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
61-
logrus.Debugf("failed to enable tracing: %v", err)
54+
if originalPreRunE != nil {
55+
return originalPreRunE(cmd, args)
56+
}
57+
return nil
6258
}
6359

64-
if originalPreRunE != nil {
65-
return originalPreRunE(cmd, args)
66-
}
67-
return nil
68-
}
69-
70-
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
71-
return dockercli.StatusError{
72-
StatusCode: 1,
73-
Status: err.Error(),
74-
}
75-
})
76-
return cmd
77-
},
60+
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
61+
return dockercli.StatusError{
62+
StatusCode: 1,
63+
Status: err.Error(),
64+
}
65+
})
66+
return cmd
67+
},
7868
metadata.Metadata{
7969
SchemaVersion: "0.1.0",
8070
Vendor: "Docker Inc.",
8171
Version: internal.Version,
82-
})
72+
},
73+
command.WithUserAgent("compose/"+internal.Version),
74+
)
8375
}
8476

8577
func main() {

0 commit comments

Comments
 (0)