cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.16.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/discovery.go

68lines · modecode

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