cloudflare/pint

Public

mirrored fromhttps://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.75.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/git/filter.go

65lines · modecode

1package git
2
3import (
4 "log/slog"
5 "regexp"
6)
7
8func NewPathFilter(include, exclude, relaxed []*regexp.Regexp) PathFilter {
9 return PathFilter{
10 include: include,
11 exclude: exclude,
12 relaxed: relaxed,
13 }
14}
15
16type PathFilter struct {
17 include []*regexp.Regexp
18 exclude []*regexp.Regexp
19 relaxed []*regexp.Regexp
20}
21
22func (pf PathFilter) IsPathAllowed(path string) (ok bool) {
23 if len(pf.include) == 0 && len(pf.exclude) == 0 {
24 return true
25 }
26
27 for _, pattern := range pf.exclude {
28 if pattern.MatchString(path) {
29 slog.Debug("File path is in the exclude list",
30 slog.String("path", path),
31 slog.Any("exclude", pf.exclude),
32 )
33 return false
34 }
35 }
36
37 for _, pattern := range pf.include {
38 if pattern.MatchString(path) {
39 slog.Debug("File path is in the include list",
40 slog.String("path", path),
41 slog.Any("include", pf.include),
42 )
43 return true
44 }
45 }
46
47 ok = len(pf.include) == 0
48 if !ok {
49 slog.Debug("File path is not allowed",
50 slog.String("path", path),
51 slog.Any("include", pf.include),
52 slog.Any("exclude", pf.exclude),
53 )
54 }
55 return ok
56}
57
58func (pf PathFilter) IsRelaxed(path string) bool {
59 for _, r := range pf.relaxed {
60 if v := r.MatchString(path); v {
61 return true
62 }
63 }
64 return false
65}
66