Skip to content

Commit e2f33af

Browse files
ulyssessouzandeloof
authored andcommitted
Merge and fix Convert function from docker/compose-switch
This also removes the vendoring of the repo Note that it fixes the problem of double "compose" on calling the compose plugin with "docker" root flags. Like in "docker --context default compose version" Signed-off-by: Ulysses Souza <[email protected]>
1 parent 19b9fdf commit e2f33af

File tree

6 files changed

+178
-9
lines changed

6 files changed

+178
-9
lines changed

cmd/compose/compose.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ func (o *projectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.Proj
212212
cli.WithName(o.ProjectName))...)
213213
}
214214

215-
const pluginName = "compose"
215+
const PluginName = "compose"
216216

217217
// RunningAsStandalone detects when running as a standalone program
218218
func RunningAsStandalone() bool {
219-
return len(os.Args) < 2 || os.Args[1] != manager.MetadataSubcommandName && os.Args[1] != pluginName
219+
return len(os.Args) < 2 || os.Args[1] != manager.MetadataSubcommandName && os.Args[1] != PluginName
220220
}
221221

222222
// RootCommand returns the compose command with its child commands
@@ -230,7 +230,7 @@ func RootCommand(backend api.Service) *cobra.Command {
230230
)
231231
command := &cobra.Command{
232232
Short: "Docker Compose",
233-
Use: pluginName,
233+
Use: PluginName,
234234
TraverseChildren: true,
235235
// By default (no Run/RunE in parent command) for typos in subcommands, cobra displays the help of parent command but exit(0) !
236236
RunE: func(cmd *cobra.Command, args []string) error {

cmd/convert.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/docker/compose/v2/cmd/compose"
24+
)
25+
26+
func getBoolFlags() []string {
27+
return []string{
28+
"--debug", "-D",
29+
"--verbose",
30+
"--tls",
31+
"--tlsverify",
32+
}
33+
}
34+
35+
func getStringFlags() []string {
36+
return []string{
37+
"--tlscacert",
38+
"--tlscert",
39+
"--tlskey",
40+
"--host", "-H",
41+
"--context",
42+
"--log-level",
43+
}
44+
}
45+
46+
func convert(args []string) []string {
47+
var rootFlags []string
48+
command := []string{compose.PluginName}
49+
l := len(args)
50+
for i := 0; i < l; i++ {
51+
arg := args[i]
52+
if arg[0] != '-' {
53+
// not a top-level flag anymore, keep the rest of the command unmodified
54+
if arg == compose.PluginName {
55+
i++
56+
}
57+
command = append(command, args[i:]...)
58+
break
59+
}
60+
if arg == "--verbose" {
61+
arg = "--debug"
62+
}
63+
if arg == "-h" {
64+
// docker cli has deprecated -h to avoid ambiguity with -H, while docker-compose still support it
65+
arg = "--help"
66+
}
67+
if arg == "--version" || arg == "-v" {
68+
// redirect --version pseudo-command to actual command
69+
arg = "version"
70+
}
71+
if contains(getBoolFlags(), arg) {
72+
rootFlags = append(rootFlags, arg)
73+
continue
74+
}
75+
if contains(getStringFlags(), arg) {
76+
i++
77+
if i >= l {
78+
fmt.Fprintf(os.Stderr, "flag needs an argument: '%s'\n", arg)
79+
os.Exit(1)
80+
}
81+
rootFlags = append(rootFlags, arg, args[i])
82+
continue
83+
}
84+
command = append(command, arg)
85+
}
86+
return append(rootFlags, command...)
87+
}
88+
89+
func contains(array []string, needle string) bool {
90+
for _, val := range array {
91+
if val == needle {
92+
return true
93+
}
94+
}
95+
return false
96+
}

cmd/convert_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"testing"
21+
22+
"gotest.tools/v3/assert"
23+
)
24+
25+
func Test_convert(t *testing.T) {
26+
tests := []struct {
27+
name string
28+
args []string
29+
want []string
30+
}{
31+
{
32+
name: "compose only",
33+
args: []string{"up"},
34+
want: []string{"compose", "up"},
35+
},
36+
{
37+
name: "with context",
38+
args: []string{"--context", "foo", "-f", "compose.yaml", "up"},
39+
want: []string{"--context", "foo", "compose", "-f", "compose.yaml", "up"},
40+
},
41+
{
42+
name: "with host",
43+
args: []string{"--host", "tcp://1.2.3.4", "up"},
44+
want: []string{"--host", "tcp://1.2.3.4", "compose", "up"},
45+
},
46+
{
47+
name: "compose --version",
48+
args: []string{"--version"},
49+
want: []string{"compose", "version"},
50+
},
51+
{
52+
name: "help",
53+
args: []string{"-h"},
54+
want: []string{"compose", "--help"},
55+
},
56+
{
57+
name: "issues/1962",
58+
args: []string{"psql", "-h", "postgres"},
59+
want: []string{"compose", "psql", "-h", "postgres"}, // -h should not be converted to --help
60+
},
61+
{
62+
name: "issues/8648",
63+
args: []string{"exec", "mongo", "mongo", "--host", "mongo"},
64+
want: []string{"compose", "exec", "mongo", "mongo", "--host", "mongo"}, // --host is passed to exec
65+
},
66+
{
67+
name: "issues/12",
68+
args: []string{"--log-level", "INFO", "up"},
69+
want: []string{"--log-level", "INFO", "compose", "up"},
70+
},
71+
}
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
got := convert(tt.args)
75+
assert.DeepEqual(t, tt.want, got)
76+
})
77+
}
78+
}

cmd/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/docker/cli/cli-plugins/manager"
2424
"github.com/docker/cli/cli-plugins/plugin"
2525
"github.com/docker/cli/cli/command"
26-
"github.com/docker/compose-switch/redirect"
2726
"github.com/spf13/cobra"
2827

2928
commands "github.com/docker/compose/v2/cmd/compose"
@@ -69,7 +68,7 @@ func pluginMain() {
6968

7069
func main() {
7170
if commands.RunningAsStandalone() {
72-
os.Args = append([]string{"docker"}, redirect.Convert(os.Args[1:])...)
71+
os.Args = append([]string{"docker"}, convert(os.Args[1:])...)
7372
}
7473
pluginMain()
7574
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/docker/buildx v0.5.2-0.20210422185057-908a856079fc
1414
github.com/docker/cli v20.10.7+incompatible
1515
github.com/docker/cli-docs-tool v0.1.1
16-
github.com/docker/compose-switch v1.0.2
1716
github.com/docker/docker v20.10.7+incompatible
1817
github.com/docker/go-connections v0.4.0
1918
github.com/docker/go-units v0.4.0

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,6 @@ github.com/docker/cli v20.10.7+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHv
353353
github.com/docker/cli-docs-tool v0.1.1 h1:c6vuTMvogCkSFQCXIr6Mb4gFgUpdZ+28YMbCBfaQLik=
354354
github.com/docker/cli-docs-tool v0.1.1/go.mod h1:oMzPNt1wC3TcxuY22GMnOODNOxkwGH51gV3AhqAjFQ4=
355355
github.com/docker/compose-on-kubernetes v0.4.19-0.20190128150448-356b2919c496/go.mod h1:iT2pYfi580XlpaV4KmK0T6+4/9+XoKmk/fhoDod1emE=
356-
github.com/docker/compose-switch v1.0.2 h1:chXFNNcnRvmtQYzwTaVsv/KSLRt8riSRAiSav89mLfk=
357-
github.com/docker/compose-switch v1.0.2/go.mod h1:uyPj8S3oH1O9rSZ5QVozw28OIjdNIflSSYElC2P0plQ=
358356
github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY=
359357
github.com/docker/distribution v2.6.0-rc.1.0.20180327202408-83389a148052+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
360358
github.com/docker/distribution v2.7.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
@@ -967,7 +965,6 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3
967965
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
968966
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
969967
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
970-
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
971968
github.com/spf13/cobra v1.2.1 h1:+KmjbUw1hriSNMF55oPrkZcb27aECyrj8V2ytv7kWDw=
972969
github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk=
973970
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=

0 commit comments

Comments
 (0)