goldsmith/filters/wildcard/wildcard.go

51 lines
912 B
Go
Raw Normal View History

2024-02-17 06:35:49 +00:00
package wildcard
import (
2024-04-09 17:34:51 +00:00
"path/filepath"
2024-02-17 06:35:49 +00:00
"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 {
2024-04-09 17:34:51 +00:00
filePath := self.adjustPath(file.Path())
2024-02-17 06:35:49 +00:00
for _, wildcard := range self.wildcards {
2024-04-09 17:34:51 +00:00
wildcard = self.adjustPath(wildcard)
2024-02-17 06:35:49 +00:00
if matched, _ := doublestar.PathMatch(wildcard, filePath); matched {
return true
}
}
return false
}
2024-04-09 17:34:51 +00:00
func (self *Wildcard) adjustPath(str string) string {
str = filepath.FromSlash(str)
2024-02-17 06:35:49 +00:00
if self.caseSensitive {
return str
}
return strings.ToLower(str)
}