From b8c0a83a87eb108e8dfe02fd08c234087f166f04 Mon Sep 17 00:00:00 2001 From: Jeff Halter <868228+jhalter@users.noreply.github.com> Date: Sat, 25 Jun 2022 20:51:16 -0700 Subject: Add config option to specify ignored filename patterns --- hotline/files.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'hotline/files.go') diff --git a/hotline/files.go b/hotline/files.go index dc19653..aa248f8 100644 --- a/hotline/files.go +++ b/hotline/files.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "strings" ) @@ -30,7 +31,7 @@ func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) { return ft, nil } -func getFileNameList(path string) (fields []Field, err error) { +func getFileNameList(path string, ignoreList []string) (fields []Field, err error) { files, err := os.ReadDir(path) if err != nil { return fields, nil @@ -39,7 +40,7 @@ func getFileNameList(path string) (fields []Field, err error) { for _, file := range files { var fnwi FileNameWithInfo - if strings.HasPrefix(file.Name(), ".") { + if ignoreFile(file.Name(), ignoreList) { continue } @@ -72,7 +73,7 @@ func getFileNameList(path string) (fields []Field, err error) { var c uint32 for _, f := range dir { - if !strings.HasPrefix(f.Name(), ".") { + if !ignoreFile(f.Name(), ignoreList) { c += 1 } } @@ -94,7 +95,7 @@ func getFileNameList(path string) (fields []Field, err error) { var c uint32 for _, f := range dir { - if !strings.HasPrefix(f.Name(), ".") { + if !ignoreFile(f.Name(), ignoreList) { c += 1 } } @@ -201,3 +202,14 @@ func EncodeFilePath(filePath string) []byte { return bytes } + +func ignoreFile(fileName string, ignoreList []string) bool { + // skip files that match any regular expression present in the IgnoreFiles list + matchIgnoreFilter := 0 + for _, pattern := range ignoreList { + if match, _ := regexp.MatchString(pattern, fileName); match { + matchIgnoreFilter += 1 + } + } + return matchIgnoreFilter > 0 +} -- cgit