cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/git/filter.go
46lines · modecode
| 1 | package git |
| 2 | |
| 3 | import "regexp" |
| 4 | |
| 5 | func NewPathFilter(include, exclude, relaxed []*regexp.Regexp) PathFilter { |
| 6 | return PathFilter{ |
| 7 | include: include, |
| 8 | exclude: exclude, |
| 9 | relaxed: relaxed, |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | type PathFilter struct { |
| 14 | include []*regexp.Regexp |
| 15 | exclude []*regexp.Regexp |
| 16 | relaxed []*regexp.Regexp |
| 17 | } |
| 18 | |
| 19 | func (pf PathFilter) IsPathAllowed(path string) bool { |
| 20 | if len(pf.include) == 0 && len(pf.exclude) == 0 { |
| 21 | return true |
| 22 | } |
| 23 | |
| 24 | for _, pattern := range pf.exclude { |
| 25 | if pattern.MatchString(path) { |
| 26 | return false |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | for _, pattern := range pf.include { |
| 31 | if pattern.MatchString(path) { |
| 32 | return true |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return false |
| 37 | } |
| 38 | |
| 39 | func (pf PathFilter) IsRelaxed(path string) bool { |
| 40 | for _, r := range pf.relaxed { |
| 41 | if v := r.MatchString(path); v { |
| 42 | return true |
| 43 | } |
| 44 | } |
| 45 | return false |
| 46 | } |
| 47 | |