main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "os"
  16. dockercli "github.com/docker/cli/cli"
  17. "github.com/docker/cli/cli-plugins/manager"
  18. "github.com/docker/cli/cli-plugins/plugin"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/docker/compose/v2/cmd/cmdtrace"
  21. "github.com/docker/docker/client"
  22. "github.com/sirupsen/logrus"
  23. "github.com/spf13/cobra"
  24. "github.com/docker/compose/v2/cmd/compatibility"
  25. commands "github.com/docker/compose/v2/cmd/compose"
  26. "github.com/docker/compose/v2/internal"
  27. "github.com/docker/compose/v2/pkg/compose"
  28. )
  29. func pluginMain() {
  30. plugin.Run(func(dockerCli command.Cli) *cobra.Command {
  31. backend := compose.NewComposeService(dockerCli)
  32. cmd := commands.RootCommand(dockerCli, backend)
  33. originalPreRunE := cmd.PersistentPreRunE
  34. cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
  35. // initialize the dockerCli instance
  36. if err := plugin.PersistentPreRunE(cmd, args); err != nil {
  37. return err
  38. }
  39. // compose-specific initialization
  40. dockerCliPostInitialize(dockerCli)
  41. if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
  42. logrus.Debugf("failed to enable tracing: %v", err)
  43. }
  44. if originalPreRunE != nil {
  45. return originalPreRunE(cmd, args)
  46. }
  47. return nil
  48. }
  49. cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
  50. return dockercli.StatusError{
  51. StatusCode: compose.CommandSyntaxFailure.ExitCode,
  52. Status: err.Error(),
  53. }
  54. })
  55. return cmd
  56. },
  57. manager.Metadata{
  58. SchemaVersion: "0.1.0",
  59. Vendor: "Docker Inc.",
  60. Version: internal.Version,
  61. })
  62. }
  63. // dockerCliPostInitialize performs Compose-specific configuration for the
  64. // command.Cli instance provided by the plugin.Run() initialization.
  65. //
  66. // NOTE: This must be called AFTER plugin.PersistentPreRunE.
  67. func dockerCliPostInitialize(dockerCli command.Cli) {
  68. // HACK(milas): remove once docker/cli#4574 is merged; for now,
  69. // set it in a rather roundabout way by grabbing the underlying
  70. // concrete client and manually invoking an option on it
  71. _ = dockerCli.Apply(func(cli *command.DockerCli) error {
  72. if mobyClient, ok := cli.Client().(*client.Client); ok {
  73. _ = client.WithUserAgent("compose/" + internal.Version)(mobyClient)
  74. }
  75. return nil
  76. })
  77. }
  78. func main() {
  79. if plugin.RunningStandalone() {
  80. os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
  81. }
  82. pluginMain()
  83. }