-
Notifications
You must be signed in to change notification settings - Fork 197
/
config.go
47 lines (41 loc) · 1.06 KB
/
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
39
40
41
42
43
44
45
46
47
package jsonexporter
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
)
type Config struct {
Name string `yaml:name`
Path string `yaml:path`
Labels map[string]string `yaml:labels`
Type string `yaml:type`
Help string `yaml:help`
Values map[string]string `yaml:values`
}
func (config *Config) labelNames() []string {
labelNames := make([]string, 0, len(config.Labels))
for name := range config.Labels {
labelNames = append(labelNames, name)
}
return labelNames
}
func loadConfig(configPath string) ([]*Config, error) {
data, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("failed to load config;path:<%s>,err:<%s>", configPath, err)
}
var configs []*Config
if err := yaml.Unmarshal(data, &configs); err != nil {
return nil, fmt.Errorf("failed to parse yaml;err:<%s>", err)
}
// Complete defaults
for _, config := range configs {
if config.Type == "" {
config.Type = DefaultScrapeType
}
if config.Help == "" {
config.Help = config.Name
}
}
return configs, nil
}