cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/checks/base.go
102lines · modecode
| 1 | package checks |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/cloudflare/pint/internal/parser" |
| 8 | ) |
| 9 | |
| 10 | var ( |
| 11 | CheckNames = []string{ |
| 12 | AnnotationCheckName, |
| 13 | AlertsCheckName, |
| 14 | AlertForCheckName, |
| 15 | TemplateCheckName, |
| 16 | AggregationCheckName, |
| 17 | ComparisonCheckName, |
| 18 | RateCheckName, |
| 19 | SyntaxCheckName, |
| 20 | VectorMatchingCheckName, |
| 21 | CostCheckName, |
| 22 | SeriesCheckName, |
| 23 | LabelCheckName, |
| 24 | RejectCheckName, |
| 25 | } |
| 26 | OnlineChecks = []string{ |
| 27 | AlertsCheckName, |
| 28 | RateCheckName, |
| 29 | VectorMatchingCheckName, |
| 30 | CostCheckName, |
| 31 | SeriesCheckName, |
| 32 | } |
| 33 | ) |
| 34 | |
| 35 | // Severity of the problem reported |
| 36 | type Severity int |
| 37 | |
| 38 | func (s Severity) String() string { |
| 39 | switch s { |
| 40 | case Fatal: |
| 41 | return "Fatal" |
| 42 | case Information: |
| 43 | return "Information" |
| 44 | case Warning: |
| 45 | return "Warning" |
| 46 | default: |
| 47 | return "Bug" |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func ParseSeverity(s string) (Severity, error) { |
| 52 | switch s { |
| 53 | case "fatal": |
| 54 | return Fatal, nil |
| 55 | case "bug": |
| 56 | return Bug, nil |
| 57 | case "info": |
| 58 | return Information, nil |
| 59 | case "warning": |
| 60 | return Warning, nil |
| 61 | default: |
| 62 | return Fatal, fmt.Errorf("unknown severity: %s", s) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const ( |
| 67 | // Information doesn't count as a problem, it's a comment |
| 68 | Information Severity = iota |
| 69 | |
| 70 | // Warning is not consider an error |
| 71 | Warning |
| 72 | |
| 73 | // Bug is an error that should be corrected |
| 74 | Bug |
| 75 | |
| 76 | // Fatal is a problem with linting content |
| 77 | Fatal |
| 78 | ) |
| 79 | |
| 80 | type Problem struct { |
| 81 | Fragment string |
| 82 | Lines []int |
| 83 | Reporter string |
| 84 | Text string |
| 85 | Severity Severity |
| 86 | } |
| 87 | |
| 88 | func (p Problem) LineRange() (int, int) { |
| 89 | return p.Lines[0], p.Lines[len(p.Lines)-1] |
| 90 | } |
| 91 | |
| 92 | type RuleChecker interface { |
| 93 | String() string |
| 94 | Reporter() string |
| 95 | Check(ctx context.Context, rule parser.Rule) []Problem |
| 96 | } |
| 97 | |
| 98 | type exprProblem struct { |
| 99 | expr string |
| 100 | text string |
| 101 | severity Severity |
| 102 | } |
| 103 | |