Skip to content

Commit 7cab4c9

Browse files
committed
feat: add support for well known files
Related to #135
1 parent 283ce95 commit 7cab4c9

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

pkg/filetype/file_type.go

Lines changed: 2 additions & 0 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

@@ -52,6 +53,7 @@ var TomlFileType = FileType{
5253
var IniFileType = FileType{
5354
Name: "ini",
5455
Extensions: misc.ArrToMap("ini"),
56+
KnownFiles: misc.ArrToMap(".editorconfig", ".gitconfig", ".gitmodules", ".shellcheckrc", ".npmrc", "inputrc", ".inputrc", ".wgetrc", ".curlrc", ".nanorc"),
5557
Validator: validator.IniValidator{},
5658
}
5759

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: misc.ArrToMap("whatever"),
138+
KnownFiles: misc.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.Errorf("Unable to find files")
151+
}
152+
153+
if err != nil {
154+
t.Errorf("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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,21 @@ func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) {
133133
// filepath.Ext() returns the extension name with a dot so it
134134
// needs to be removed.
135135

136+
walkFileName := filepath.Base(path)
136137
walkFileExtension := strings.TrimPrefix(filepath.Ext(path), ".")
137138

138139
if _, ok := fsf.ExcludeFileTypes[walkFileExtension]; ok {
139140
return nil
140141
}
141142
extensionLowerCase := strings.ToLower(walkFileExtension)
142143
for _, fileType := range fsf.FileTypes {
144+
145+
if _, ok := fileType.KnownFiles[walkFileName]; ok {
146+
fileMetadata := FileMetadata{dirEntry.Name(), path, fileType}
147+
matchingFiles = append(matchingFiles, fileMetadata)
148+
break
149+
}
150+
143151
if _, ok := fileType.Extensions[extensionLowerCase]; ok {
144152
fileMetadata := FileMetadata{dirEntry.Name(), path, fileType}
145153
matchingFiles = append(matchingFiles, fileMetadata)

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)