cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/config/annotation.go
45lines · modecode
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "regexp" |
| 6 | |
| 7 | "github.com/cloudflare/pint/internal/checks" |
| 8 | ) |
| 9 | |
| 10 | type AnnotationSettings struct { |
| 11 | Key string `hcl:",label" json:"key"` |
| 12 | Value string `hcl:"value,optional" json:"value,omitempty"` |
| 13 | Required bool `hcl:"required,optional" json:"required,omitempty"` |
| 14 | Severity string `hcl:"severity,optional" json:"severity,omitempty"` |
| 15 | } |
| 16 | |
| 17 | func (as AnnotationSettings) validate() error { |
| 18 | if as.Key == "" { |
| 19 | return errors.New("annotation key cannot be empty") |
| 20 | } |
| 21 | |
| 22 | if _, err := regexp.Compile(as.Key); err != nil { |
| 23 | return err |
| 24 | } |
| 25 | |
| 26 | if _, err := regexp.Compile(as.Value); err != nil { |
| 27 | return err |
| 28 | } |
| 29 | |
| 30 | if as.Severity != "" { |
| 31 | if _, err := checks.ParseSeverity(as.Severity); err != nil { |
| 32 | return err |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return nil |
| 37 | } |
| 38 | |
| 39 | func (as AnnotationSettings) getSeverity(fallback checks.Severity) checks.Severity { |
| 40 | if as.Severity != "" { |
| 41 | sev, _ := checks.ParseSeverity(as.Severity) |
| 42 | return sev |
| 43 | } |
| 44 | return fallback |
| 45 | } |
| 46 | |