Skip to content

Commit 83f31f9

Browse files
committed
feat: add support for well known files
1 parent 85fc8b9 commit 83f31f9

File tree

5 files changed

+77
-15
lines changed

5 files changed

+77
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Support of well known files for configuration discovery
13+
1014
## [1.9.0] - 2025-11-14
1115

1216
### Added

pkg/filetype/file_type.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type FileType struct {
1313
Name string
1414
Extensions map[string]struct{}
15+
KnownFiles map[string]struct{}
1516
Validator validator.Validator
1617
}
1718

@@ -28,7 +29,13 @@ var JSONFileType = FileType{
2829
var YAMLFileType = FileType{
2930
Name: "yaml",
3031
Extensions: tools.ArrToMap("yml", "yaml"),
31-
Validator: validator.YAMLValidator{},
32+
KnownFiles: tools.ArrToMap(
33+
".clang-format",
34+
".clang-tidy",
35+
".clangd",
36+
".gemrc",
37+
),
38+
Validator: validator.YAMLValidator{},
3239
}
3340

3441
// Instance of FileType object to
@@ -52,7 +59,21 @@ var TomlFileType = FileType{
5259
var IniFileType = FileType{
5360
Name: "ini",
5461
Extensions: tools.ArrToMap("ini"),
55-
Validator: validator.IniValidator{},
62+
KnownFiles: tools.ArrToMap(
63+
".editorconfig",
64+
".gitconfig",
65+
".gitmodules",
66+
".shellcheckrc",
67+
".npmrc",
68+
"inputrc",
69+
".inputrc",
70+
".wgetrc",
71+
".curlrc",
72+
".nanorc",
73+
".flake8",
74+
".pylintrc",
75+
),
76+
Validator: validator.IniValidator{},
5677
}
5778

5879
// Instance of FileType object to

pkg/finder/finder_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ func Test_fsFinderCustomTypes(t *testing.T) {
131131
}
132132
}
133133

134+
func Test_fsFinderKnownFiles(t *testing.T) {
135+
jsonFileType := filetype.FileType{
136+
Name: "json",
137+
Extensions: tools.ArrToMap("whatever"),
138+
KnownFiles: tools.ArrToMap(".editorconfig"),
139+
Validator: validator.JSONValidator{},
140+
}
141+
fsFinder := FileSystemFinderInit(
142+
WithPathRoots("../../test/fixtures"),
143+
WithExcludeDirs([]string{"subdir"}),
144+
WithFileTypes([]filetype.FileType{jsonFileType}),
145+
)
146+
147+
files, err := fsFinder.Find()
148+
149+
if len(files) < 1 {
150+
t.Error("Unable to find files")
151+
}
152+
153+
if err != nil {
154+
t.Error("Unable to find files")
155+
}
156+
}
157+
134158
func Test_fsFinderPathNoExist(t *testing.T) {
135159
fsFinder := FileSystemFinderInit(
136160
WithPathRoots("/bad/path"),

pkg/finder/fsfinder.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ func (fsf FileSystemFinder) findOne(pathRoot string, seenMap map[string]struct{}
128128
if !dirEntry.IsDir() {
129129
// filepath.Ext() returns the extension name with a dot so it
130130
// needs to be removed.
131+
132+
walkFileName := filepath.Base(path)
131133
walkFileExtension := strings.TrimPrefix(filepath.Ext(path), ".")
132134
extensionLowerCase := strings.ToLower(walkFileExtension)
133135

@@ -136,20 +138,25 @@ func (fsf FileSystemFinder) findOne(pathRoot string, seenMap map[string]struct{}
136138
}
137139

138140
for _, fileType := range fsf.FileTypes {
139-
if _, isMatched := fileType.Extensions[extensionLowerCase]; isMatched {
140-
absPath, err := filepath.Abs(path)
141-
if err != nil {
142-
return err
143-
}
144-
145-
if _, seen := seenMap[absPath]; !seen {
146-
fileMetadata := FileMetadata{dirEntry.Name(), absPath, fileType}
147-
matchingFiles = append(matchingFiles, fileMetadata)
148-
seenMap[absPath] = struct{}{}
149-
}
150-
151-
return nil
141+
_, isKnownFile := fileType.KnownFiles[walkFileName]
142+
_, hasExtension := fileType.Extensions[extensionLowerCase]
143+
144+
if !isKnownFile && !hasExtension {
145+
continue
146+
}
147+
148+
absPath, err := filepath.Abs(path)
149+
if err != nil {
150+
return err
152151
}
152+
153+
if _, seen := seenMap[absPath]; !seen {
154+
fileMetadata := FileMetadata{dirEntry.Name(), absPath, fileType}
155+
matchingFiles = append(matchingFiles, fileMetadata)
156+
seenMap[absPath] = struct{}{}
157+
}
158+
159+
return nil
153160
}
154161
fsf.ExcludeFileTypes[extensionLowerCase] = struct{}{}
155162
}

test/fixtures/.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf

0 commit comments

Comments
 (0)