forked from docker/compose
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <ulyssessouza@gmail.com>
- Loading branch information
1 parent
19b9fdf
commit e2f33af
Showing
6 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/docker/compose/v2/cmd/compose" | ||
) | ||
|
||
func getBoolFlags() []string { | ||
return []string{ | ||
"--debug", "-D", | ||
"--verbose", | ||
"--tls", | ||
"--tlsverify", | ||
} | ||
} | ||
|
||
func getStringFlags() []string { | ||
return []string{ | ||
"--tlscacert", | ||
"--tlscert", | ||
"--tlskey", | ||
"--host", "-H", | ||
"--context", | ||
"--log-level", | ||
} | ||
} | ||
|
||
func convert(args []string) []string { | ||
var rootFlags []string | ||
command := []string{compose.PluginName} | ||
l := len(args) | ||
for i := 0; i < l; i++ { | ||
arg := args[i] | ||
if arg[0] != '-' { | ||
// not a top-level flag anymore, keep the rest of the command unmodified | ||
if arg == compose.PluginName { | ||
i++ | ||
} | ||
command = append(command, args[i:]...) | ||
break | ||
} | ||
if arg == "--verbose" { | ||
arg = "--debug" | ||
} | ||
if arg == "-h" { | ||
// docker cli has deprecated -h to avoid ambiguity with -H, while docker-compose still support it | ||
arg = "--help" | ||
} | ||
if arg == "--version" || arg == "-v" { | ||
// redirect --version pseudo-command to actual command | ||
arg = "version" | ||
} | ||
if contains(getBoolFlags(), arg) { | ||
rootFlags = append(rootFlags, arg) | ||
continue | ||
} | ||
if contains(getStringFlags(), arg) { | ||
i++ | ||
if i >= l { | ||
fmt.Fprintf(os.Stderr, "flag needs an argument: '%s'\n", arg) | ||
os.Exit(1) | ||
} | ||
rootFlags = append(rootFlags, arg, args[i]) | ||
continue | ||
} | ||
command = append(command, arg) | ||
} | ||
return append(rootFlags, command...) | ||
} | ||
|
||
func contains(array []string, needle string) bool { | ||
for _, val := range array { | ||
if val == needle { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func Test_convert(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
want []string | ||
}{ | ||
{ | ||
name: "compose only", | ||
args: []string{"up"}, | ||
want: []string{"compose", "up"}, | ||
}, | ||
{ | ||
name: "with context", | ||
args: []string{"--context", "foo", "-f", "compose.yaml", "up"}, | ||
want: []string{"--context", "foo", "compose", "-f", "compose.yaml", "up"}, | ||
}, | ||
{ | ||
name: "with host", | ||
args: []string{"--host", "tcp://1.2.3.4", "up"}, | ||
want: []string{"--host", "tcp://1.2.3.4", "compose", "up"}, | ||
}, | ||
{ | ||
name: "compose --version", | ||
args: []string{"--version"}, | ||
want: []string{"compose", "version"}, | ||
}, | ||
{ | ||
name: "help", | ||
args: []string{"-h"}, | ||
want: []string{"compose", "--help"}, | ||
}, | ||
{ | ||
name: "issues/1962", | ||
args: []string{"psql", "-h", "postgres"}, | ||
want: []string{"compose", "psql", "-h", "postgres"}, // -h should not be converted to --help | ||
}, | ||
{ | ||
name: "issues/8648", | ||
args: []string{"exec", "mongo", "mongo", "--host", "mongo"}, | ||
want: []string{"compose", "exec", "mongo", "mongo", "--host", "mongo"}, // --host is passed to exec | ||
}, | ||
{ | ||
name: "issues/12", | ||
args: []string{"--log-level", "INFO", "up"}, | ||
want: []string{"--log-level", "INFO", "compose", "up"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := convert(tt.args) | ||
assert.DeepEqual(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters