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/format/glob.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

33 lines
615 B
Go

package format
import (
"fmt"
"github.com/gobwas/glob"
)
// CompileGlobs prepares the globs, where the patterns are all right-matching.
func CompileGlobs(patterns []string) ([]glob.Glob, error) {
globs := make([]glob.Glob, len(patterns))
for i, pattern := range patterns {
g, err := glob.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("%w: failed to compile include pattern '%v'", err, pattern)
}
globs[i] = g
}
return globs, nil
}
func PathMatches(path string, globs []glob.Glob) bool {
for idx := range globs {
if globs[idx].Match(path) {
return true
}
}
return false
}