mirror of
https://github.com/docker/compose.git
synced 2025-11-11 09:19:56 +01:00
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 <github@gone.nl>
This commit is contained in:
parent
38ba35e165
commit
032e0309ee
72
cmd/main.go
72
cmd/main.go
@ -24,7 +24,6 @@ import (
|
|||||||
"github.com/docker/cli/cli-plugins/plugin"
|
"github.com/docker/cli/cli-plugins/plugin"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/compose/v2/cmd/cmdtrace"
|
"github.com/docker/compose/v2/cmd/cmdtrace"
|
||||||
"github.com/docker/docker/client"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@ -35,51 +34,44 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func pluginMain() {
|
func pluginMain() {
|
||||||
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
|
plugin.Run(
|
||||||
// TODO(milas): this cast is safe but we should not need to do this,
|
func(dockerCli command.Cli) *cobra.Command {
|
||||||
// we should expose the concrete service type so that we do not need
|
// TODO(milas): this cast is safe but we should not need to do this,
|
||||||
// to rely on the `api.Service` interface internally
|
// we should expose the concrete service type so that we do not need
|
||||||
backend := compose.NewComposeService(dockerCli).(commands.Backend)
|
// to rely on the `api.Service` interface internally
|
||||||
cmd := commands.RootCommand(dockerCli, backend)
|
backend := compose.NewComposeService(dockerCli).(commands.Backend)
|
||||||
originalPreRunE := cmd.PersistentPreRunE
|
cmd := commands.RootCommand(dockerCli, backend)
|
||||||
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
originalPreRunE := cmd.PersistentPreRunE
|
||||||
// initialize the dockerCli instance
|
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
||||||
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
|
// initialize the dockerCli instance
|
||||||
return err
|
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
|
||||||
}
|
return err
|
||||||
// compose-specific initialization. This must be called AFTER
|
}
|
||||||
// plugin.PersistentPreRunE.
|
if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
|
||||||
//
|
logrus.Debugf("failed to enable tracing: %v", err)
|
||||||
// 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 originalPreRunE != nil {
|
||||||
if mobyClient, ok := dockerCli.Client().(*client.Client); ok {
|
return originalPreRunE(cmd, args)
|
||||||
_ = client.WithUserAgent("compose/" + internal.Version)(mobyClient)
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
|
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
|
||||||
logrus.Debugf("failed to enable tracing: %v", err)
|
return dockercli.StatusError{
|
||||||
}
|
StatusCode: 1,
|
||||||
|
Status: err.Error(),
|
||||||
if originalPreRunE != nil {
|
}
|
||||||
return originalPreRunE(cmd, args)
|
})
|
||||||
}
|
return cmd
|
||||||
return nil
|
},
|
||||||
}
|
|
||||||
|
|
||||||
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
|
|
||||||
return dockercli.StatusError{
|
|
||||||
StatusCode: 1,
|
|
||||||
Status: err.Error(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return cmd
|
|
||||||
},
|
|
||||||
metadata.Metadata{
|
metadata.Metadata{
|
||||||
SchemaVersion: "0.1.0",
|
SchemaVersion: "0.1.0",
|
||||||
Vendor: "Docker Inc.",
|
Vendor: "Docker Inc.",
|
||||||
Version: internal.Version,
|
Version: internal.Version,
|
||||||
})
|
},
|
||||||
|
command.WithUserAgent("compose/"+internal.Version),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user