From e339a64261426a74bef8c619d3185f84ca816f01 Mon Sep 17 00:00:00 2001 From: Peter Byfield Date: Thu, 25 Nov 2021 14:03:31 +0100 Subject: [PATCH] Fix quoting of CLI_ARGS Consider a task: test: cmds: - pytest {{.CLI_ARGS}} Running `task test -- -m "not foo"` should be equivalent to running `pytest -m "not foo"`. However, with the current implementation the quoting of CLI_ARGS is lost and the task executes `python -m not foo`, which results in an error. --- cmd/task/task.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index b481efe5..0f9f9654 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -12,6 +12,7 @@ import ( "syscall" "github.com/spf13/pflag" + "mvdan.cc/sh/v3/syntax" "github.com/go-task/task/v3" "github.com/go-task/task/v3/args" @@ -159,18 +160,22 @@ func main() { } var ( - calls []taskfile.Call - globals *taskfile.Vars - tasksAndVars, cliArgs = getArgs() + calls []taskfile.Call + globals *taskfile.Vars ) + tasksAndVars, cliArgs, err := getArgs() + if err != nil { + log.Fatal(err) + } + if v >= 3.0 { calls, globals = args.ParseV3(tasksAndVars...) } else { calls, globals = args.ParseV2(tasksAndVars...) } - globals.Set("CLI_ARGS", taskfile.Var{Static: strings.Join(cliArgs, " ")}) + globals.Set("CLI_ARGS", taskfile.Var{Static: cliArgs}) e.Taskfile.Vars.Merge(globals) ctx := context.Background() @@ -191,20 +196,25 @@ func main() { } } -func getArgs() (tasksAndVars, cliArgs []string) { +func getArgs() ([]string, string, error) { var ( args = pflag.Args() doubleDashPos = pflag.CommandLine.ArgsLenAtDash() ) - if doubleDashPos != -1 { - tasksAndVars = args[:doubleDashPos] - cliArgs = args[doubleDashPos:] - } else { - tasksAndVars = args + if doubleDashPos == -1 { + return args, "", nil } - return + quotedCliArgs := []string{} + for _, arg := range args[doubleDashPos:] { + quotedCliArg, err := syntax.Quote(arg, syntax.LangBash) + if err != nil { + return []string{}, "", err + } + quotedCliArgs = append(quotedCliArgs, quotedCliArg) + } + return args[:doubleDashPos], strings.Join(quotedCliArgs, " "), nil } func getSignalContext() context.Context {