Skip to content

Commit

Permalink
feat: add .KEY variable
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Dec 21, 2023
1 parent d41f437 commit f6b9fb0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
11 changes: 6 additions & 5 deletions docs/docs/experiments/any_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ tasks:
```
There are many more templating functions which can be used with the new types of
variables. For a full list, see the
[slim-sprig][slim-sprig] documentation.
variables. For a full list, see the [slim-sprig][slim-sprig] documentation.
## Looping over variables
Expand Down Expand Up @@ -102,8 +101,10 @@ tasks:
cmd: echo {{.ITEM}}
```

This also works for maps. However, remember that maps are unordered, so the
order in which the items are looped over is random:
This also works for maps. When looping over a map we also make an additional
`{{.KEY}}` variable availabe that holds the string value of the map key.
Remember that maps are unordered, so the order in which the items are looped
over is random:

```yaml
version: 3
Expand All @@ -121,7 +122,7 @@ tasks:
cmds:
- for:
var: MAP
cmd: echo {{.ITEM.SUBKEY}}
cmd: echo {{.KEY}} {{.ITEM.SUBKEY}}
```

String splitting is still supported and remember that for simple cases, you have
Expand Down
4 changes: 2 additions & 2 deletions testdata/vars/any/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ tasks:
cmds:
- for:
var: MAP
cmd: echo {{.ITEM}}
cmd: echo {{.KEY}} {{.ITEM}}

for-multi-layer-map:
vars:
Expand All @@ -103,4 +103,4 @@ tasks:
cmds:
- for:
var: MAP
cmd: echo {{.ITEM.SUBKEY}}
cmd: echo {{.KEY}} {{.ITEM.SUBKEY}}
12 changes: 9 additions & 3 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/joho/godotenv"
"golang.org/x/exp/maps"

"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/execext"
Expand Down Expand Up @@ -134,6 +133,7 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
continue
}
if cmd.For != nil {
var keys []string
var list []any
// Get the list from the explicit for list
if cmd.For.List != nil && len(cmd.For.List) > 0 {
Expand Down Expand Up @@ -171,7 +171,10 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
case []any:
list = value
case map[string]any:
list = maps.Values(value)
for k, v := range value {
keys = append(keys, k)
list = append(list, v)
}
default:
return nil, errors.TaskfileInvalidError{
URI: origTask.Location.Taskfile,
Expand All @@ -189,10 +192,13 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
as = "ITEM"
}
// Create a new command for each item in the list
for _, loopValue := range list {
for i, loopValue := range list {
extra := map[string]any{
as: loopValue,
}
if len(keys) > 0 {
extra["KEY"] = keys[i]
}
new.Cmds = append(new.Cmds, &taskfile.Cmd{
Cmd: r.ReplaceWithExtra(cmd.Cmd, extra),
Task: r.ReplaceWithExtra(cmd.Task, extra),
Expand Down

0 comments on commit f6b9fb0

Please sign in to comment.