2017-09-07 18:57:06 +02:00
|
|
|
package args
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2017-09-07 18:57:06 +02:00
|
|
|
)
|
|
|
|
|
2020-08-17 02:26:10 +02:00
|
|
|
// ParseV3 parses command line argument: tasks and global variables
|
|
|
|
func ParseV3(args ...string) ([]taskfile.Call, *taskfile.Vars) {
|
|
|
|
var calls []taskfile.Call
|
2023-03-31 21:13:29 +02:00
|
|
|
globals := &taskfile.Vars{}
|
2020-08-17 02:26:10 +02:00
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
if !strings.Contains(arg, "=") {
|
2023-06-18 03:32:18 +02:00
|
|
|
calls = append(calls, taskfile.Call{Task: arg, Direct: true})
|
2020-08-17 02:26:10 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name, value := splitVar(arg)
|
|
|
|
globals.Set(name, taskfile.Var{Static: value})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(calls) == 0 {
|
2023-06-18 03:32:18 +02:00
|
|
|
calls = append(calls, taskfile.Call{Task: "default", Direct: true})
|
2020-08-17 02:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return calls, globals
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseV2 parses command line argument: tasks and vars of each task
|
|
|
|
func ParseV2(args ...string) ([]taskfile.Call, *taskfile.Vars) {
|
2018-02-17 18:22:18 +02:00
|
|
|
var calls []taskfile.Call
|
2023-03-31 21:13:29 +02:00
|
|
|
globals := &taskfile.Vars{}
|
2017-09-07 18:57:06 +02:00
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
if !strings.Contains(arg, "=") {
|
2023-06-18 03:32:18 +02:00
|
|
|
calls = append(calls, taskfile.Call{Task: arg, Direct: true})
|
2017-09-07 18:57:06 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-05-11 16:06:47 +02:00
|
|
|
|
2017-09-07 18:57:06 +02:00
|
|
|
if len(calls) < 1 {
|
2019-05-11 16:06:47 +02:00
|
|
|
name, value := splitVar(arg)
|
2020-03-29 21:54:59 +02:00
|
|
|
globals.Set(name, taskfile.Var{Static: value})
|
2019-05-11 16:06:47 +02:00
|
|
|
} else {
|
|
|
|
if calls[len(calls)-1].Vars == nil {
|
2020-03-29 21:54:59 +02:00
|
|
|
calls[len(calls)-1].Vars = &taskfile.Vars{}
|
2019-05-11 16:06:47 +02:00
|
|
|
}
|
2020-03-29 21:54:59 +02:00
|
|
|
name, value := splitVar(arg)
|
|
|
|
calls[len(calls)-1].Vars.Set(name, taskfile.Var{Static: value})
|
2019-05-11 16:06:47 +02:00
|
|
|
}
|
2017-09-07 18:57:06 +02:00
|
|
|
}
|
2019-05-11 16:06:47 +02:00
|
|
|
|
2019-12-07 21:20:36 +02:00
|
|
|
if len(calls) == 0 {
|
2023-06-18 03:32:18 +02:00
|
|
|
calls = append(calls, taskfile.Call{Task: "default", Direct: true})
|
2019-12-07 21:20:36 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 16:06:47 +02:00
|
|
|
return calls, globals
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitVar(s string) (string, string) {
|
|
|
|
pair := strings.SplitN(s, "=", 2)
|
|
|
|
return pair[0], pair[1]
|
2017-09-07 18:57:06 +02:00
|
|
|
}
|