main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // TODO(milas): this cast is safe but we should not need to do this,
  32. // we should expose the concrete service type so that we do not need
  33. // to rely on the `api.Service` interface internally
  34. backend := compose.NewComposeService(dockerCli).(commands.Backend)
  35. cmd := commands.RootCommand(dockerCli, backend)
  36. originalPreRunE := cmd.PersistentPreRunE
  37. cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
  38. // initialize the dockerCli instance
  39. if err := plugin.PersistentPreRunE(cmd, args); err != nil {
  40. return err
  41. }
  42. // compose-specific initialization
  43. dockerCliPostInitialize(dockerCli)
  44. if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
  45. logrus.Debugf("failed to enable tracing: %v", err)
  46. }
  47. if originalPreRunE != nil {
  48. return originalPreRunE(cmd, args)
  49. }
  50. return nil
  51. }
  52. cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
  53. return dockercli.StatusError{
  54. StatusCode: compose.CommandSyntaxFailure.ExitCode,
  55. Status: err.Error(),
  56. }
  57. })
  58. return cmd
  59. },
  60. manager.Metadata{
  61. SchemaVersion: "0.1.0",
  62. Vendor: "Docker Inc.",
  63. Version: internal.Version,
  64. })
  65. }
  66. // dockerCliPostInitialize performs Compose-specific configuration for the
  67. // command.Cli instance provided by the plugin.Run() initialization.
  68. //
  69. // NOTE: This must be called AFTER plugin.PersistentPreRunE.
  70. func dockerCliPostInitialize(dockerCli command.Cli) {
  71. // HACK(milas): remove once docker/cli#4574 is merged; for now,
  72. // set it in a rather roundabout way by grabbing the underlying
  73. // concrete client and manually invoking an option on it
  74. _ = dockerCli.Apply(func(cli *command.DockerCli) error {
  75. if mobyClient, ok := cli.Client().(*client.Client); ok {
  76. _ = client.WithUserAgent("compose/" + internal.Version)(mobyClient)
  77. }
  78. return nil
  79. })
  80. }
  81. func main() {
  82. if plugin.RunningStandalone() {
  83. os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
  84. }
  85. pluginMain()
  86. }