cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.74.5

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/config/check.go

55lines · modecode

1package config
2
3import (
4 "encoding/json"
5 "fmt"
6
7 "github.com/cloudflare/pint/internal/checks"
8
9 "github.com/hashicorp/hcl/v2"
10 "github.com/hashicorp/hcl/v2/gohcl"
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 default:
33 return nil, fmt.Errorf("unknown check %q", c.Name)
34 }
35
36 if diag := gohcl.DecodeBody(c.Body, nil, s); diag != nil && diag.HasErrors() {
37 return nil, diag
38 }
39 if err = s.Validate(); err != nil {
40 return nil, err
41 }
42 return s, nil
43}
44
45func (c Check) validate() error {
46 s, err := c.Decode()
47 if err != nil {
48 return err
49 }
50 return s.Validate()
51}
52
53type CheckSettings interface {
54 Validate() error
55}