Skip to content

Commit e09f682

Browse files
committed
iterate over all modules and break out early if possible
1 parent f64821c commit e09f682

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

internal/configs/config.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package configs
55

66
import (
77
"fmt"
8+
"iter"
89
"log"
910
"maps"
1011
"slices"
@@ -143,15 +144,16 @@ func (c *Config) DeepEach(cb func(c *Config)) {
143144
}
144145
}
145146

146-
// AllModules returns a slice of all the receiver and all of its descendant
147-
// nodes in the module tree, in the same order they would be visited by
148-
// DeepEach.
149-
func (c *Config) AllModules() []*Config {
150-
var ret []*Config
151-
c.DeepEach(func(c *Config) {
152-
ret = append(ret, c)
153-
})
154-
return ret
147+
// AllModules returns an iterator of all the receiver and all of its descendant
148+
// nodes in the module tree until the iterator is exhausted or terminated.
149+
func (c *Config) AllModules() iter.Seq[*Config] {
150+
return func(yield func(*Config) bool) {
151+
for _, ch := range c.Children {
152+
if !yield(ch) {
153+
return
154+
}
155+
}
156+
}
155157
}
156158

157159
// Descendant returns the descendant config that has the given path beneath

internal/terraform/context_plan.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,12 @@ The -target option is not for routine use, and is provided only for exceptional
272272

273273
if opts.Query {
274274
var hasQuery bool
275-
config.DeepEach(func(c *configs.Config) {
275+
for c := range config.AllModules() {
276276
if len(c.Module.ListResources) > 0 {
277277
hasQuery = true
278+
break
278279
}
279-
})
280+
}
280281

281282
if !hasQuery {
282283
diags = diags.Append(tfdiags.Sourceless(

0 commit comments

Comments
 (0)