feature: added sub file finders

master
amorozov 2024-06-29 23:48:26 +03:00
parent 7c0e31aec7
commit 1fe9bae942
1 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package files
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"git.tswf.io/incredible-go/incredible-go-core/pkg/collections/array_list"
)
type FileFilter func(fileInfo os.FileInfo, fileName string) bool
func ListFiles(dirPath string) (*array_list.ArrayList[os.FileInfo], error) {
dirInfo, err := os.Stat(dirPath)
if err != nil {
return nil, err
}
if !dirInfo.IsDir() {
return nil, fmt.Errorf("'%s' is not directory", dirPath)
}
dirEntries, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}
result := array_list.NewArrayList[os.FileInfo]()
if len(dirEntries) > 0 {
for _, dirEntry := range dirEntries {
dirEntryFileInfo, err := dirEntry.Info()
if err != nil {
return nil, err
}
result.Add(dirEntryFileInfo)
}
}
return result, nil
}
func FindSubfilesByFilter(rootPath string, filter FileFilter) (*array_list.ArrayList[os.FileInfo], error) {
result := array_list.NewArrayList[os.FileInfo]()
err := filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
if err != nil && filter(info, path) {
result.Add(info)
}
return nil
})
if err != nil {
return nil, err
}
return result, nil
}