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

33 lines
615 B
Go
Raw Permalink Normal View History

2023-12-23 12:50:47 +00:00
package format
import (
"fmt"
2023-12-23 12:50:47 +00:00
"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("failed to compile include pattern '%v': %w", pattern, err)
}
globs[i] = g
}
return globs, nil
}
2023-12-23 12:50:47 +00:00
func PathMatches(path string, globs []glob.Glob) bool {
for idx := range globs {
if globs[idx].Match(path) {
return true
}
}
return false
}