]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/files.go
Replace deprecated ioutil
[rbdr/mobius] / hotline / files.go
index dc196536d8b7abfc870c07f96ecb0dbc2a48eef5..1eeef593b8536709708932245264da992a7d38aa 100644 (file)
@@ -4,9 +4,9 @@ import (
        "encoding/binary"
        "errors"
        "io/fs"
        "encoding/binary"
        "errors"
        "io/fs"
-       "io/ioutil"
        "os"
        "path/filepath"
        "os"
        "path/filepath"
+       "regexp"
        "strings"
 )
 
        "strings"
 )
 
@@ -30,7 +30,7 @@ func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) {
        return ft, nil
 }
 
        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
        files, err := os.ReadDir(path)
        if err != nil {
                return fields, nil
@@ -39,7 +39,7 @@ func getFileNameList(path string) (fields []Field, err error) {
        for _, file := range files {
                var fnwi FileNameWithInfo
 
        for _, file := range files {
                var fnwi FileNameWithInfo
 
-               if strings.HasPrefix(file.Name(), ".") {
+               if ignoreFile(file.Name(), ignoreList) {
                        continue
                }
 
                        continue
                }
 
@@ -65,14 +65,14 @@ func getFileNameList(path string) (fields []Field, err error) {
                        }
 
                        if rFile.IsDir() {
                        }
 
                        if rFile.IsDir() {
-                               dir, err := ioutil.ReadDir(filepath.Join(path, file.Name()))
+                               dir, err := os.ReadDir(filepath.Join(path, file.Name()))
                                if err != nil {
                                        return fields, err
                                }
 
                                var c uint32
                                for _, f := range dir {
                                if err != nil {
                                        return fields, err
                                }
 
                                var c uint32
                                for _, f := range dir {
-                                       if !strings.HasPrefix(f.Name(), ".") {
+                                       if !ignoreFile(f.Name(), ignoreList) {
                                                c += 1
                                        }
                                }
                                                c += 1
                                        }
                                }
@@ -87,14 +87,14 @@ func getFileNameList(path string) (fields []Field, err error) {
                        }
 
                } else if file.IsDir() {
                        }
 
                } else if file.IsDir() {
-                       dir, err := ioutil.ReadDir(filepath.Join(path, file.Name()))
+                       dir, err := os.ReadDir(filepath.Join(path, file.Name()))
                        if err != nil {
                                return fields, err
                        }
 
                        var c uint32
                        for _, f := range dir {
                        if err != nil {
                                return fields, err
                        }
 
                        var c uint32
                        for _, f := range dir {
-                               if !strings.HasPrefix(f.Name(), ".") {
+                               if !ignoreFile(f.Name(), ignoreList) {
                                        c += 1
                                }
                        }
                                        c += 1
                                }
                        }
@@ -130,7 +130,7 @@ func getFileNameList(path string) (fields []Field, err error) {
                if err != nil {
                        return nil, err
                }
                if err != nil {
                        return nil, err
                }
-               fields = append(fields, NewField(fieldFileNameWithInfo, b))
+               fields = append(fields, NewField(FieldFileNameWithInfo, b))
        }
 
        return fields, nil
        }
 
        return fields, nil
@@ -201,3 +201,14 @@ func EncodeFilePath(filePath string) []byte {
 
        return bytes
 }
 
        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
+}