main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/metadata"
  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. This must be called AFTER
  43. // plugin.PersistentPreRunE.
  44. //
  45. // FIXME(milas): remove once https://github.com/docker/cli/pull/4574 is merged; for now,
  46. // set it in a rather roundabout way by grabbing the underlying
  47. // concrete client and manually invoking an option on it
  48. if mobyClient, ok := dockerCli.Client().(*client.Client); ok {
  49. _ = client.WithUserAgent("compose/" + internal.Version)(mobyClient)
  50. }
  51. if err := cmdtrace.Setup(cmd, dockerCli, os.Args[1:]); err != nil {
  52. logrus.Debugf("failed to enable tracing: %v", err)
  53. }
  54. if originalPreRunE != nil {
  55. return originalPreRunE(cmd, args)
  56. }
  57. return nil
  58. }
  59. cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
  60. return dockercli.StatusError{
  61. StatusCode: 1,
  62. Status: err.Error(),
  63. }
  64. })
  65. return cmd
  66. },
  67. metadata.Metadata{
  68. SchemaVersion: "0.1.0",
  69. Vendor: "Docker Inc.",
  70. Version: internal.Version,
  71. })
  72. }
  73. func main() {
  74. if plugin.RunningStandalone() {
  75. os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
  76. }
  77. pluginMain()
  78. }