Skip to content

Commit dd8daa6

Browse files
authored
feat: allow wildcards to match multiple tasks (#2121)
* feat: allow wildcards to match multiple tasks * docs: improved wildcard section
1 parent 55617e0 commit dd8daa6

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

task.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ func (e *Executor) FindMatchingTasks(call *Call) []*MatchingTask {
412412
return matchingTasks
413413
}
414414
// Attempt a wildcard match
415-
// For now, we can just nil check the task before each loop
416415
for _, value := range e.Taskfile.Tasks.All(nil) {
417416
if match, wildcards := value.WildcardMatch(call.Task); match {
418417
matchingTasks = append(matchingTasks, &MatchingTask{
@@ -430,23 +429,12 @@ func (e *Executor) FindMatchingTasks(call *Call) []*MatchingTask {
430429
func (e *Executor) GetTask(call *Call) (*ast.Task, error) {
431430
// Search for a matching task
432431
matchingTasks := e.FindMatchingTasks(call)
433-
switch len(matchingTasks) {
434-
case 0: // Carry on
435-
case 1:
432+
if len(matchingTasks) > 0 {
436433
if call.Vars == nil {
437434
call.Vars = ast.NewVars()
438435
}
439436
call.Vars.Set("MATCH", ast.Var{Value: matchingTasks[0].Wildcards})
440437
return matchingTasks[0].Task, nil
441-
default:
442-
taskNames := make([]string, len(matchingTasks))
443-
for i, matchingTask := range matchingTasks {
444-
taskNames[i] = matchingTask.Task.Task
445-
}
446-
return nil, &errors.TaskNameConflictError{
447-
Call: call.Task,
448-
TaskNames: taskNames,
449-
}
450438
}
451439

452440
// If didn't find one, search for a task with a matching alias

task_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,9 +3196,9 @@ func TestWildcard(t *testing.T) {
31963196
wantErr: true,
31973197
},
31983198
{
3199-
name: "multiple matches",
3200-
call: "wildcard-foo-bar",
3201-
wantErr: true,
3199+
name: "multiple matches",
3200+
call: "wildcard-foo-bar",
3201+
expectedOutput: "Hello foo-bar\n",
32023202
},
32033203
}
32043204

website/docs/usage.mdx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,36 +1682,45 @@ clear what they contain:
16821682
version: '3'
16831683
16841684
tasks:
1685-
echo-*:
1685+
start:*:*:
16861686
vars:
1687-
TEXT: '{{index .MATCH 0}}'
1687+
SERVICE: "{{index .MATCH 0}}"
1688+
REPLICAS: "{{index .MATCH 1}}"
16881689
cmds:
1689-
- echo {{.TEXT}}
1690+
- echo "Starting {{.SERVICE}} with {{.REPLICAS}} replicas"
16901691
1691-
run-*-*:
1692+
start:*:
16921693
vars:
1693-
ARG_1: '{{index .MATCH 0}}'
1694-
ARG_2: '{{index .MATCH 1}}'
1694+
SERVICE: "{{index .MATCH 0}}"
16951695
cmds:
1696-
- echo {{.ARG_1}} {{.ARG_2}}
1696+
- echo "Starting {{.SERVICE}}"
1697+
```
1698+
1699+
This call matches the `start:*` task and the string "foo" is captured by the
1700+
wildcard and stored in the `.MATCH` variable. We then index the `.MATCH` array
1701+
and store the result in the `.SERVICE` variable which is then echoed out in the
1702+
cmds:
1703+
1704+
```shell
1705+
$ task start:foo
1706+
Starting foo
16971707
```
16981708

1709+
You can use whitespace in your arguments as long as you quote the task name:
1710+
16991711
```shell
1700-
# This call matches the "echo-*" task and the string "hello" is captured by the
1701-
# wildcard and stored in the .MATCH variable. We then index the .MATCH array and
1702-
# store the result in the .TEXT variable which is then echoed out in the cmds.
1703-
$ task echo-hello
1704-
hello
1705-
# You can use whitespace in your arguments as long as you quote the task name
1706-
$ task "echo-hello world"
1707-
hello world
1708-
# And you can pass multiple arguments
1709-
$ task run-foo-bar
1710-
foo bar
1711-
```
1712-
1713-
If multiple matching tasks are found, an error occurs. If you are using included
1714-
Taskfiles, tasks in parent files will be considered first.
1712+
$ task "start:foo bar"
1713+
Starting foo bar
1714+
```
1715+
1716+
If multiple matching tasks are found, the first one listed in the Taskfile will
1717+
be used. If you are using included Taskfiles, tasks in parent files will be
1718+
considered first.
1719+
1720+
```shell
1721+
$ task start:foo:3
1722+
Starting foo with 3 replicas
1723+
```
17151724

17161725
## Doing task cleanup with `defer`
17171726

0 commit comments

Comments
 (0)