Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support known files #156

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions pkg/filetype/file_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,95 +12,97 @@ import (
type FileType struct {
Name string
Extensions map[string]struct{}
KnownFiles map[string]struct{}
Validator validator.Validator
}

// 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"),
KnownFiles: misc.ArrToMap(".editorconfig", ".gitconfig", ".gitmodules", ".shellcheckrc", ".npmrc", "inputrc", ".inputrc", ".wgetrc", ".curlrc", ".nanorc"),
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
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{},
}
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
Loading