cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.26.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/glob.go

84lines · modecode

1package discovery
2
3import (
4 "fmt"
5 "io/fs"
6 "os"
7 "path/filepath"
8 "regexp"
9)
10
11func NewGlobFinder(patterns []string, relaxed []*regexp.Regexp) GlobFinder {
12 return GlobFinder{
13 patterns: patterns,
14 relaxed: relaxed,
15 }
16}
17
18type GlobFinder struct {
19 patterns []string
20 relaxed []*regexp.Regexp
21}
22
23func (f GlobFinder) Find() (entries []Entry, err error) {
24 paths := []string{}
25 for _, p := range f.patterns {
26 matches, err := filepath.Glob(p)
27 if err != nil {
28 return nil, err
29 }
30
31 for _, path := range matches {
32 s, err := os.Stat(path)
33 if err != nil {
34 return nil, err
35 }
36 if s.IsDir() {
37 subpaths, err := walkDir(path)
38 if err != nil {
39 return nil, err
40 }
41 paths = append(paths, subpaths...)
42 } else {
43 paths = append(paths, path)
44 }
45 }
46 }
47
48 if len(paths) == 0 {
49 return nil, fmt.Errorf("no matching files")
50 }
51
52 for _, path := range paths {
53 el, err := readFile(path, !matchesAny(f.relaxed, path))
54 if err != nil {
55 return nil, fmt.Errorf("invalid file syntax: %w", err)
56 }
57 for _, e := range el {
58 if len(e.ModifiedLines) == 0 {
59 e.ModifiedLines = e.Rule.Lines()
60 }
61 entries = append(entries, e)
62 }
63 }
64
65 return entries, nil
66}
67
68func walkDir(dirname string) (paths []string, err error) {
69 err = filepath.WalkDir(dirname,
70 func(path string, d fs.DirEntry, err error) error {
71 if err != nil {
72 return err
73 }
74
75 if d.IsDir() {
76 return nil
77 }
78
79 paths = append(paths, path)
80 return nil
81 })
82
83 return
84}
85