cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.17.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/glob.go

82lines · 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 e.ModifiedLines = e.Rule.Lines()
59 entries = append(entries, e)
60 }
61 }
62
63 return entries, nil
64}
65
66func walkDir(dirname string) (paths []string, err error) {
67 err = filepath.WalkDir(dirname,
68 func(path string, d fs.DirEntry, err error) error {
69 if err != nil {
70 return err
71 }
72
73 if d.IsDir() {
74 return nil
75 }
76
77 paths = append(paths, path)
78 return nil
79 })
80
81 return
82}
83