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