forked from martin-helmich/prometheus-nginxlog-exporter
/
loader.go
52 lines (43 loc) · 1.23 KB
/
loader.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
48
49
50
51
52
package config
import (
"strings"
"fmt"
"io"
"os"
)
type ConfigType int
const (
TYPE_HCL ConfigType = iota
TYPE_YAML
)
// LoadConfigFromFile fills a configuration object (passed as parameter) with
// values read from a configuration file (pass as parameter by filename). The
// configuration file needs to be in HCL format.
func LoadConfigFromFile(config *Config, filename string) error {
var typ ConfigType
reader, err := os.Open(filename)
if err != nil {
return err
}
defer reader.Close()
if strings.HasSuffix(filename, ".hcl") {
typ = TYPE_HCL
} else if strings.HasSuffix(filename, ".yaml") || strings.HasSuffix(filename, ".yml") {
typ = TYPE_YAML
} else {
return fmt.Errorf("config file '%s' has unsupported file type", filename)
}
return LoadConfigFromStream(config, reader, typ)
}
// LoadConfigFromStream fills a configuration object (passed as parameter) with
// values read from a Reader interface (passed as parameter).
func LoadConfigFromStream(config *Config, stream io.Reader, typ ConfigType) error {
switch typ {
case TYPE_HCL:
return loadConfigFromHCLStream(config, stream)
case TYPE_YAML:
return loadConfigFromYAMLStream(config, stream)
default:
return fmt.Errorf("unsupported config type %d", typ)
}
}