export Pattern and NewPattern
This commit is contained in:
parent
bf173a7a21
commit
5f017796f5
@ -7,20 +7,21 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pattern interface {
|
type Pattern interface {
|
||||||
Match(filename string) bool
|
Match(filename string) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
include []pattern
|
include []Pattern
|
||||||
exclude []pattern
|
exclude []Pattern
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFromPatterns(include, exclude []Pattern) *Filter {
|
||||||
|
return &Filter{include: include, exclude: exclude}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(include, exclude []string) *Filter {
|
func New(include, exclude []string) *Filter {
|
||||||
return &Filter{
|
return NewFromPatterns(convertToPatterns(include), convertToPatterns(exclude))
|
||||||
include: convertToPatterns(include),
|
|
||||||
exclude: convertToPatterns(exclude),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filter) Allows(filename string) bool {
|
func (f *Filter) Allows(filename string) bool {
|
||||||
@ -58,7 +59,7 @@ func (f *Filter) Allows(filename string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPattern(rawpattern string) pattern {
|
func NewPattern(rawpattern string) Pattern {
|
||||||
cleanpattern := filepath.Clean(rawpattern)
|
cleanpattern := filepath.Clean(rawpattern)
|
||||||
|
|
||||||
// Special case local dir, matches all (inc subpaths)
|
// Special case local dir, matches all (inc subpaths)
|
||||||
@ -94,10 +95,10 @@ func newPattern(rawpattern string) pattern {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertToPatterns(rawpatterns []string) []pattern {
|
func convertToPatterns(rawpatterns []string) []Pattern {
|
||||||
patterns := make([]pattern, len(rawpatterns))
|
patterns := make([]Pattern, len(rawpatterns))
|
||||||
for i, raw := range rawpatterns {
|
for i, raw := range rawpatterns {
|
||||||
patterns[i] = newPattern(raw)
|
patterns[i] = NewPattern(raw)
|
||||||
}
|
}
|
||||||
return patterns
|
return patterns
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ func TestPatternMatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func patternMatch(pattern, filename string) bool {
|
func patternMatch(pattern, filename string) bool {
|
||||||
return newPattern(pattern).Match(filepath.Clean(filename))
|
return NewPattern(pattern).Match(filepath.Clean(filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
type filterTest struct {
|
type filterTest struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user