goldsmith/filters/wildcard/wildcard.go
2024-04-09 10:34:51 -07:00

51 lines
912 B
Go

package wildcard
import (
"path/filepath"
"strings"
"git.foosoft.net/alex/goldsmith"
"github.com/bmatcuk/doublestar/v4"
)
type Wildcard struct {
wildcards []string
caseSensitive bool
}
func New(wildcards ...string) *Wildcard {
return &Wildcard{wildcards: wildcards}
}
func (self *Wildcard) CaseSensitive(caseSensitive bool) *Wildcard {
self.caseSensitive = caseSensitive
return self
}
func (*Wildcard) Name() string {
return "wildcard"
}
func (self *Wildcard) Accept(file *goldsmith.File) bool {
filePath := self.adjustPath(file.Path())
for _, wildcard := range self.wildcards {
wildcard = self.adjustPath(wildcard)
if matched, _ := doublestar.PathMatch(wildcard, filePath); matched {
return true
}
}
return false
}
func (self *Wildcard) adjustPath(str string) string {
str = filepath.FromSlash(str)
if self.caseSensitive {
return str
}
return strings.ToLower(str)
}