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:
34
args/args.go
Normal file
34
args/args.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user