From 283ce95087c67ba025199ad28c4c4d4e1af7a156 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Tue, 9 Jul 2024 23:56:28 +0200 Subject: [PATCH 1/2] chore: use field name for setting struct --- pkg/filetype/file_type.go | 66 +++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/pkg/filetype/file_type.go b/pkg/filetype/file_type.go index 24795bd9..cc2ac0f2 100644 --- a/pkg/filetype/file_type.go +++ b/pkg/filetype/file_type.go @@ -18,89 +18,89 @@ type FileType struct { // Instance of the FileType object to // represent a JSON file var JSONFileType = FileType{ - "json", - misc.ArrToMap("json"), - validator.JSONValidator{}, + Name: "json", + Extensions: misc.ArrToMap("json"), + Validator: validator.JSONValidator{}, } // Instance of the FileType object to // represent a YAML file var YAMLFileType = FileType{ - "yaml", - misc.ArrToMap("yml", "yaml"), - validator.YAMLValidator{}, + Name: "yaml", + Extensions: misc.ArrToMap("yml", "yaml"), + Validator: validator.YAMLValidator{}, } // Instance of FileType object to // represent a XML file var XMLFileType = FileType{ - "xml", - misc.ArrToMap("xml"), - validator.XMLValidator{}, + Name: "xml", + Extensions: misc.ArrToMap("xml"), + Validator: validator.XMLValidator{}, } // Instance of FileType object to // represent a Toml file var TomlFileType = FileType{ - "toml", - misc.ArrToMap("toml"), - validator.TomlValidator{}, + Name: "toml", + Extensions: misc.ArrToMap("toml"), + Validator: validator.TomlValidator{}, } // Instance of FileType object to // represent a Ini file var IniFileType = FileType{ - "ini", - misc.ArrToMap("ini"), - validator.IniValidator{}, + Name: "ini", + Extensions: misc.ArrToMap("ini"), + Validator: validator.IniValidator{}, } // Instance of FileType object to // represent a Properties file var PropFileType = FileType{ - "properties", - misc.ArrToMap("properties"), - validator.PropValidator{}, + Name: "properties", + Extensions: misc.ArrToMap("properties"), + Validator: validator.PropValidator{}, } // Instance of the FileType object to // represent a HCL file var HclFileType = FileType{ - "hcl", - misc.ArrToMap("hcl"), - validator.HclValidator{}, + Name: "hcl", + Extensions: misc.ArrToMap("hcl", ".tftpl"), + Validator: validator.HclValidator{}, } // Instance of the FileType object to // represent a Plist file var PlistFileType = FileType{ - "plist", - misc.ArrToMap("plist"), - validator.PlistValidator{}, + Name: "plist", + Extensions: misc.ArrToMap("plist"), + Validator: validator.PlistValidator{}, } // Instance of the FileType object to // represent a CSV file var CsvFileType = FileType{ - "csv", - misc.ArrToMap("csv"), - validator.CsvValidator{}, + Name: "csv", + Extensions: misc.ArrToMap("csv"), + Validator: validator.CsvValidator{}, } // Instance of the FileType object to // represent a HOCON file var HoconFileType = FileType{ - "hocon", - misc.ArrToMap("hocon"), - validator.HoconValidator{}, + Name: "hocon", + Extensions: misc.ArrToMap("hocon"), + Validator: validator.HoconValidator{}, } // Instance of the FileType object to // represent a ENV file var EnvFileType = FileType{ - "env", - misc.ArrToMap("env"), - validator.EnvValidator{}, + Name: "env", + Extensions: misc.ArrToMap("env"), + Validator: validator.EnvValidator{}, } // An array of files types that are supported From 7cab4c9f4d55fe62baa8a6b6ad30aad05a0b71c0 Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Tue, 9 Jul 2024 23:56:58 +0200 Subject: [PATCH 2/2] feat: add support for well known files Related to #135 --- pkg/filetype/file_type.go | 2 ++ pkg/finder/finder_test.go | 24 ++++++++++++++++++++++++ pkg/finder/fsfinder.go | 8 ++++++++ test/fixtures/.editorconfig | 6 ++++++ 4 files changed, 40 insertions(+) create mode 100644 test/fixtures/.editorconfig diff --git a/pkg/filetype/file_type.go b/pkg/filetype/file_type.go index cc2ac0f2..d8fc0565 100644 --- a/pkg/filetype/file_type.go +++ b/pkg/filetype/file_type.go @@ -12,6 +12,7 @@ import ( type FileType struct { Name string Extensions map[string]struct{} + KnownFiles map[string]struct{} Validator validator.Validator } @@ -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{}, } diff --git a/pkg/finder/finder_test.go b/pkg/finder/finder_test.go index 4b5b8242..3dfe5f81 100644 --- a/pkg/finder/finder_test.go +++ b/pkg/finder/finder_test.go @@ -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{}, + } + 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"), diff --git a/pkg/finder/fsfinder.go b/pkg/finder/fsfinder.go index 01a81f37..1c988d85 100644 --- a/pkg/finder/fsfinder.go +++ b/pkg/finder/fsfinder.go @@ -133,6 +133,7 @@ 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 { @@ -140,6 +141,13 @@ func (fsf FileSystemFinder) findOne(pathRoot string) ([]FileMetadata, error) { } 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) diff --git a/test/fixtures/.editorconfig b/test/fixtures/.editorconfig new file mode 100644 index 00000000..1e2c5c9e --- /dev/null +++ b/test/fixtures/.editorconfig @@ -0,0 +1,6 @@ +# https://editorconfig.org +root = true + +[*] +charset = utf-8 +end_of_line = lf \ No newline at end of file