cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/glob.go
56lines · modecode
| 1 | package discovery |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | ) |
| 7 | |
| 8 | func NewGlobFileFinder() GlobFileFinder { |
| 9 | return GlobFileFinder{} |
| 10 | } |
| 11 | |
| 12 | type GlobFileFinder struct { |
| 13 | } |
| 14 | |
| 15 | func (gd GlobFileFinder) Find(pattern ...string) (FileFindResults, error) { |
| 16 | results := FileCommits{ |
| 17 | pathCommits: map[string][]string{}, |
| 18 | } |
| 19 | |
| 20 | for _, p := range pattern { |
| 21 | matches, err := filepath.Glob(p) |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | |
| 26 | for _, path := range matches { |
| 27 | s, err := os.Stat(path) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | if s.IsDir() { |
| 32 | err = filepath.Walk(path, |
| 33 | func(path string, info os.FileInfo, err error) error { |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | if info.IsDir() { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | results.pathCommits[path] = nil |
| 43 | |
| 44 | return nil |
| 45 | }) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | } else { |
| 50 | results.pathCommits[path] = nil |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return results, nil |
| 56 | } |
| 57 | |