github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/tools/query-audit/config.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "time" 6 7 "github.com/pkg/errors" 8 9 "sigs.k8s.io/yaml" 10 ) 11 12 type Backend struct { 13 Host string `yaml:"host" json:"host"` 14 Headers map[string]string `yaml:"headers" json:"headers"` 15 } 16 17 type Query struct { 18 Query string `yaml:"query" json:"query"` 19 Start time.Time `yaml:"start" json:"start"` 20 End time.Time `yaml:"end" json:"end"` 21 StepSizeStr string `yaml:"step_size" json:"step_size"` 22 StepSize time.Duration 23 } 24 25 func (q *Query) Validate() error { 26 parsedDur, err := time.ParseDuration(q.StepSizeStr) 27 if err != nil { 28 return err 29 } 30 31 q.StepSize = parsedDur 32 33 if q.StepSize == time.Duration(0) { 34 q.StepSize = time.Minute 35 } 36 return nil 37 } 38 39 type Config struct { 40 Control Backend `yaml:"control" json:"control"` 41 Test Backend `yaml:"test" json:"test"` 42 Queries []*Query `yaml:"queries" json:"queries"` 43 } 44 45 func (cfg *Config) Validate() error { 46 for _, q := range cfg.Queries { 47 if err := q.Validate(); err != nil { 48 return err 49 } 50 } 51 return nil 52 } 53 54 // LoadConfig read YAML-formatted config from filename into cfg. 55 func LoadConfig(filename string, cfg *Config) error { 56 buf, err := ioutil.ReadFile(filename) 57 if err != nil { 58 return errors.Wrap(err, "Error reading config file") 59 } 60 61 err = yaml.Unmarshal(buf, cfg) 62 if err != nil { 63 return errors.Wrap(err, "Error parsing config file") 64 } 65 66 return cfg.Validate() 67 }