cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.84.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/config/check.go

57lines · modecode

1package config
2
3import (
4 "encoding/json"
5 "fmt"
6
7 "github.com/hashicorp/hcl/v2"
8 "github.com/hashicorp/hcl/v2/gohcl"
9
10 "github.com/cloudflare/pint/internal/checks"
11)
12
13type Check struct {
14 Body hcl.Body `hcl:",remain" json:"-"`
15 Name string `hcl:",label" json:"name"`
16}
17
18func (c Check) MarshalJSON() ([]byte, error) {
19 s, err := c.Decode()
20 if err != nil {
21 return nil, err
22 }
23 return json.MarshalIndent(s, "", " ")
24}
25
26func (c Check) Decode() (s CheckSettings, err error) {
27 switch c.Name {
28 case checks.SeriesCheckName:
29 s = &checks.PromqlSeriesSettings{}
30 case checks.RegexpCheckName:
31 s = &checks.PromqlRegexpSettings{}
32 case checks.RuleDependencyCheckName:
33 s = &checks.RuleDependencySettings{}
34 default:
35 return nil, fmt.Errorf("unknown check %q", c.Name)
36 }
37
38 if diag := gohcl.DecodeBody(c.Body, nil, s); diag != nil && diag.HasErrors() {
39 return nil, diag
40 }
41 if err = s.Validate(); err != nil {
42 return nil, err
43 }
44 return s, nil
45}
46
47func (c Check) validate() error {
48 s, err := c.Decode()
49 if err != nil {
50 return err
51 }
52 return s.Validate()
53}
54
55type CheckSettings interface {
56 Validate() error
57}
58