-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
45 lines (37 loc) · 1.12 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
package main
import (
"fmt"
"os"
"github.com/hostnetbr/gatekeeper-log-exporter/exporter/influx"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
GkLogDir string `yaml:"gk_log_dir"`
LogLineRegex string `yaml:"log_line_regex"`
InfluxDB *influx.Config `yaml:"influxdb"`
}
func parseConfig() (Config, error) {
data, err := os.ReadFile(confFile)
if err != nil {
return Config{}, fmt.Errorf("error reading config file: %w", err)
}
var cfg Config
if err = yaml.Unmarshal(data, &cfg); err != nil {
return Config{}, fmt.Errorf("error parsing config: %w", err)
}
if cfg.GkLogDir == "" {
return Config{}, fmt.Errorf("gk_log_dir empty: %w", err)
}
if cfg.InfluxDB == nil {
return Config{}, fmt.Errorf("error parsing influxdb config")
}
if cfg.InfluxDB.User == "" || cfg.InfluxDB.Pass == "" || cfg.InfluxDB.Database == "" {
return Config{}, fmt.Errorf("not enough authentication credentials for influxdb")
}
if cfg.InfluxDB.Hostname == "" {
if cfg.InfluxDB.Hostname, err = os.Hostname(); err != nil {
return Config{}, fmt.Errorf("error parsing hostname: %w", err)
}
}
return cfg, nil
}