cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.15.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/glob.go

76lines · modecode

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