Skip to content

Commit

Permalink
feat: add support for well known files
Browse files Browse the repository at this point in the history
Related to #135
  • Loading branch information
ccoVeille committed Jul 9, 2024
1 parent 283ce95 commit ec013c7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/filetype/file_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type FileType struct {
Name string
Extensions map[string]struct{}
KnownFiles map[string]struct{}
Validator validator.Validator
}

Expand Down Expand Up @@ -52,6 +53,7 @@ var TomlFileType = FileType{
var IniFileType = FileType{
Name: "ini",
Extensions: misc.ArrToMap("ini"),
KnownFiles: misc.ArrToMap(".editorconfig", ".gitconfig", ".gitmodules", ".shellcheckrc", ".npmrc", "inputrc", ".inputrc", ".wgetrc", ".curlrc", ".nanorc"),
Validator: validator.IniValidator{},
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/finder/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ func Test_fsFinderCustomTypes(t *testing.T) {
}
}

func Test_fsFinderKnownFiles(t *testing.T) {
jsonFileType := filetype.FileType{
Name: "json",
Extensions: misc.ArrToMap("whatever"),
KnownFiles: misc.ArrToMap(".editorconfig"),
Validator: validator.JsonValidator{},

Check failure on line 139 in pkg/finder/finder_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21, ubuntu-latest)

undefined: validator.JsonValidator (typecheck)

Check failure on line 139 in pkg/finder/finder_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21, macos-latest)

undefined: validator.JsonValidator (typecheck)

Check failure on line 139 in pkg/finder/finder_test.go

View workflow job for this annotation

GitHub Actions / Update coverage badge

undefined: validator.JsonValidator
}
fsFinder := FileSystemFinderInit(
WithPathRoots("../../test/fixtures"),
WithExcludeDirs([]string{"subdir"}),
WithFileTypes([]filetype.FileType{jsonFileType}),
)

files, err := fsFinder.Find()

if len(files) < 1 {
t.Errorf("Unable to find files")
}

if err != nil {
t.Errorf("Unable to find files")
}
}

func Test_fsFinderPathNoExist(t *testing.T) {
fsFinder := FileSystemFinderInit(
WithPathRoots("/bad/path"),
Expand Down
8 changes: 8 additions & 0 deletions pkg/finder/fsfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,21 @@ func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) {
// filepath.Ext() returns the extension name with a dot so it
// needs to be removed.

walkFileName := filepath.Base(path)
walkFileExtension := strings.TrimPrefix(filepath.Ext(path), ".")

if _, ok := fsf.ExcludeFileTypes[walkFileExtension]; ok {
return nil
}
extensionLowerCase := strings.ToLower(walkFileExtension)
for _, fileType := range fsf.FileTypes {

if _, ok := fileType.KnownFiles[walkFileName]; ok {
fileMetadata := FileMetadata{dirEntry.Name(), path, fileType}
matchingFiles = append(matchingFiles, fileMetadata)
break
}

if _, ok := fileType.Extensions[extensionLowerCase]; ok {
fileMetadata := FileMetadata{dirEntry.Name(), path, fileType}
matchingFiles = append(matchingFiles, fileMetadata)
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# https://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf

0 comments on commit ec013c7

Please sign in to comment.