From 4e7e4457cdcd1d3986581c0a500a28323fa5e5e2 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 6 Jul 2022 07:55:25 +0200 Subject: [PATCH] cmd/entrypoint: do not interpret anything after `--` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The way the `flag` package works, it can eat the flag "terminator" (aka the double dash `--`). This means that, in some very specific cases — where the first item after the `--` is also a subcommand, it would execute the entrypoint subcommand instead of the actual command. For example : ``` $ /ko-app/entrypoint -- init a b $ /ko-app/entrypoint init a b ``` This is fixed by making sure we remove anything after `--` for the subcommand processing. And then we pass the rest (after `--`) to the entrypointer to be executed. Signed-off-by: Vincent Demeester --- cmd/entrypoint/main.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/entrypoint/main.go b/cmd/entrypoint/main.go index 5fd6cb67cf5..92100a24a98 100644 --- a/cmd/entrypoint/main.go +++ b/cmd/entrypoint/main.go @@ -78,6 +78,21 @@ func main() { gitcreds.AddFlags(flag.CommandLine) dockercreds.AddFlags(flag.CommandLine) + // Keep only args *before* the "terminator" `--`. + initialArgs := os.Args + newArgs := []string{} + for _, a := range initialArgs { + if a == "--" { + break + } + newArgs = append(newArgs, a) + } + os.Args = newArgs + commandArgs := []string{} + if len(initialArgs) > len(initialArgs) { + commandArgs = initialArgs[len(initialArgs)+1:] + } + flag.Parse() if err := subcommands.Process(flag.Args()); err != nil { @@ -123,7 +138,7 @@ func main() { } e := entrypoint.Entrypointer{ - Command: append(cmd, flag.Args()...), + Command: append(cmd, commandArgs...), WaitFiles: strings.Split(*waitFiles, ","), WaitFileContent: *waitFileContent, PostFile: *postFile,