Skip to content

Commit

Permalink
cmd/entrypoint: do not interpret anything after --
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vdemeester committed Jul 6, 2022
1 parent 6876520 commit 4e7e445
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4e7e445

Please sign in to comment.