cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.28.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/config/check.go

53lines · 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 Name string `hcl:",label" json:"name"`
15 Body hcl.Body `hcl:",remain" json:"-"`
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 default:
31 return nil, fmt.Errorf("unknown check %q", c.Name)
32 }
33
34 if diag := gohcl.DecodeBody(c.Body, nil, s); diag != nil && diag.HasErrors() {
35 return nil, diag
36 }
37 if err = s.Validate(); err != nil {
38 return nil, err
39 }
40 return s, nil
41}
42
43func (c Check) validate() error {
44 s, err := c.Decode()
45 if err != nil {
46 return err
47 }
48 return s.Validate()
49}
50
51type CheckSettings interface {
52 Validate() error
53}
54