main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/spf13/cobra"
  22. "github.com/docker/compose/v2/cmd/compatibility"
  23. commands "github.com/docker/compose/v2/cmd/compose"
  24. "github.com/docker/compose/v2/internal"
  25. "github.com/docker/compose/v2/pkg/api"
  26. "github.com/docker/compose/v2/pkg/compose"
  27. )
  28. func pluginMain() {
  29. plugin.Run(func(dockerCli command.Cli) *cobra.Command {
  30. serviceProxy := api.NewServiceProxy().WithService(compose.NewComposeService(dockerCli))
  31. cmd := commands.RootCommand(dockerCli, serviceProxy)
  32. originalPreRun := cmd.PersistentPreRunE
  33. cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
  34. // initialize the dockerCli instance
  35. if err := plugin.PersistentPreRunE(cmd, args); err != nil {
  36. return err
  37. }
  38. // TODO(milas): add an env var to enable logging from the
  39. // OTel components for debugging purposes
  40. _ = cmdtrace.Setup(cmd, dockerCli)
  41. if originalPreRun != nil {
  42. return originalPreRun(cmd, args)
  43. }
  44. return nil
  45. }
  46. cmd.SetFlagErrorFunc(func(c *cobra.Command, err error) error {
  47. return dockercli.StatusError{
  48. StatusCode: compose.CommandSyntaxFailure.ExitCode,
  49. Status: err.Error(),
  50. }
  51. })
  52. return cmd
  53. },
  54. manager.Metadata{
  55. SchemaVersion: "0.1.0",
  56. Vendor: "Docker Inc.",
  57. Version: internal.Version,
  58. })
  59. }
  60. func main() {
  61. if plugin.RunningStandalone() {
  62. os.Args = append([]string{"docker"}, compatibility.Convert(os.Args[1:])...)
  63. }
  64. pluginMain()
  65. }