mirror of
https://github.com/docker/compose.git
synced 2025-04-08 17:05:13 +02:00
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 <milas.bowman@docker.com>
96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
/*
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
|
|
dockercli "github.com/docker/cli/cli"
|
|
"github.com/docker/cli/cli-plugins/manager"
|
|
"github.com/docker/cli/cli-plugins/plugin"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/docker/compose/v2/cmd/cmdtrace"
|
|
"github.com/docker/docker/client"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/docker/compose/v2/cmd/compatibility"
|
|
commands "github.com/docker/compose/v2/cmd/compose"
|
|
"github.com/docker/compose/v2/internal"
|
|
"github.com/docker/compose/v2/pkg/compose"
|
|
)
|
|
|
|
func pluginMain() {
|
|
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
|
|
backend := compose.NewComposeService(dockerCli)
|
|
cmd := commands.RootCommand(dockerCli, backend)
|
|
originalPreRun := cmd.PersistentPreRunE
|
|
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
|
|
// initialize the dockerCli instance
|
|
if err := plugin.PersistentPreRunE(cmd, args); err != nil {
|
|
return err
|
|
}
|
|
// compose-specific initialization
|
|
dockerCliPostInitialize(dockerCli)
|
|
|
|
// TODO(milas): add an env var to enable logging from the
|
|
// OTel components for debugging purposes
|
|
_ = cmdtrace.Setup(cmd, dockerCli, os.Args[1:])
|
|
|
|
if originalPreRun != nil {
|
|
return originalPreRun(cmd, args)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
|
|
return dockercli.StatusError{
|
|
StatusCode: compose.CommandSyntaxFailure.ExitCode,
|
|
Status: err.Error(),
|
|
}
|
|
})
|
|
return cmd
|
|
},
|
|
manager.Metadata{
|
|
SchemaVersion: "0.1.0",
|
|
Vendor: "Docker Inc.",
|
|
Version: internal.Version,
|
|
})
|
|
}
|
|
|
|
// 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:])...)
|
|
}
|
|
pluginMain()
|
|
}
|