cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/discovery.go
112lines · modecode
| 1 | package discovery |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/prometheus/prometheus/model/rulefmt" |
| 9 | "gopkg.in/yaml.v3" |
| 10 | |
| 11 | "github.com/cloudflare/pint/internal/output" |
| 12 | "github.com/cloudflare/pint/internal/parser" |
| 13 | |
| 14 | "github.com/rs/zerolog/log" |
| 15 | ) |
| 16 | |
| 17 | const ( |
| 18 | FileOwnerComment = "file/owner" |
| 19 | RuleOwnerComment = "rule/owner" |
| 20 | ) |
| 21 | |
| 22 | type RuleFinder interface { |
| 23 | Find() ([]Entry, error) |
| 24 | } |
| 25 | |
| 26 | type Entry struct { |
| 27 | Path string |
| 28 | PathError error |
| 29 | ModifiedLines []int |
| 30 | Rule parser.Rule |
| 31 | Owner string |
| 32 | } |
| 33 | |
| 34 | func readFile(path string, isStrict bool) (entries []Entry, err error) { |
| 35 | p := parser.NewParser() |
| 36 | |
| 37 | f, err := os.Open(path) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | content, err := parser.ReadContent(f) |
| 43 | f.Close() |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | contentLines := []int{} |
| 49 | for i := 1; i <= strings.Count(string(content), "\n"); i++ { |
| 50 | contentLines = append(contentLines, i) |
| 51 | } |
| 52 | |
| 53 | fileOwner, _ := parser.GetComment(string(content), FileOwnerComment) |
| 54 | |
| 55 | if isStrict { |
| 56 | var r rulefmt.RuleGroups |
| 57 | if err = yaml.Unmarshal(content, &r); err != nil { |
| 58 | log.Error(). |
| 59 | Err(err). |
| 60 | Str("path", path). |
| 61 | Str("lines", output.FormatLineRangeString(contentLines)). |
| 62 | Msg("Failed to unmarshal file content") |
| 63 | entries = append(entries, Entry{ |
| 64 | Path: path, |
| 65 | PathError: err, |
| 66 | Owner: fileOwner.Value, |
| 67 | ModifiedLines: contentLines, |
| 68 | }) |
| 69 | return entries, nil |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | rules, err := p.Parse(content) |
| 74 | if err != nil { |
| 75 | log.Error(). |
| 76 | Err(err). |
| 77 | Str("path", path). |
| 78 | Str("lines", output.FormatLineRangeString(contentLines)). |
| 79 | Msg("Failed to parse file content") |
| 80 | entries = append(entries, Entry{ |
| 81 | Path: path, |
| 82 | PathError: err, |
| 83 | Owner: fileOwner.Value, |
| 84 | ModifiedLines: contentLines, |
| 85 | }) |
| 86 | return entries, nil |
| 87 | } |
| 88 | |
| 89 | for _, rule := range rules { |
| 90 | owner, ok := rule.GetComment(RuleOwnerComment) |
| 91 | if !ok { |
| 92 | owner = fileOwner |
| 93 | } |
| 94 | entries = append(entries, Entry{ |
| 95 | Path: path, |
| 96 | Rule: rule, |
| 97 | Owner: owner.Value, |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | log.Info().Str("path", path).Int("rules", len(entries)).Msg("File parsed") |
| 102 | return entries, nil |
| 103 | } |
| 104 | |
| 105 | func matchesAny(re []*regexp.Regexp, s string) bool { |
| 106 | for _, r := range re { |
| 107 | if v := r.MatchString(s); v { |
| 108 | return true |
| 109 | } |
| 110 | } |
| 111 | return false |
| 112 | } |
| 113 | |