From 824079038189c06989f5928abda51b69054b3683 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 19 Dec 2020 20:01:15 -0500 Subject: [PATCH 1/4] fix: correct argument handling when setting the `--app` flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When setting the `--app` flag, there was a special case for the config plugin. This plugin used to take the `--no-restart` flag as the first argument - and still did until this change - resulting in an invalid call to the plugin subcommands. What is worse is that this functionality carried over to all other plugins as they were rewritten in golang, and thus the issue spread without us knowing. The fix is to support GNU flag parsing rules, which Golang does not do by default (Because Of Reasons™). Switching to `github.com/spf13/pflag` fixes this issue, and also allows positional arguments to be placed wherever, even in the middle of flags. Closes #4255 --- dokku | 7 +--- plugins/apps/go.mod | 1 + plugins/apps/go.sum | 2 + plugins/apps/src/subcommands/subcommands.go | 3 +- plugins/buildpacks/go.mod | 1 + plugins/buildpacks/go.sum | 2 + .../buildpacks/src/subcommands/subcommands.go | 3 +- plugins/config/go.mod | 1 + plugins/config/go.sum | 2 + plugins/config/src/subcommands/subcommands.go | 3 +- plugins/logs/go.mod | 1 + plugins/logs/go.sum | 2 + plugins/logs/src/subcommands/subcommands.go | 3 +- plugins/network/go.mod | 1 + plugins/network/go.sum | 2 + .../network/src/subcommands/subcommands.go | 3 +- plugins/proxy/go.mod | 1 + plugins/proxy/go.sum | 2 + plugins/proxy/src/subcommands/subcommands.go | 3 +- plugins/ps/go.mod | 1 + plugins/ps/go.sum | 2 + plugins/ps/src/subcommands/subcommands.go | 3 +- plugins/repo/go.mod | 1 + plugins/repo/go.sum | 2 + plugins/repo/src/subcommands/subcommands.go | 3 +- plugins/resource/go.mod | 3 +- plugins/resource/go.sum | 2 + .../resource/src/subcommands/subcommands.go | 3 +- tests/unit/client.bats | 40 +++++++++++++++++++ 29 files changed, 87 insertions(+), 16 deletions(-) diff --git a/dokku b/dokku index 51d2fced2..5e10bbb97 100755 --- a/dokku +++ b/dokku @@ -144,12 +144,7 @@ execute_dokku_cmd() { if [[ "$(readlink -f "$PLUGIN_ENABLED_PATH/${PLUGIN_NAME%%:*}")" == *core-plugins* ]]; then [[ ${argv[0]} == "$PLUGIN_CMD" ]] && shift 1 if [[ -n $DOKKU_APP_NAME ]]; then - if [[ "$PLUGIN_CMD" == config* ]] && [[ ${argv[1]} == "--no-restart" ]]; then - shift 1 - set -- "--no-restart" "$DOKKU_APP_NAME" "$@" - else - set -- "$DOKKU_APP_NAME" "$@" - fi + set -- "$DOKKU_APP_NAME" "$@" fi set -- "$PLUGIN_CMD" "$@" fi diff --git a/plugins/apps/go.mod b/plugins/apps/go.mod index c43805370..45d84a255 100644 --- a/plugins/apps/go.mod +++ b/plugins/apps/go.mod @@ -6,6 +6,7 @@ require ( github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 // indirect github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 // indirect + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/apps/go.sum b/plugins/apps/go.sum index f05068f8d..8d94a86e4 100644 --- a/plugins/apps/go.sum +++ b/plugins/apps/go.sum @@ -4,3 +4,5 @@ github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 h1:HHUr4P/aKh4qu github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/apps/src/subcommands/subcommands.go b/plugins/apps/src/subcommands/subcommands.go index 218031a12..208d9eed2 100644 --- a/plugins/apps/src/subcommands/subcommands.go +++ b/plugins/apps/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/apps" "github.com/dokku/dokku/plugins/common" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/plugins/buildpacks/go.mod b/plugins/buildpacks/go.mod index b77ee8b6f..d37f10d5c 100644 --- a/plugins/buildpacks/go.mod +++ b/plugins/buildpacks/go.mod @@ -6,6 +6,7 @@ require ( github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 // indirect github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/buildpacks/go.sum b/plugins/buildpacks/go.sum index f05068f8d..8d94a86e4 100644 --- a/plugins/buildpacks/go.sum +++ b/plugins/buildpacks/go.sum @@ -4,3 +4,5 @@ github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 h1:HHUr4P/aKh4qu github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/buildpacks/src/subcommands/subcommands.go b/plugins/buildpacks/src/subcommands/subcommands.go index fe5e46179..dc9743d0c 100644 --- a/plugins/buildpacks/src/subcommands/subcommands.go +++ b/plugins/buildpacks/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/buildpacks" "github.com/dokku/dokku/plugins/common" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/plugins/config/go.mod b/plugins/config/go.mod index 2e777c4f2..1b9ee9e55 100644 --- a/plugins/config/go.mod +++ b/plugins/config/go.mod @@ -7,6 +7,7 @@ require ( github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/joho/godotenv v1.2.0 github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/config/go.sum b/plugins/config/go.sum index 8966db493..bb951f71d 100644 --- a/plugins/config/go.sum +++ b/plugins/config/go.sum @@ -6,3 +6,5 @@ github.com/joho/godotenv v1.2.0 h1:vGTvz69FzUFp+X4/bAkb0j5BoLC+9bpqTWY8mjhA9pc= github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/config/src/subcommands/subcommands.go b/plugins/config/src/subcommands/subcommands.go index 2d7cdb04a..0ad64a674 100644 --- a/plugins/config/src/subcommands/subcommands.go +++ b/plugins/config/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/config" + + flag "github.com/spf13/pflag" ) func getKeys(args []string, global bool) []string { diff --git a/plugins/logs/go.mod b/plugins/logs/go.mod index 9c7166d02..7d6a5255d 100644 --- a/plugins/logs/go.mod +++ b/plugins/logs/go.mod @@ -6,6 +6,7 @@ require ( github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 // indirect + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/logs/go.sum b/plugins/logs/go.sum index f05068f8d..8d94a86e4 100644 --- a/plugins/logs/go.sum +++ b/plugins/logs/go.sum @@ -4,3 +4,5 @@ github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 h1:HHUr4P/aKh4qu github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/logs/src/subcommands/subcommands.go b/plugins/logs/src/subcommands/subcommands.go index 1e438bda5..9d09b974a 100644 --- a/plugins/logs/src/subcommands/subcommands.go +++ b/plugins/logs/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/logs" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/plugins/network/go.mod b/plugins/network/go.mod index c683b16ff..9208e00ad 100644 --- a/plugins/network/go.mod +++ b/plugins/network/go.mod @@ -7,6 +7,7 @@ require ( github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/dokku/dokku/plugins/config v0.0.0-00010101000000-000000000000 + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/network/go.sum b/plugins/network/go.sum index 8966db493..bb951f71d 100644 --- a/plugins/network/go.sum +++ b/plugins/network/go.sum @@ -6,3 +6,5 @@ github.com/joho/godotenv v1.2.0 h1:vGTvz69FzUFp+X4/bAkb0j5BoLC+9bpqTWY8mjhA9pc= github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/network/src/subcommands/subcommands.go b/plugins/network/src/subcommands/subcommands.go index cb9ac377e..88f3040ab 100644 --- a/plugins/network/src/subcommands/subcommands.go +++ b/plugins/network/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/network" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/plugins/proxy/go.mod b/plugins/proxy/go.mod index 165889ac8..55a0e008b 100644 --- a/plugins/proxy/go.mod +++ b/plugins/proxy/go.mod @@ -7,6 +7,7 @@ require ( github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/dokku/dokku/plugins/config v0.0.0-00010101000000-000000000000 github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/proxy/go.sum b/plugins/proxy/go.sum index 8966db493..bb951f71d 100644 --- a/plugins/proxy/go.sum +++ b/plugins/proxy/go.sum @@ -6,3 +6,5 @@ github.com/joho/godotenv v1.2.0 h1:vGTvz69FzUFp+X4/bAkb0j5BoLC+9bpqTWY8mjhA9pc= github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/proxy/src/subcommands/subcommands.go b/plugins/proxy/src/subcommands/subcommands.go index fa320b175..77a0dfb70 100644 --- a/plugins/proxy/src/subcommands/subcommands.go +++ b/plugins/proxy/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/proxy" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/plugins/ps/go.mod b/plugins/ps/go.mod index 466666a27..7db5a8ce9 100644 --- a/plugins/ps/go.mod +++ b/plugins/ps/go.mod @@ -9,6 +9,7 @@ require ( github.com/dokku/dokku/plugins/docker-options v0.0.0-00010101000000-000000000000 github.com/gofrs/flock v0.8.0 github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/ps/go.sum b/plugins/ps/go.sum index b44b50726..e991213a1 100644 --- a/plugins/ps/go.sum +++ b/plugins/ps/go.sum @@ -10,3 +10,5 @@ github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqx github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/ps/src/subcommands/subcommands.go b/plugins/ps/src/subcommands/subcommands.go index 6eec6e0ae..887843d8d 100644 --- a/plugins/ps/src/subcommands/subcommands.go +++ b/plugins/ps/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/ps" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/plugins/repo/go.mod b/plugins/repo/go.mod index 3588d0833..bffe0f1c5 100644 --- a/plugins/repo/go.mod +++ b/plugins/repo/go.mod @@ -6,6 +6,7 @@ require ( github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 // indirect github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/dokku/dokku/plugins/config v0.0.0-00010101000000-000000000000 + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/repo/go.sum b/plugins/repo/go.sum index 8966db493..bb951f71d 100644 --- a/plugins/repo/go.sum +++ b/plugins/repo/go.sum @@ -6,3 +6,5 @@ github.com/joho/godotenv v1.2.0 h1:vGTvz69FzUFp+X4/bAkb0j5BoLC+9bpqTWY8mjhA9pc= github.com/joho/godotenv v1.2.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/repo/src/subcommands/subcommands.go b/plugins/repo/src/subcommands/subcommands.go index 3ef36c88c..9e46455d7 100644 --- a/plugins/repo/src/subcommands/subcommands.go +++ b/plugins/repo/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/repo" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/plugins/resource/go.mod b/plugins/resource/go.mod index ec4d3930c..8b8594909 100644 --- a/plugins/resource/go.mod +++ b/plugins/resource/go.mod @@ -4,8 +4,9 @@ go 1.14 require ( github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 // indirect - github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 + github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000 github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 + github.com/spf13/pflag v1.0.5 // indirect ) replace github.com/dokku/dokku/plugins/common => ../common diff --git a/plugins/resource/go.sum b/plugins/resource/go.sum index f05068f8d..8d94a86e4 100644 --- a/plugins/resource/go.sum +++ b/plugins/resource/go.sum @@ -4,3 +4,5 @@ github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 h1:HHUr4P/aKh4qu github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3 h1:utdYOikI1XjNtTFGCwSM6OmFJblU4ld4gACoJsbadJg= github.com/ryanuber/columnize v1.1.2-0.20190319233515-9e6335e58db3/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= diff --git a/plugins/resource/src/subcommands/subcommands.go b/plugins/resource/src/subcommands/subcommands.go index b6fbf99ea..7a060cc23 100644 --- a/plugins/resource/src/subcommands/subcommands.go +++ b/plugins/resource/src/subcommands/subcommands.go @@ -1,13 +1,14 @@ package main import ( - "flag" "fmt" "os" "strings" "github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/resource" + + flag "github.com/spf13/pflag" ) // main entrypoint to all subcommands diff --git a/tests/unit/client.bats b/tests/unit/client.bats index f5906f201..6af86a629 100644 --- a/tests/unit/client.bats +++ b/tests/unit/client.bats @@ -29,6 +29,46 @@ teardown() { assert_success } +@test "(client) arg parsing" { + run /bin/bash -c "dokku config:set --global GLOBAL_KEY=VALUE" + echo "output: $output" + echo "status: $status" + + run /bin/bash -c "dokku config:set $TEST_APP GLOBAL_KEY=VALUE" + echo "output: $output" + echo "status: $status" + + run /bin/bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh config:export --merged --format shell" + echo "output: $output" + echo "status: $status" + assert_success + + common_output="$output" + run /bin/bash -c "dokku config:export $TEST_APP --merged --format shell" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "$common_output" + + run /bin/bash -c "dokku config:export --merged $TEST_APP --format shell" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "$common_output" + + run /bin/bash -c "dokku config:export --merged --format shell $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "$common_output" + + run /bin/bash -c "dokku --app $TEST_APP config:export --merged --format shell" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "$common_output" +} + @test "(client) apps:create AND apps:destroy with random name" { setup_client_repo run /bin/bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh apps:create" From 1439e8b30b48d1dc52cf6ab192d3227ea04ef833 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 21 Dec 2020 01:36:59 -0500 Subject: [PATCH 2/4] hack: parse out the info flags for report subcommands separately pflag doesn't ignore unspecified arguments, so they must be parsed out separately first. --- plugins/apps/src/subcommands/subcommands.go | 10 ++++++---- plugins/apps/subcommands.go | 6 ------ .../buildpacks/src/subcommands/subcommands.go | 10 ++++++---- plugins/buildpacks/subcommands.go | 6 ------ plugins/common/common.go | 20 +++++++++++++++++++ .../network/src/subcommands/subcommands.go | 10 ++++++---- plugins/network/subcommands.go | 5 ----- plugins/proxy/src/subcommands/subcommands.go | 10 ++++++---- plugins/proxy/subcommands.go | 5 ----- plugins/ps/src/subcommands/subcommands.go | 10 ++++++---- plugins/ps/subcommands.go | 6 ------ .../resource/src/subcommands/subcommands.go | 10 ++++++---- plugins/resource/subcommands.go | 6 ------ 13 files changed, 56 insertions(+), 58 deletions(-) diff --git a/plugins/apps/src/subcommands/subcommands.go b/plugins/apps/src/subcommands/subcommands.go index 208d9eed2..0f146428e 100644 --- a/plugins/apps/src/subcommands/subcommands.go +++ b/plugins/apps/src/subcommands/subcommands.go @@ -65,10 +65,12 @@ func main() { err = apps.CommandRename(oldAppName, newAppName, *skipDeploy) case "report": args := flag.NewFlagSet("apps:report", flag.ExitOnError) - args.Parse(os.Args[2:]) - appName := args.Arg(0) - infoFlag := args.Arg(1) - err = apps.CommandReport(appName, infoFlag) + osArgs, infoFlag, err := common.ParseReportArgs("apps", os.Args[2:]) + if err == nil { + args.Parse(osArgs) + appName := args.Arg(0) + err = apps.CommandReport(appName, infoFlag) + } case "unlock": args := flag.NewFlagSet("apps:unlock", flag.ExitOnError) args.Parse(os.Args[2:]) diff --git a/plugins/apps/subcommands.go b/plugins/apps/subcommands.go index 3cf15f173..2c73a234b 100644 --- a/plugins/apps/subcommands.go +++ b/plugins/apps/subcommands.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "os" - "strings" "github.com/dokku/dokku/plugins/common" ) @@ -197,11 +196,6 @@ func CommandRename(oldAppName string, newAppName string, skipDeploy bool) error // CommandReport displays an app report for one or more apps func CommandReport(appName string, infoFlag string) error { - if strings.HasPrefix(appName, "--") { - infoFlag = appName - appName = "" - } - if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { diff --git a/plugins/buildpacks/src/subcommands/subcommands.go b/plugins/buildpacks/src/subcommands/subcommands.go index dc9743d0c..375b8794e 100644 --- a/plugins/buildpacks/src/subcommands/subcommands.go +++ b/plugins/buildpacks/src/subcommands/subcommands.go @@ -44,10 +44,12 @@ func main() { err = buildpacks.CommandRemove(appName, buildpack, *index) case "report": args := flag.NewFlagSet("buildpacks:report", flag.ExitOnError) - args.Parse(os.Args[2:]) - appName := args.Arg(0) - infoFlag := args.Arg(1) - err = buildpacks.CommandReport(appName, infoFlag) + osArgs, infoFlag, err := common.ParseReportArgs("buildpacks", os.Args[2:]) + if err == nil { + args.Parse(osArgs) + appName := args.Arg(0) + err = buildpacks.CommandReport(appName, infoFlag) + } case "set": args := flag.NewFlagSet("buildpacks:set", flag.ExitOnError) index := args.Int("index", 0, "--index: the 1-based index of the URL in the list of URLs") diff --git a/plugins/buildpacks/subcommands.go b/plugins/buildpacks/subcommands.go index 53a97c963..539f01a62 100644 --- a/plugins/buildpacks/subcommands.go +++ b/plugins/buildpacks/subcommands.go @@ -3,7 +3,6 @@ package buildpacks import ( "errors" "fmt" - "strings" "github.com/dokku/dokku/plugins/common" ) @@ -106,11 +105,6 @@ func CommandRemove(appName string, buildpack string, index int) (err error) { // CommandReport displays a buildpacks report for one or more apps func CommandReport(appName string, infoFlag string) error { - if strings.HasPrefix(appName, "--") { - infoFlag = appName - appName = "" - } - if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { diff --git a/plugins/common/common.go b/plugins/common/common.go index a4636e950..5ed135f23 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -286,6 +286,26 @@ func GetenvWithDefault(key string, defaultValue string) (val string) { return } +func ParseReportArgs(pluginName string, arguments []string) ([]string, string, error) { + osArgs := []string{} + infoFlags := []string{} + for _, argument := range arguments { + if strings.HasPrefix(argument, "--") { + infoFlags = append(infoFlags, argument) + } else { + osArgs = append(osArgs, argument) + } + } + + if len(infoFlags) == 0 { + return osArgs, "", nil + } + if len(infoFlags) == 1 { + return osArgs, infoFlags[0], nil + } + return osArgs, "", fmt.Errorf("%s:report command allows only a single flag", pluginName) +} + // ReportSingleApp is an internal function that displays a report for an app func ReportSingleApp(reportType string, appName string, infoFlag string, infoFlags map[string]string, trimPrefix bool, uppercaseFirstCharacter bool) error { flags := []string{} diff --git a/plugins/network/src/subcommands/subcommands.go b/plugins/network/src/subcommands/subcommands.go index 88f3040ab..b5f1a5cc7 100644 --- a/plugins/network/src/subcommands/subcommands.go +++ b/plugins/network/src/subcommands/subcommands.go @@ -53,10 +53,12 @@ func main() { err = network.CommandRebuildall() case "report": args := flag.NewFlagSet("network:report", flag.ExitOnError) - args.Parse(os.Args[2:]) - appName := args.Arg(0) - infoFlag := args.Arg(1) - err = network.CommandReport(appName, infoFlag) + osArgs, infoFlag, err := common.ParseReportArgs("network", os.Args[2:]) + if err == nil { + args.Parse(osArgs) + appName := args.Arg(0) + err = network.CommandReport(appName, infoFlag) + } case "set": args := flag.NewFlagSet("network:set", flag.ExitOnError) args.Parse(os.Args[2:]) diff --git a/plugins/network/subcommands.go b/plugins/network/subcommands.go index 7253d7f7b..a4db4cc93 100644 --- a/plugins/network/subcommands.go +++ b/plugins/network/subcommands.go @@ -124,11 +124,6 @@ func CommandRebuildall() error { // CommandReport displays a network report for one or more apps func CommandReport(appName string, infoFlag string) error { - if strings.HasPrefix(appName, "--") { - infoFlag = appName - appName = "" - } - if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { diff --git a/plugins/proxy/src/subcommands/subcommands.go b/plugins/proxy/src/subcommands/subcommands.go index 77a0dfb70..7765b0375 100644 --- a/plugins/proxy/src/subcommands/subcommands.go +++ b/plugins/proxy/src/subcommands/subcommands.go @@ -64,10 +64,12 @@ func main() { err = proxy.CommandPortsSet(appName, portMaps) case "report": args := flag.NewFlagSet("proxy:report", flag.ExitOnError) - args.Parse(os.Args[2:]) - appName := args.Arg(0) - infoFlag := args.Arg(1) - err = proxy.CommandReport(appName, infoFlag) + osArgs, infoFlag, err := common.ParseReportArgs("proxy", os.Args[2:]) + if err == nil { + args.Parse(osArgs) + appName := args.Arg(0) + err = proxy.CommandReport(appName, infoFlag) + } case "set": args := flag.NewFlagSet("proxy:set", flag.ExitOnError) args.Parse(os.Args[2:]) diff --git a/plugins/proxy/subcommands.go b/plugins/proxy/subcommands.go index 9db5cfd24..dcb37425c 100644 --- a/plugins/proxy/subcommands.go +++ b/plugins/proxy/subcommands.go @@ -155,11 +155,6 @@ func CommandPortsSet(appName string, portMaps []string) error { // CommandReport displays a proxy report for one or more apps func CommandReport(appName string, infoFlag string) error { - if strings.HasPrefix(appName, "--") { - infoFlag = appName - appName = "" - } - if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { diff --git a/plugins/ps/src/subcommands/subcommands.go b/plugins/ps/src/subcommands/subcommands.go index 887843d8d..d19c51173 100644 --- a/plugins/ps/src/subcommands/subcommands.go +++ b/plugins/ps/src/subcommands/subcommands.go @@ -32,10 +32,12 @@ func main() { err = ps.CommandRebuild(appName, *allApps, *parallelCount) case "report": args := flag.NewFlagSet("ps:report", flag.ExitOnError) - args.Parse(os.Args[2:]) - appName := args.Arg(0) - infoFlag := args.Arg(1) - err = ps.CommandReport(appName, infoFlag) + osArgs, infoFlag, err := common.ParseReportArgs("ps", os.Args[2:]) + if err == nil { + args.Parse(osArgs) + appName := args.Arg(0) + err = ps.CommandReport(appName, infoFlag) + } case "restart": args := flag.NewFlagSet("ps:restart", flag.ExitOnError) allApps := args.Bool("all", false, "--all: restart all apps") diff --git a/plugins/ps/subcommands.go b/plugins/ps/subcommands.go index 49a61b616..2cc6bd131 100644 --- a/plugins/ps/subcommands.go +++ b/plugins/ps/subcommands.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "path/filepath" - "strings" dockeroptions "github.com/dokku/dokku/plugins/docker-options" @@ -45,11 +44,6 @@ func CommandRebuild(appName string, allApps bool, parallelCount int) error { // CommandReport displays a ps report for one or more apps func CommandReport(appName string, infoFlag string) error { - if strings.HasPrefix(appName, "--") { - infoFlag = appName - appName = "" - } - if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { diff --git a/plugins/resource/src/subcommands/subcommands.go b/plugins/resource/src/subcommands/subcommands.go index 7a060cc23..c0808d06b 100644 --- a/plugins/resource/src/subcommands/subcommands.go +++ b/plugins/resource/src/subcommands/subcommands.go @@ -50,10 +50,12 @@ func main() { err = resource.CommandLimitClear(appName, *processType) case "report": args := flag.NewFlagSet("resource:report", flag.ExitOnError) - args.Parse(os.Args[2:]) - appName := args.Arg(0) - infoFlag := args.Arg(1) - err = resource.CommandReport(appName, infoFlag) + osArgs, infoFlag, err := common.ParseReportArgs("resource", os.Args[2:]) + if err == nil { + args.Parse(osArgs) + appName := args.Arg(0) + err = resource.CommandReport(appName, infoFlag) + } case "reserve": args := flag.NewFlagSet("resource:reserve", flag.ExitOnError) processType := args.String("process-type", "", "process-type: A process type to manage") diff --git a/plugins/resource/subcommands.go b/plugins/resource/subcommands.go index 32cc9e055..8dc67fc5c 100644 --- a/plugins/resource/subcommands.go +++ b/plugins/resource/subcommands.go @@ -2,7 +2,6 @@ package resource import ( "errors" - "strings" "github.com/dokku/dokku/plugins/common" ) @@ -28,11 +27,6 @@ func CommandLimitClear(appName string, processType string) error { // CommandReport displays a resource report for one or more apps func CommandReport(appName string, infoFlag string) error { - if strings.HasPrefix(appName, "--") { - infoFlag = appName - appName = "" - } - if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { From dd1962f92de1c9a83f245ff9e2b381a30cbac2b4 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 21 Dec 2020 01:40:40 -0500 Subject: [PATCH 3/4] fix: add missing docblock --- plugins/common/common.go | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/common/common.go b/plugins/common/common.go index 5ed135f23..5ef60c4ed 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -286,6 +286,7 @@ func GetenvWithDefault(key string, defaultValue string) (val string) { return } +// ParseReportArgs splits out flags from non-flags for input into report commands func ParseReportArgs(pluginName string, arguments []string) ([]string, string, error) { osArgs := []string{} infoFlags := []string{} From 2e4c886ab2f431b14e53be17251e9129ab786ec1 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 21 Dec 2020 13:36:04 -0500 Subject: [PATCH 4/4] fix: correct app arg parsing and new test --- contrib/dokku_client.sh | 11 +++++++---- tests/unit/client.bats | 6 ++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/contrib/dokku_client.sh b/contrib/dokku_client.sh index a10c73a0c..a377ae38f 100755 --- a/contrib/dokku_client.sh +++ b/contrib/dokku_client.sh @@ -72,6 +72,9 @@ main() { skip=false continue fi + is_flag=false + + [[ "$arg" =~ ^--.* ]] && is_flag=true if [[ "$arg" == "--app" ]]; then APP=${args[$next_index]} @@ -82,10 +85,10 @@ main() { skip=true shift 2 elif [[ "$arg" =~ ^--.* ]]; then - [[ "$cmd_set" == "true" ]] && APP_ARG="$arg" && break + [[ "$cmd_set" == "true" ]] && [[ "$is_flag" == "false" ]] && APP_ARG="$arg" && break [[ "$arg" == "--trace" ]] && export DOKKU_TRACE=1 && set -x else - if [[ "$cmd_set" == "true" ]]; then + if [[ "$cmd_set" == "true" ]] && [[ "$is_flag" == "false" ]]; then APP_ARG="$arg" break else @@ -110,7 +113,7 @@ main() { case "$CMD" in apps:create) - if [[ -z "$APP_ARG" ]]; then + if [[ -z "$APP" ]] && [[ -z "$APP_ARG" ]]; then APP=$(fn-random-name) counter=0 while ssh -p "$DOKKU_PORT" "dokku@$DOKKU_REMOTE_HOST" apps 2>/dev/null | grep -q "$APP"; do @@ -123,7 +126,7 @@ main() { counter=$((counter + 1)) fi done - else + elif [[ -z "$APP" ]]; then APP="$APP_ARG" fi if git remote add "$DOKKU_GIT_REMOTE" "dokku@$DOKKU_REMOTE_HOST:$APP"; then diff --git a/tests/unit/client.bats b/tests/unit/client.bats index 6af86a629..208beb6d8 100644 --- a/tests/unit/client.bats +++ b/tests/unit/client.bats @@ -33,12 +33,14 @@ teardown() { run /bin/bash -c "dokku config:set --global GLOBAL_KEY=VALUE" echo "output: $output" echo "status: $status" + assert_success - run /bin/bash -c "dokku config:set $TEST_APP GLOBAL_KEY=VALUE" + run /bin/bash -c "dokku config:set $TEST_APP APP_KEY=VALUE" echo "output: $output" echo "status: $status" + assert_success - run /bin/bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh config:export --merged --format shell" + run /bin/bash -c "${BATS_TEST_DIRNAME}/../../contrib/dokku_client.sh --app $TEST_APP config:export --merged --format shell" echo "output: $output" echo "status: $status" assert_success