This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
89 lines (76 loc) · 1.66 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package drone
import (
"github.com/BurntSushi/toml"
"log"
"os"
"path/filepath"
)
func GetConfig(path string) *Config {
conf := &Config{}
good := false
// get outer config dir
d := filepath.Dir(path)
// check if main config exists
_, err := os.Stat(path)
if err != nil {
if _, ok := err.(*os.PathError); ok {
log.Print("config: base config not found")
} else {
log.Panicf("config: stat err :: %v", err)
}
} else {
_, err = toml.DecodeFile(path, conf)
if err != nil {
log.Panicf("config: decode err :: %v", err)
} else {
log.Printf("config: loaded root: %s", path)
good = true
}
}
// check conf.d
cd, err := os.Stat(d)
if err != nil {
if _, ok := err.(*os.PathError); ok {
log.Printf("config: %s doesn't exist, skipping")
} else {
log.Panicf("config: stat err :: %v", err)
}
} else {
if cd.IsDir() {
cdf, _ := filepath.Glob(filepath.Join(d, "conf.d", "*.toml"))
for _, c := range cdf {
_, err = toml.DecodeFile(c, conf)
if err != nil {
log.Printf("config: decode err (file: %s) :: %v", c, err)
} else {
log.Printf("config: loaded conf.d: %s", c)
good = true
}
}
} else {
log.Printf("config: %s isn't a directory, skipping")
}
}
if !good {
log.Fatal("config: i didn't load any sort of config. exiting.")
}
return conf
}
type Config struct {
Graylog graylogConfig
Collector collectorConfig
Logs logsConfig
}
type graylogConfig struct {
Address string
}
type collectorConfig struct {
Hostname string
}
type logsConfig map[string]LogConfig
type LogConfig struct {
File string
Parser string
Pattern string
ShortText string `toml:"short_text",omitempty`
}