1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

allow assigning variables to tasks at run time via CLI

using a similar syntax than setting env variables to command in bash,
but used right after the task:

```bash
task print MESSAGE=Hello!
```

closes #33
This commit is contained in:
Andrey Nering
2017-09-07 13:57:06 -03:00
parent 5e72de4ba2
commit f3097845b4
8 changed files with 159 additions and 35 deletions

34
args/args.go Normal file
View File

@@ -0,0 +1,34 @@
package args
import (
"errors"
"strings"
"github.com/go-task/task"
)
var (
ErrVariableWithoutTask = errors.New("task: variable given before any task")
)
func Parse(args ...string) ([]task.Call, error) {
var calls []task.Call
for _, arg := range args {
if !strings.Contains(arg, "=") {
calls = append(calls, task.Call{Task: arg})
continue
}
if len(calls) < 1 {
return nil, ErrVariableWithoutTask
}
if calls[len(calls)-1].Vars == nil {
calls[len(calls)-1].Vars = make(task.Vars)
}
pair := strings.SplitN(arg, "=", 2)
calls[len(calls)-1].Vars[pair[0]] = task.Var{Static: pair[1]}
}
return calls, nil
}