fix: glob patterns for includes and excludes

Prefix with **/
This commit is contained in:
Brian McGee 2023-12-23 13:20:11 +00:00
parent 12452b01b6
commit 12aa9a7bef
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0

View File

@ -3,7 +3,6 @@ package format
import ( import (
"context" "context"
"os/exec" "os/exec"
"strings"
"time" "time"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
@ -44,10 +43,7 @@ func (f *Formatter) Init(name string) error {
// todo refactor common code below // todo refactor common code below
if len(f.Includes) > 0 { if len(f.Includes) > 0 {
for _, pattern := range f.Includes { for _, pattern := range f.Includes {
if !strings.Contains(pattern, "/") { g, err := glob.Compile("**/" + pattern)
pattern = "**/" + pattern
}
g, err := glob.Compile(pattern)
if err != nil { if err != nil {
return errors.Annotatef(err, "failed to compile include pattern '%v' for formatter '%v'", pattern, f.Name) return errors.Annotatef(err, "failed to compile include pattern '%v' for formatter '%v'", pattern, f.Name)
} }
@ -57,10 +53,7 @@ func (f *Formatter) Init(name string) error {
if len(f.Excludes) > 0 { if len(f.Excludes) > 0 {
for _, pattern := range f.Excludes { for _, pattern := range f.Excludes {
if !strings.Contains(pattern, "/") { g, err := glob.Compile("**/" + pattern)
pattern = "**/" + pattern
}
g, err := glob.Compile(pattern)
if err != nil { if err != nil {
return errors.Annotatef(err, "failed to compile exclude pattern '%v' for formatter '%v'", pattern, f.Name) return errors.Annotatef(err, "failed to compile exclude pattern '%v' for formatter '%v'", pattern, f.Name)
} }
@ -72,10 +65,11 @@ func (f *Formatter) Init(name string) error {
} }
func (f *Formatter) Wants(path string) bool { func (f *Formatter) Wants(path string) bool {
if PathMatches(path, f.excludes) { match := !PathMatches(path, f.excludes) && PathMatches(path, f.includes)
return false if match {
f.log.Debugf("match: %v", path)
} }
return PathMatches(path, f.includes) return match
} }
func (f *Formatter) Put(path string) { func (f *Formatter) Put(path string) {