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/internal/walk/walker.go
Brian McGee 5711caebb9 feat: support .gitignore files (#19)
Introduces a `--walk` flag which can be used to tell `treefmt` how to traverse the directory specified by `--tree-root`.

By default, it will attempt to use `git ls-files`. If this fails, it falls back to using the filesystem.

You can explicitly traverse the filesystem instead of using git by providing `--walk filesystem`.

Close #1

Reviewed-on: #19
Reviewed-by: Jonas Chevalier <zimbatm@noreply.git.numtide.com>
Co-authored-by: Brian McGee <brian@bmcgee.ie>
Co-committed-by: Brian McGee <brian@bmcgee.ie>
2024-01-11 20:52:22 +00:00

43 lines
743 B
Go

package walk
import (
"context"
"fmt"
"path/filepath"
)
type Type string
const (
Git Type = "git"
Auto Type = "auto"
Filesystem Type = "filesystem"
)
type Walker interface {
Root() string
Walk(ctx context.Context, fn filepath.WalkFunc) error
}
func New(walkerType Type, root string) (Walker, error) {
switch walkerType {
case Git:
return NewGit(root)
case Auto:
return Detect(root)
case Filesystem:
return NewFilesystem(root)
default:
return nil, fmt.Errorf("unknown walker type: %v", walkerType)
}
}
func Detect(root string) (Walker, error) {
// for now, we keep it simple and try git first, filesystem second
w, err := NewGit(root)
if err == nil {
return w, err
}
return NewFilesystem(root)
}