1
0
mirror of https://github.com/go-task/task.git synced 2025-04-18 12:04:04 +03: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:
Pete Davison 2025-03-26 22:17:27 +00:00 committed by GitHub
parent 55617e062f
commit dd8daa68cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 36 deletions

14
task.go
View File

@ -412,7 +412,6 @@ func (e *Executor) FindMatchingTasks(call *Call) []*MatchingTask {
return matchingTasks
}
// Attempt a wildcard match
// For now, we can just nil check the task before each loop
for _, value := range e.Taskfile.Tasks.All(nil) {
if match, wildcards := value.WildcardMatch(call.Task); match {
matchingTasks = append(matchingTasks, &MatchingTask{
@ -430,23 +429,12 @@ func (e *Executor) FindMatchingTasks(call *Call) []*MatchingTask {
func (e *Executor) GetTask(call *Call) (*ast.Task, error) {
// Search for a matching task
matchingTasks := e.FindMatchingTasks(call)
switch len(matchingTasks) {
case 0: // Carry on
case 1:
if len(matchingTasks) > 0 {
if call.Vars == nil {
call.Vars = ast.NewVars()
}
call.Vars.Set("MATCH", ast.Var{Value: matchingTasks[0].Wildcards})
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

View File

@ -3196,9 +3196,9 @@ func TestWildcard(t *testing.T) {
wantErr: true,
},
{
name: "multiple matches",
call: "wildcard-foo-bar",
wantErr: true,
name: "multiple matches",
call: "wildcard-foo-bar",
expectedOutput: "Hello foo-bar\n",
},
}

View File

@ -1682,36 +1682,45 @@ clear what they contain:
version: '3'
tasks:
echo-*:
start:*:*:
vars:
TEXT: '{{index .MATCH 0}}'
SERVICE: "{{index .MATCH 0}}"
REPLICAS: "{{index .MATCH 1}}"
cmds:
- echo {{.TEXT}}
- echo "Starting {{.SERVICE}} with {{.REPLICAS}} replicas"
run-*-*:
start:*:
vars:
ARG_1: '{{index .MATCH 0}}'
ARG_2: '{{index .MATCH 1}}'
SERVICE: "{{index .MATCH 0}}"
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
# This call matches the "echo-*" task and the string "hello" is captured by the
# wildcard and stored in the .MATCH variable. We then index the .MATCH array and
# 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
$ task start:foo
Starting foo
```
If multiple matching tasks are found, an error occurs. If you are using included
Taskfiles, tasks in parent files will be considered first.
You can use whitespace in your arguments as long as you quote the task name:
```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`