Skip to content

Commit 935539e

Browse files
committed
remove dirFiles
1 parent 2e453c3 commit 935539e

File tree

2 files changed

+28
-130
lines changed

2 files changed

+28
-130
lines changed

internal/configs/parser_config_dir.go

+10-112
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package configs
66
import (
77
"fmt"
88
"os"
9-
"path"
109
"path/filepath"
1110
"strings"
1211

@@ -26,10 +25,10 @@ const (
2625
// overrides.
2726
// Optionally, test files (.tftest.hcl and .tftest.json) can be loaded from
2827
// a subdirectory of the given directory, which is specified by the
29-
// WithTestFiles option, or from the default test directory.
28+
// MatchTestFiles option, or from the default test directory.
3029
// If this option is not specified, test files will not be loaded.
3130
// Query files (.tfquery.hcl) are also loaded from the given directory if
32-
// specified by the WithQueryFiles option.
31+
// specified by the MatchQueryFiles option.
3332
//
3433
// If this method returns nil, that indicates that the given directory does not
3534
// exist at all or could not be opened for some reason. Callers may wish to
@@ -52,7 +51,7 @@ func (p *Parser) LoadConfigDir(path string, opts ...Option) (*Module, hcl.Diagno
5251
return nil, diags
5352
}
5453

55-
// Load the actual files
54+
// Load the .tf configuration files
5655
primary, fDiags := p.loadFiles(fileSet.Primary, false)
5756
diags = diags.Extend(fDiags)
5857

@@ -154,10 +153,10 @@ func (p Parser) ConfigDirFiles(dir string, opts ...Option) (primary, override []
154153
// IsConfigDir determines whether the given path refers to a directory that
155154
// exists and contains at least one Terraform config file (with a .tf or
156155
// .tf.json extension.). Note, we explicitely exclude checking for tests here
157-
// as tests must live alongside actual .tf config files.
156+
// as tests must live alongside actual .tf config files. Same goes for query files.
158157
func (p *Parser) IsConfigDir(path string) bool {
159-
primaryPaths, overridePaths, _, _ := p.dirFiles(path, "")
160-
return (len(primaryPaths) + len(overridePaths)) > 0
158+
pathSet, _ := p.dirFileSet(path)
159+
return (len(pathSet.Primary) + len(pathSet.Override)) > 0
161160
}
162161

163162
func (p *Parser) loadFiles(paths []string, override bool) ([]*File, hcl.Diagnostics) {
@@ -181,109 +180,6 @@ func (p *Parser) loadFiles(paths []string, override bool) ([]*File, hcl.Diagnost
181180
return files, diags
182181
}
183182

184-
// dirFiles finds Terraform configuration files within dir, splitting them into
185-
// primary and override files based on the filename.
186-
//
187-
// If testsDir is not empty, dirFiles will also retrieve Terraform testing files
188-
// both directly within dir and within testsDir as a subdirectory of dir. In
189-
// this way, testsDir acts both as a direction to retrieve test files within the
190-
// main direction and as the location for additional test files.
191-
func (p *Parser) dirFiles(dir string, testsDir string) (primary, override, tests []string, diags hcl.Diagnostics) {
192-
includeTests := len(testsDir) > 0
193-
194-
if includeTests {
195-
testPath := path.Join(dir, testsDir)
196-
197-
infos, err := p.fs.ReadDir(testPath)
198-
if err != nil {
199-
// Then we couldn't read from the testing directory for some reason.
200-
201-
if os.IsNotExist(err) {
202-
// Then this means the testing directory did not exist.
203-
// We won't actually stop loading the rest of the configuration
204-
// for this, we will add a warning to explain to the user why
205-
// test files weren't processed but leave it at that.
206-
if testsDir != DefaultTestDirectory {
207-
// We'll only add the warning if a directory other than the
208-
// default has been requested. If the user is just loading
209-
// the default directory then we have no expectation that
210-
// it should actually exist.
211-
diags = append(diags, &hcl.Diagnostic{
212-
Severity: hcl.DiagWarning,
213-
Summary: "Test directory does not exist",
214-
Detail: fmt.Sprintf("Requested test directory %s does not exist.", testPath),
215-
})
216-
}
217-
} else {
218-
// Then there is some other reason we couldn't load. We will
219-
// treat this as a full error.
220-
diags = append(diags, &hcl.Diagnostic{
221-
Severity: hcl.DiagError,
222-
Summary: "Failed to read test directory",
223-
Detail: fmt.Sprintf("Test directory %s could not be read: %v.", testPath, err),
224-
})
225-
226-
// We'll also stop loading the rest of the config for this.
227-
return
228-
}
229-
230-
} else {
231-
for _, testInfo := range infos {
232-
if testInfo.IsDir() || IsIgnoredFile(testInfo.Name()) {
233-
continue
234-
}
235-
236-
if strings.HasSuffix(testInfo.Name(), ".tftest.hcl") || strings.HasSuffix(testInfo.Name(), ".tftest.json") {
237-
tests = append(tests, filepath.Join(testPath, testInfo.Name()))
238-
}
239-
}
240-
}
241-
242-
}
243-
244-
infos, err := p.fs.ReadDir(dir)
245-
if err != nil {
246-
diags = append(diags, &hcl.Diagnostic{
247-
Severity: hcl.DiagError,
248-
Summary: "Failed to read module directory",
249-
Detail: fmt.Sprintf("Module directory %s does not exist or cannot be read.", dir),
250-
})
251-
return
252-
}
253-
254-
for _, info := range infos {
255-
if info.IsDir() {
256-
// We only care about terraform configuration files.
257-
continue
258-
}
259-
260-
name := info.Name()
261-
ext := fileExt(name)
262-
if ext == "" || IsIgnoredFile(name) {
263-
continue
264-
}
265-
266-
if ext == ".tftest.hcl" || ext == ".tftest.json" {
267-
if includeTests {
268-
tests = append(tests, filepath.Join(dir, name))
269-
}
270-
continue
271-
}
272-
273-
baseName := name[:len(name)-len(ext)] // strip extension
274-
isOverride := baseName == "override" || strings.HasSuffix(baseName, "_override")
275-
276-
fullPath := filepath.Join(dir, name)
277-
if isOverride {
278-
override = append(override, fullPath)
279-
} else {
280-
primary = append(primary, fullPath)
281-
}
282-
}
283-
284-
return
285-
}
286-
287183
func (p *Parser) loadTestFiles(basePath string, paths []string) (map[string]*TestFile, hcl.Diagnostics) {
288184
var diags hcl.Diagnostics
289185

@@ -349,6 +245,8 @@ func fileExt(path string) string {
349245
return ".tftest.json"
350246
} else if strings.HasSuffix(path, ".tfquery.hcl") {
351247
return ".tfquery.hcl"
248+
} else if strings.HasSuffix(path, ".tfquery.json") {
249+
return ".tfquery.json"
352250
} else {
353251
return ""
354252
}
@@ -374,10 +272,10 @@ func IsEmptyDir(path, testDir string) (bool, error) {
374272
}
375273

376274
p := NewParser(nil)
377-
fs, os, tests, diags := p.dirFiles(path, testDir)
275+
fSet, diags := p.dirFileSet(path, MatchTestFiles(testDir))
378276
if diags.HasErrors() {
379277
return false, diags
380278
}
381279

382-
return len(fs) == 0 && len(os) == 0 && len(tests) == 0, nil
280+
return len(fSet.Primary) == 0 && len(fSet.Override) == 0 && len(fSet.Tests) == 0, nil
383281
}

internal/configs/parser_file_matcher.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ type FileMatcher interface {
3131

3232
// DirFiles allows the matcher to process files in a directory
3333
// only relevant to its type.
34-
DirFiles(dir string, options *Options, fileSet *ConfigFileSet) hcl.Diagnostics
34+
DirFiles(dir string, cfg *parserConfig, fileSet *ConfigFileSet) hcl.Diagnostics
3535
}
3636

3737
// Option is a functional option type for configuring the parser
38-
type Option func(*Options)
38+
type Option func(*parserConfig)
3939

40-
type Options struct {
40+
type parserConfig struct {
4141
matchers []FileMatcher
4242
testDirectory string
4343
fs afero.Afero
@@ -57,27 +57,27 @@ func (p *Parser) dirFileSet(dir string, opts ...Option) (ConfigFileSet, hcl.Diag
5757
Queries: []string{},
5858
}
5959

60-
// Set up options
61-
options := &Options{
62-
// We always match module and override files
60+
// Set up the parser configuration
61+
cfg := &parserConfig{
62+
// We always match .tf files
6363
matchers: []FileMatcher{&moduleFiles{}},
6464
testDirectory: DefaultTestDirectory,
6565
fs: p.fs,
6666
}
6767
for _, opt := range opts {
68-
opt(options)
68+
opt(cfg)
6969
}
7070

7171
// Scan and categorize main directory files
72-
mainDirDiags := p.rootFiles(dir, options.matchers, &fileSet)
72+
mainDirDiags := p.rootFiles(dir, cfg.matchers, &fileSet)
7373
diags = append(diags, mainDirDiags...)
7474
if diags.HasErrors() {
7575
return fileSet, diags
7676
}
7777

7878
// Process matcher-specific files
79-
for _, matcher := range options.matchers {
80-
matcherDiags := matcher.DirFiles(dir, options, &fileSet)
79+
for _, matcher := range cfg.matchers {
80+
matcherDiags := matcher.DirFiles(dir, cfg, &fileSet)
8181
diags = append(diags, matcherDiags...)
8282
}
8383

@@ -133,15 +133,15 @@ func (p *Parser) rootFiles(dir string, matchers []FileMatcher, fileSet *ConfigFi
133133

134134
// MatchTestFiles adds a matcher for Terraform test files (.tftest.hcl and .tftest.json)
135135
func MatchTestFiles(dir string) Option {
136-
return func(o *Options) {
136+
return func(o *parserConfig) {
137137
o.testDirectory = dir
138138
o.matchers = append(o.matchers, &testFiles{})
139139
}
140140
}
141141

142-
// MatchQueryFiles adds a matcher for Terraform query files (.tfquery.hcl)
142+
// MatchQueryFiles adds a matcher for Terraform query files (.tfquery.hcl and .tfquery.json)
143143
func MatchQueryFiles() Option {
144-
return func(o *Options) {
144+
return func(o *parserConfig) {
145145
o.matchers = append(o.matchers, &queryFiles{})
146146
}
147147
}
@@ -169,7 +169,7 @@ func (m *moduleFiles) isOverride(name string) bool {
169169
return isOverride
170170
}
171171

172-
func (m *moduleFiles) DirFiles(dir string, options *Options, fileSet *ConfigFileSet) hcl.Diagnostics {
172+
func (m *moduleFiles) DirFiles(dir string, options *parserConfig, fileSet *ConfigFileSet) hcl.Diagnostics {
173173
return nil
174174
}
175175

@@ -180,7 +180,7 @@ func (t *testFiles) Matches(name string) bool {
180180
return strings.HasSuffix(name, ".tftest.hcl") || strings.HasSuffix(name, ".tftest.json")
181181
}
182182

183-
func (t *testFiles) DirFiles(dir string, opts *Options, fileSet *ConfigFileSet) hcl.Diagnostics {
183+
func (t *testFiles) DirFiles(dir string, opts *parserConfig, fileSet *ConfigFileSet) hcl.Diagnostics {
184184
var diags hcl.Diagnostics
185185

186186
testPath := path.Join(dir, opts.testDirectory)
@@ -232,13 +232,13 @@ func (t *testFiles) DirFiles(dir string, opts *Options, fileSet *ConfigFileSet)
232232
return diags
233233
}
234234

235-
// queryFiles matches Terraform query files (.tfquery.hcl)
235+
// queryFiles matches Terraform query files (.tfquery.hcl and .tfquery.json)
236236
type queryFiles struct{}
237237

238238
func (q *queryFiles) Matches(name string) bool {
239-
return strings.HasSuffix(name, ".tfquery.hcl")
239+
return strings.HasSuffix(name, ".tfquery.hcl") || strings.HasSuffix(name, ".tfquery.json")
240240
}
241241

242-
func (q *queryFiles) DirFiles(dir string, options *Options, fileSet *ConfigFileSet) hcl.Diagnostics {
242+
func (q *queryFiles) DirFiles(dir string, options *parserConfig, fileSet *ConfigFileSet) hcl.Diagnostics {
243243
return nil
244244
}

0 commit comments

Comments
 (0)