-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_global_config.go
68 lines (58 loc) · 1.68 KB
/
app_global_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
package app
import (
"strings"
"path/filepath"
"bytes"
"os"
"github.com/davidhiendl/telegraf-docker-sd/app/globalconfig"
"github.com/sirupsen/logrus"
)
func (app *App) processGlobalConfig() {
globalConfig := &globalconfig.GlobalConfigSpec{
EnvMap: app.config.EnvMap,
Tags: app.config.GlobalTags,
Backends: app.config.Backends,
}
for _, template := range app.templates {
// filter by backend type
if template.Spec.Backend != "global" {
continue
}
simpleName := strings.TrimSuffix(strings.TrimSuffix(template.FileName, ".yaml"), ".yml")
configFile, _ := filepath.Abs(
app.config.ConfigDir +
"/" + app.config.AutoConfPrefix +
"_global_" +
simpleName +
app.config.AutoConfExtension)
configBuffer := new(bytes.Buffer)
err := template.Execute(configBuffer, globalConfig)
if err != nil {
logrus.Fatalf("[global][%v] error during template execution: %+v", simpleName, err)
}
// write out config file
writeConfigFile(configFile, configBuffer.String())
logrus.Debugf("[global][%v] wrote main config file: %v", simpleName, configFile)
}
}
func writeConfigFile(path string, contents string) {
// open file using READ & WRITE permission
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
logrus.Errorf("[global] failed to open file: %v err: %v", path, err)
panic(err)
}
defer file.Close()
// write some text line-by-line to file
_, err = file.WriteString(contents)
if err != nil {
logrus.Errorf("[global] failed to write file: %v err: %v", path, err)
panic(err)
}
// save changes
err = file.Sync()
if err != nil {
logrus.Errorf("[global] failed to sync file: %v err: %v", path, err)
panic(err)
}
}