incredible-go-core/pkg/io/files/subfile_finder.go

59 lines
1.0 KiB
Go
Raw Normal View History

2024-06-29 22:48:26 +02:00
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[string], error) {
2024-06-29 22:48:26 +02:00
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[string]()
2024-06-29 22:48:26 +02:00
if len(dirEntries) > 0 {
for _, dirEntry := range dirEntries {
result.Add(dirEntry.Name())
2024-06-29 22:48:26 +02:00
}
}
return result, nil
}
func FindSubFilesByFilter(rootPath string, filter FileFilter) (*array_list.ArrayList[string], error) {
result := array_list.NewArrayList[string]()
2024-06-29 22:48:26 +02:00
err := filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
if err == nil && filter(info, path) {
result.Add(path)
2024-06-29 22:48:26 +02:00
}
return err
2024-06-29 22:48:26 +02:00
})
if err != nil {
return nil, err
}
return result, nil
}