-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
38 lines (33 loc) · 988 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"fmt"
"os"
"gopkg.in/yaml.v2"
)
// queryBackendConfig represents the configuration for a backend query target.
type queryBackendConfig struct {
QueryTargetURL string `yaml:"query_target_url"`
Auth struct {
CredentialsFile string `yaml:"credentials_file"`
Scopes []string `yaml:"scopes"`
} `yaml:"auth"`
}
// loadQueryConfig parses the given YAML file into a queryTargetConfig.
func loadQueryConfig(fileName string) (*queryBackendConfig, error) {
if fileName == "" {
return nil, fmt.Errorf("query.config-file flag must be set")
}
content, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("error opening config file: %s", err)
}
cfg := &queryBackendConfig{}
err = yaml.UnmarshalStrict([]byte(content), cfg)
if err != nil {
return nil, fmt.Errorf("error parsing YAML file: %s", err)
}
if cfg.QueryTargetURL == "" {
return nil, fmt.Errorf("query_target_url needs to be set")
}
return cfg, nil
}