2024-01-11 23:52:22 +03:00
|
|
|
package walk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-10 18:45:57 +03:00
|
|
|
"os"
|
2024-01-11 23:52:22 +03:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2024-01-12 14:33:14 +03:00
|
|
|
type filesystemWalker struct {
|
2024-01-10 18:45:57 +03:00
|
|
|
root string
|
|
|
|
paths []string
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
|
|
|
|
2024-01-12 14:33:14 +03:00
|
|
|
func (f filesystemWalker) Root() string {
|
2024-01-11 23:52:22 +03:00
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:33:14 +03:00
|
|
|
func (f filesystemWalker) Walk(_ context.Context, fn filepath.WalkFunc) error {
|
2024-01-10 18:45:57 +03:00
|
|
|
if len(f.paths) == 0 {
|
|
|
|
return filepath.Walk(f.root, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range f.paths {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err = filepath.Walk(path, fn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fn(path, info, err); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
|
|
|
|
2024-01-10 18:45:57 +03:00
|
|
|
func NewFilesystem(root string, paths []string) (Walker, error) {
|
|
|
|
return filesystemWalker{root, paths}, nil
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|