2023-12-23 15:50:47 +03:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
2024-01-02 15:12:47 +03:00
|
|
|
"fmt"
|
|
|
|
|
2023-12-23 15:50:47 +03:00
|
|
|
"github.com/gobwas/glob"
|
|
|
|
)
|
|
|
|
|
2024-01-02 15:12:47 +03:00
|
|
|
// 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 {
|
2024-01-11 23:52:22 +03:00
|
|
|
g, err := glob.Compile(pattern)
|
2024-01-02 15:12:47 +03:00
|
|
|
if err != nil {
|
2024-05-02 13:40:49 +03:00
|
|
|
return nil, fmt.Errorf("failed to compile include pattern '%v': %w", pattern, err)
|
2024-01-02 15:12:47 +03:00
|
|
|
}
|
|
|
|
globs[i] = g
|
|
|
|
}
|
|
|
|
|
|
|
|
return globs, nil
|
|
|
|
}
|
|
|
|
|
2023-12-23 15:50:47 +03:00
|
|
|
func PathMatches(path string, globs []glob.Glob) bool {
|
|
|
|
for idx := range globs {
|
|
|
|
if globs[idx].Match(path) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|