From 40f0dbd971009463c25c580ad9d509f6bbb7e949 Mon Sep 17 00:00:00 2001 From: ThedosiouTh Date: Sat, 28 May 2022 11:20:23 +0300 Subject: [PATCH 1/4] TC: Use map to simplify flag conversion and avoid multilple if statements Signed-off-by: ThedosiouTh --- cmd/compatibility/convert.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/compatibility/convert.go b/cmd/compatibility/convert.go index 27a57063b..dc8f8f2e6 100644 --- a/cmd/compatibility/convert.go +++ b/cmd/compatibility/convert.go @@ -43,6 +43,15 @@ func getStringFlags() []string { } } +var argToActualFlag = map[string]string{ + "--verbose": "--debug", + // docker cli has deprecated -h to avoid ambiguity with -H, while docker-compose still support it + "-h": "--help", + // redirect --version pseudo-command to actual command + "--version": "version", + "-v": "version", +} + // Convert transforms standalone docker-compose args into CLI plugin compliant ones func Convert(args []string) []string { var rootFlags []string @@ -58,16 +67,8 @@ func Convert(args []string) []string { 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 actualFlag, ok := argToActualFlag[arg]; ok { + arg = actualFlag } if contains(getBoolFlags(), arg) { rootFlags = append(rootFlags, arg) From 9542bdf44547ab3b0c5c764ecafbe7d8272ed219 Mon Sep 17 00:00:00 2001 From: ThedosiouTh Date: Sat, 28 May 2022 11:39:48 +0300 Subject: [PATCH 2/4] TC: fix naming Signed-off-by: ThedosiouTh --- cmd/compatibility/convert.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/compatibility/convert.go b/cmd/compatibility/convert.go index dc8f8f2e6..c92a6fdae 100644 --- a/cmd/compatibility/convert.go +++ b/cmd/compatibility/convert.go @@ -43,7 +43,7 @@ func getStringFlags() []string { } } -var argToActualFlag = map[string]string{ +var argToActualArgument = map[string]string{ "--verbose": "--debug", // docker cli has deprecated -h to avoid ambiguity with -H, while docker-compose still support it "-h": "--help", @@ -67,8 +67,8 @@ func Convert(args []string) []string { command = append(command, args[i:]...) break } - if actualFlag, ok := argToActualFlag[arg]; ok { - arg = actualFlag + if actualArgument, ok := argToActualArgument[arg]; ok { + arg = actualArgument } if contains(getBoolFlags(), arg) { rootFlags = append(rootFlags, arg) From 8648f303519560b0e97ede6c839be8834acefa3a Mon Sep 17 00:00:00 2001 From: ThedosiouTh Date: Sat, 28 May 2022 11:48:04 +0300 Subject: [PATCH 3/4] TC: Add missing tests for argumetns Signed-off-by: ThedosiouTh --- cmd/compatibility/convert_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd/compatibility/convert_test.go b/cmd/compatibility/convert_test.go index 68fc66de7..1de3e58dd 100644 --- a/cmd/compatibility/convert_test.go +++ b/cmd/compatibility/convert_test.go @@ -43,11 +43,21 @@ func Test_convert(t *testing.T) { args: []string{"--host", "tcp://1.2.3.4", "up"}, want: []string{"--host", "tcp://1.2.3.4", "compose", "up"}, }, + { + name: "compose --verbose", + args: []string{"--verbose"}, + want: []string{"--debug", "compose"}, + }, { name: "compose --version", args: []string{"--version"}, want: []string{"compose", "version"}, }, + { + name: "compose -v", + args: []string{"-v"}, + want: []string{"compose", "version"}, + }, { name: "help", args: []string{"-h"}, From 115ac6d5296789c0d5d9bf387d1e516f5af3cf53 Mon Sep 17 00:00:00 2001 From: ThedosiouTh Date: Thu, 30 Jun 2022 18:49:28 +0300 Subject: [PATCH 4/4] Use switch/case instead of static map for simplicity Signed-off-by: ThedosiouTh --- cmd/compatibility/convert.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/compatibility/convert.go b/cmd/compatibility/convert.go index c92a6fdae..c9fbf7df2 100644 --- a/cmd/compatibility/convert.go +++ b/cmd/compatibility/convert.go @@ -43,15 +43,6 @@ func getStringFlags() []string { } } -var argToActualArgument = map[string]string{ - "--verbose": "--debug", - // docker cli has deprecated -h to avoid ambiguity with -H, while docker-compose still support it - "-h": "--help", - // redirect --version pseudo-command to actual command - "--version": "version", - "-v": "version", -} - // Convert transforms standalone docker-compose args into CLI plugin compliant ones func Convert(args []string) []string { var rootFlags []string @@ -67,9 +58,18 @@ func Convert(args []string) []string { command = append(command, args[i:]...) break } - if actualArgument, ok := argToActualArgument[arg]; ok { - arg = actualArgument + + switch arg { + case "--verbose": + arg = "--debug" + case "-h": + // docker cli has deprecated -h to avoid ambiguity with -H, while docker-compose still support it + arg = "--help" + case "--version", "-v": + // redirect --version pseudo-command to actual command + arg = "version" } + if contains(getBoolFlags(), arg) { rootFlags = append(rootFlags, arg) continue