This repository has been archived on 2024-05-03. You can view files and clone it, but cannot push or open issues or pull requests.
treefmt/walk/filesystem.go
Brian McGee 6d9ce65f6c
feat: use go-walk
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-02-28 09:40:54 +00:00

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
}