Skip to content

Commit f6b9fb0

Browse files
committed
feat: add .KEY variable
1 parent d41f437 commit f6b9fb0

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

docs/docs/experiments/any_variables.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ tasks:
6363
```
6464
6565
There are many more templating functions which can be used with the new types of
66-
variables. For a full list, see the
67-
[slim-sprig][slim-sprig] documentation.
66+
variables. For a full list, see the [slim-sprig][slim-sprig] documentation.
6867
6968
## Looping over variables
7069
@@ -102,8 +101,10 @@ tasks:
102101
cmd: echo {{.ITEM}}
103102
```
104103

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

108109
```yaml
109110
version: 3
@@ -121,7 +122,7 @@ tasks:
121122
cmds:
122123
- for:
123124
var: MAP
124-
cmd: echo {{.ITEM.SUBKEY}}
125+
cmd: echo {{.KEY}} {{.ITEM.SUBKEY}}
125126
```
126127

127128
String splitting is still supported and remember that for simple cases, you have

testdata/vars/any/Taskfile.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ tasks:
8989
cmds:
9090
- for:
9191
var: MAP
92-
cmd: echo {{.ITEM}}
92+
cmd: echo {{.KEY}} {{.ITEM}}
9393

9494
for-multi-layer-map:
9595
vars:
@@ -103,4 +103,4 @@ tasks:
103103
cmds:
104104
- for:
105105
var: MAP
106-
cmd: echo {{.ITEM.SUBKEY}}
106+
cmd: echo {{.KEY}} {{.ITEM.SUBKEY}}

variables.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/joho/godotenv"
9-
"golang.org/x/exp/maps"
109

1110
"github.com/go-task/task/v3/errors"
1211
"github.com/go-task/task/v3/internal/execext"
@@ -134,6 +133,7 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
134133
continue
135134
}
136135
if cmd.For != nil {
136+
var keys []string
137137
var list []any
138138
// Get the list from the explicit for list
139139
if cmd.For.List != nil && len(cmd.For.List) > 0 {
@@ -171,7 +171,10 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
171171
case []any:
172172
list = value
173173
case map[string]any:
174-
list = maps.Values(value)
174+
for k, v := range value {
175+
keys = append(keys, k)
176+
list = append(list, v)
177+
}
175178
default:
176179
return nil, errors.TaskfileInvalidError{
177180
URI: origTask.Location.Taskfile,
@@ -189,10 +192,13 @@ func (e *Executor) compiledTask(call taskfile.Call, evaluateShVars bool) (*taskf
189192
as = "ITEM"
190193
}
191194
// Create a new command for each item in the list
192-
for _, loopValue := range list {
195+
for i, loopValue := range list {
193196
extra := map[string]any{
194197
as: loopValue,
195198
}
199+
if len(keys) > 0 {
200+
extra["KEY"] = keys[i]
201+
}
196202
new.Cmds = append(new.Cmds, &taskfile.Cmd{
197203
Cmd: r.ReplaceWithExtra(cmd.Cmd, extra),
198204
Task: r.ReplaceWithExtra(cmd.Task, extra),

0 commit comments

Comments
 (0)