mirror of
https://github.com/go-task/task.git
synced 2025-06-08 23:56:21 +02:00
feat: allow wildcards to match multiple tasks (#2121)
* feat: allow wildcards to match multiple tasks * docs: improved wildcard section
This commit is contained in:
parent
55617e062f
commit
dd8daa68cd
14
task.go
14
task.go
@ -412,7 +412,6 @@ func (e *Executor) FindMatchingTasks(call *Call) []*MatchingTask {
|
|||||||
return matchingTasks
|
return matchingTasks
|
||||||
}
|
}
|
||||||
// Attempt a wildcard match
|
// Attempt a wildcard match
|
||||||
// For now, we can just nil check the task before each loop
|
|
||||||
for _, value := range e.Taskfile.Tasks.All(nil) {
|
for _, value := range e.Taskfile.Tasks.All(nil) {
|
||||||
if match, wildcards := value.WildcardMatch(call.Task); match {
|
if match, wildcards := value.WildcardMatch(call.Task); match {
|
||||||
matchingTasks = append(matchingTasks, &MatchingTask{
|
matchingTasks = append(matchingTasks, &MatchingTask{
|
||||||
@ -430,23 +429,12 @@ func (e *Executor) FindMatchingTasks(call *Call) []*MatchingTask {
|
|||||||
func (e *Executor) GetTask(call *Call) (*ast.Task, error) {
|
func (e *Executor) GetTask(call *Call) (*ast.Task, error) {
|
||||||
// Search for a matching task
|
// Search for a matching task
|
||||||
matchingTasks := e.FindMatchingTasks(call)
|
matchingTasks := e.FindMatchingTasks(call)
|
||||||
switch len(matchingTasks) {
|
if len(matchingTasks) > 0 {
|
||||||
case 0: // Carry on
|
|
||||||
case 1:
|
|
||||||
if call.Vars == nil {
|
if call.Vars == nil {
|
||||||
call.Vars = ast.NewVars()
|
call.Vars = ast.NewVars()
|
||||||
}
|
}
|
||||||
call.Vars.Set("MATCH", ast.Var{Value: matchingTasks[0].Wildcards})
|
call.Vars.Set("MATCH", ast.Var{Value: matchingTasks[0].Wildcards})
|
||||||
return matchingTasks[0].Task, nil
|
return matchingTasks[0].Task, nil
|
||||||
default:
|
|
||||||
taskNames := make([]string, len(matchingTasks))
|
|
||||||
for i, matchingTask := range matchingTasks {
|
|
||||||
taskNames[i] = matchingTask.Task.Task
|
|
||||||
}
|
|
||||||
return nil, &errors.TaskNameConflictError{
|
|
||||||
Call: call.Task,
|
|
||||||
TaskNames: taskNames,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If didn't find one, search for a task with a matching alias
|
// If didn't find one, search for a task with a matching alias
|
||||||
|
@ -3196,9 +3196,9 @@ func TestWildcard(t *testing.T) {
|
|||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple matches",
|
name: "multiple matches",
|
||||||
call: "wildcard-foo-bar",
|
call: "wildcard-foo-bar",
|
||||||
wantErr: true,
|
expectedOutput: "Hello foo-bar\n",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1682,36 +1682,45 @@ clear what they contain:
|
|||||||
version: '3'
|
version: '3'
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
echo-*:
|
start:*:*:
|
||||||
vars:
|
vars:
|
||||||
TEXT: '{{index .MATCH 0}}'
|
SERVICE: "{{index .MATCH 0}}"
|
||||||
|
REPLICAS: "{{index .MATCH 1}}"
|
||||||
cmds:
|
cmds:
|
||||||
- echo {{.TEXT}}
|
- echo "Starting {{.SERVICE}} with {{.REPLICAS}} replicas"
|
||||||
|
|
||||||
run-*-*:
|
start:*:
|
||||||
vars:
|
vars:
|
||||||
ARG_1: '{{index .MATCH 0}}'
|
SERVICE: "{{index .MATCH 0}}"
|
||||||
ARG_2: '{{index .MATCH 1}}'
|
|
||||||
cmds:
|
cmds:
|
||||||
- echo {{.ARG_1}} {{.ARG_2}}
|
- echo "Starting {{.SERVICE}}"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This call matches the `start:*` task and the string "foo" is captured by the
|
||||||
|
wildcard and stored in the `.MATCH` variable. We then index the `.MATCH` array
|
||||||
|
and store the result in the `.SERVICE` variable which is then echoed out in the
|
||||||
|
cmds:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# This call matches the "echo-*" task and the string "hello" is captured by the
|
$ task start:foo
|
||||||
# wildcard and stored in the .MATCH variable. We then index the .MATCH array and
|
Starting foo
|
||||||
# store the result in the .TEXT variable which is then echoed out in the cmds.
|
|
||||||
$ task echo-hello
|
|
||||||
hello
|
|
||||||
# You can use whitespace in your arguments as long as you quote the task name
|
|
||||||
$ task "echo-hello world"
|
|
||||||
hello world
|
|
||||||
# And you can pass multiple arguments
|
|
||||||
$ task run-foo-bar
|
|
||||||
foo bar
|
|
||||||
```
|
```
|
||||||
|
|
||||||
If multiple matching tasks are found, an error occurs. If you are using included
|
You can use whitespace in your arguments as long as you quote the task name:
|
||||||
Taskfiles, tasks in parent files will be considered first.
|
|
||||||
|
```shell
|
||||||
|
$ task "start:foo bar"
|
||||||
|
Starting foo bar
|
||||||
|
```
|
||||||
|
|
||||||
|
If multiple matching tasks are found, the first one listed in the Taskfile will
|
||||||
|
be used. If you are using included Taskfiles, tasks in parent files will be
|
||||||
|
considered first.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ task start:foo:3
|
||||||
|
Starting foo with 3 replicas
|
||||||
|
```
|
||||||
|
|
||||||
## Doing task cleanup with `defer`
|
## Doing task cleanup with `defer`
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user