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

40 lines
649 B
Go
Raw Normal View History

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
}