59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
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) {
|
|
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]()
|
|
|
|
if len(dirEntries) > 0 {
|
|
for _, dirEntry := range dirEntries {
|
|
result.Add(dirEntry.Name())
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func FindSubFilesByFilter(rootPath string, filter FileFilter) (*array_list.ArrayList[string], error) {
|
|
result := array_list.NewArrayList[string]()
|
|
|
|
err := filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
|
|
if err == nil && filter(info, path) {
|
|
result.Add(path)
|
|
}
|
|
|
|
return err
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|