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 d4ab015bc6
chore: remove internal directory
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-02-15 13:59:55 +00:00

40 lines
649 B
Go

package walk
import (
"context"
"os"
"path/filepath"
)
type filesystemWalker struct {
root string
paths []string
}
func (f filesystemWalker) Root() string {
return f.root
}
func (f filesystemWalker) Walk(_ context.Context, fn filepath.WalkFunc) error {
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
}
func NewFilesystem(root string, paths []string) (Walker, error) {
return filesystemWalker{root, paths}, nil
}