54 lines
893 B
Go
54 lines
893 B
Go
package walk
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
|
|
"github.com/opencoff/go-walk"
|
|
)
|
|
|
|
type filesystemWalker struct {
|
|
root string
|
|
paths []string
|
|
}
|
|
|
|
func (f filesystemWalker) Root() string {
|
|
return f.root
|
|
}
|
|
|
|
func (f filesystemWalker) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
|
walkOpts := walk.Options{
|
|
OneFS: true,
|
|
Type: walk.FILE,
|
|
FollowSymlinks: false,
|
|
}
|
|
|
|
names := f.paths
|
|
if len(names) == 0 {
|
|
names = []string{f.root}
|
|
}
|
|
|
|
ch, errCh := walk.Walk(names, &walkOpts)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case err := <-errCh:
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case file, ok := <-ch:
|
|
if !ok {
|
|
return nil
|
|
} else if err := fn(file.Path, file.Stat, nil); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewFilesystem(root string, paths []string) (Walker, error) {
|
|
return filesystemWalker{root, paths}, nil
|
|
}
|