Skip to content

Commit

Permalink
Add PatternSet
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 12, 2019
1 parent 60cb5ff commit ac412b6
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/chezmoi/pattern_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package chezmoi

import "path/filepath"

// An PatternSet is a set of patterns.
type PatternSet map[string]struct{}

// NewPatternSet returns a new PatternSet.
func NewPatternSet() PatternSet {
return PatternSet(make(map[string]struct{}))
}

// Add adds pattern to ps.
func (ps PatternSet) Add(pattern string) error {
if _, err := filepath.Match(pattern, ""); err != nil {
return nil
}
ps[pattern] = struct{}{}
return nil
}

// Match returns if name matches any pattern in ps.
func (ps PatternSet) Match(name string) bool {
for pattern := range ps {
if ok, _ := filepath.Match(pattern, name); ok {
return true
}
}
return false
}

0 comments on commit ac412b6

Please sign in to comment.