From 1faa76ee4e6f9ceb1505624396fe7b95ed7e7e16 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 30 Sep 2021 09:07:19 +0200 Subject: [PATCH] use compose-switch Convert Signed-off-by: Nicolas De Loof --- cmd/compose/compose.go | 9 ++------- cmd/main.go | 24 ++---------------------- go.mod | 1 + go.sum | 3 +++ 4 files changed, 8 insertions(+), 29 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index aa9d178d6..f76ae7aca 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -223,14 +223,9 @@ func RootCommand(backend api.Service) *cobra.Command { noAnsi bool verbose bool ) - commandName := pluginName - if RunningAsStandalone() { - commandName = os.Args[0] - } - command := &cobra.Command{ Short: "Docker Compose", - Use: commandName, + Use: pluginName, TraverseChildren: true, // By default (no Run/RunE in parent command) for typos in subcommands, cobra displays the help of parent command but exit(0) ! RunE: func(cmd *cobra.Command, args []string) error { @@ -245,7 +240,7 @@ func RootCommand(backend api.Service) *cobra.Command { }, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { parent := cmd.Root() - if parent != nil && parent.Name() != commandName { + if parent != nil { parentPrerun := parent.PersistentPreRunE if parentPrerun != nil { err := parentPrerun(cmd, args) diff --git a/cmd/main.go b/cmd/main.go index 35be9873f..16ca5aaf3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,16 +17,15 @@ package main import ( - "fmt" "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-switch/redirect" "github.com/spf13/cobra" - cliFlags "github.com/docker/cli/cli/flags" commands "github.com/docker/compose/v2/cmd/compose" "github.com/docker/compose/v2/internal" "github.com/docker/compose/v2/pkg/api" @@ -38,25 +37,6 @@ func init() { "To provide feedback or request new features please open issues at https://github.com/docker/compose" } -func standaloneMain() int { - dockerCli, err := command.NewDockerCli() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - return 1 - } - opts := cliFlags.NewClientOptions() - err = dockerCli.Initialize(opts) - if err != nil { - return 1 - } - lazyInit := api.NewServiceProxy().WithService(compose.NewComposeService(dockerCli.Client(), dockerCli.ConfigFile())) - rootCmd := commands.RootCommand(lazyInit) - if err := rootCmd.Execute(); err != nil { - return 1 - } - return 0 -} - func pluginMain() { plugin.Run(func(dockerCli command.Cli) *cobra.Command { lazyInit := api.NewServiceProxy() @@ -89,7 +69,7 @@ func pluginMain() { func main() { if commands.RunningAsStandalone() { - os.Exit(standaloneMain()) + os.Args = append([]string{"docker"}, redirect.Convert(os.Args[1:])...) } pluginMain() } diff --git a/go.mod b/go.mod index 1dce6a2b2..15a908a5b 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/docker/buildx v0.5.2-0.20210422185057-908a856079fc github.com/docker/cli v20.10.7+incompatible github.com/docker/cli-docs-tool v0.1.1 + github.com/docker/compose-switch v1.0.2 github.com/docker/docker v20.10.7+incompatible github.com/docker/go-connections v0.4.0 github.com/docker/go-units v0.4.0 diff --git a/go.sum b/go.sum index 231332496..6f8da62e0 100644 --- a/go.sum +++ b/go.sum @@ -351,6 +351,8 @@ github.com/docker/cli v20.10.7+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHv github.com/docker/cli-docs-tool v0.1.1 h1:c6vuTMvogCkSFQCXIr6Mb4gFgUpdZ+28YMbCBfaQLik= github.com/docker/cli-docs-tool v0.1.1/go.mod h1:oMzPNt1wC3TcxuY22GMnOODNOxkwGH51gV3AhqAjFQ4= github.com/docker/compose-on-kubernetes v0.4.19-0.20190128150448-356b2919c496/go.mod h1:iT2pYfi580XlpaV4KmK0T6+4/9+XoKmk/fhoDod1emE= +github.com/docker/compose-switch v1.0.2 h1:chXFNNcnRvmtQYzwTaVsv/KSLRt8riSRAiSav89mLfk= +github.com/docker/compose-switch v1.0.2/go.mod h1:uyPj8S3oH1O9rSZ5QVozw28OIjdNIflSSYElC2P0plQ= github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY= github.com/docker/distribution v2.6.0-rc.1.0.20180327202408-83389a148052+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/distribution v2.7.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= @@ -960,6 +962,7 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3 github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI= +github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo= github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw= github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=