Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,47 @@ func (e *Executor) GetTask(call *Call) (*ast.Task, error) {
}

if len(matchingTasks) > 0 {
if call.Vars == nil {
call.Vars = ast.NewVars()
match := matchingTasks[0].Wildcards
if match == nil {
match = []string{}
}

var (
includeMatch ast.Var
hasMatchVar bool
)

if call.Vars != nil {
includeMatch, hasMatchVar = call.Vars.Get("MATCH")
if hasMatchVar {
if existingMatch, ok := includeMatch.Value.([]string); ok && existingMatch == nil {
includeMatch = ast.Var{Value: []string{}}
call.Vars.Set("MATCH", includeMatch)
}
}
}
call.Vars.Set("MATCH", ast.Var{Value: matchingTasks[0].Wildcards})

if len(match) > 0 || !hasMatchVar {
if call.Vars == nil {
call.Vars = ast.NewVars()
}
includeMatch = ast.Var{Value: match}
hasMatchVar = true
call.Vars.Set("MATCH", includeMatch)
}

if hasMatchVar {
taskCopy := matchingTasks[0].Task.DeepCopy()
matchFirstIncludeVars := ast.NewVars()
matchFirstIncludeVars.Set("MATCH", includeMatch)
if taskCopy.IncludeVars != nil {
matchFirstIncludeVars.Merge(taskCopy.IncludeVars, nil)
}
matchFirstIncludeVars.Set("MATCH", includeMatch)
taskCopy.IncludeVars = matchFirstIncludeVars
return taskCopy, nil
}

return matchingTasks[0].Task, nil
}

Expand Down
17 changes: 17 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,23 @@ func TestIncludesInterpolation(t *testing.T) { // nolint:paralleltest // cannot
}
}

func TestIncludeWildcardVarsExposeMatch(t *testing.T) {
t.Parallel()

var buff bytes.Buffer
e := task.NewExecutor(
task.WithDir("testdata/includes_wildcard_vars"),
task.WithSilent(true),
task.WithStdout(&buff),
task.WithStderr(&buff),
)
require.NoError(t, e.Setup())

err := e.Run(t.Context(), &task.Call{Task: "stack:prod:show"})
require.NoError(t, err)
assert.Equal(t, "prod\n", buff.String())
}

func TestIncludesWithExclude(t *testing.T) {
t.Parallel()

Expand Down
6 changes: 6 additions & 0 deletions testdata/includes_wildcard_vars/Taskfile.stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'

tasks:
show:
cmds:
- echo '{{.ENV}}'
7 changes: 7 additions & 0 deletions testdata/includes_wildcard_vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'

includes:
'stack:*':
taskfile: ./Taskfile.stack.yml
vars:
ENV: '{{index .MATCH 0}}'
Loading