cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/discovery.go
55lines · modecode
| 1 | package discovery |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | |
| 6 | "github.com/cloudflare/pint/internal/parser" |
| 7 | |
| 8 | "github.com/rs/zerolog/log" |
| 9 | ) |
| 10 | |
| 11 | type RuleFinder interface { |
| 12 | Find() ([]Entry, error) |
| 13 | } |
| 14 | |
| 15 | type Entry struct { |
| 16 | Path string |
| 17 | PathError error |
| 18 | ModifiedLines []int |
| 19 | Rule parser.Rule |
| 20 | } |
| 21 | |
| 22 | func readFile(path string) (entries []Entry, err error) { |
| 23 | p := parser.NewParser() |
| 24 | |
| 25 | f, err := os.Open(path) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | |
| 30 | content, err := parser.ReadContent(f) |
| 31 | f.Close() |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | |
| 36 | rules, err := p.Parse(content) |
| 37 | if err != nil { |
| 38 | log.Error().Str("path", path).Err(err).Msg("Failed to parse file content") |
| 39 | entries = append(entries, Entry{ |
| 40 | Path: path, |
| 41 | PathError: err, |
| 42 | }) |
| 43 | return entries, nil |
| 44 | } |
| 45 | |
| 46 | for _, rule := range rules { |
| 47 | entries = append(entries, Entry{ |
| 48 | Path: path, |
| 49 | Rule: rule, |
| 50 | }) |
| 51 | } |
| 52 | |
| 53 | log.Info().Str("path", path).Int("rules", len(entries)).Msg("File parsed") |
| 54 | return entries, nil |
| 55 | } |