cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.76.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/git/filter.go

66lines · modecode

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