This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
write_config.go
79 lines (72 loc) · 1.99 KB
/
write_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
package v5
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/jetstack/navigator/pkg/apis/navigator/v1alpha1"
)
const (
elasticsearchConfigSubDir = "elasticsearch/config"
)
func (p *Pilot) WriteConfig(pilot *v1alpha1.Pilot) error {
esConfigPath := fmt.Sprintf("%s/%s", p.Options.ConfigDir, elasticsearchConfigSubDir)
files, err := ioutil.ReadDir(esConfigPath)
if err != nil {
return fmt.Errorf("error listing provided config files: %s", err.Error())
}
for _, info := range files {
path := filepath.Join(esConfigPath, info.Name())
path, err = filepath.EvalSymlinks(path)
if err != nil {
return fmt.Errorf("error evaluating symlinks in path %q: %s", path, err.Error())
}
glog.V(2).Infof("Considering file %q (path: %q) when writing elasticsearch config", info.Name(), path)
// re-check info after evaluating symlinks
info, err = os.Stat(path)
if err != nil {
return fmt.Errorf("error getting info for path %q: %s", path, err.Error())
}
if info.IsDir() {
continue
}
dstPath := fmt.Sprintf("%s/%s", p.Options.ElasticsearchOptions.ConfigDir, info.Name())
glog.V(2).Infof("Copying config file from %q to %q", path, dstPath)
if err = copyFileContents(path, dstPath); err != nil {
return err
}
}
if err != nil {
return fmt.Errorf("error writing config file: %s", err.Error())
}
return nil
}
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
// From: https://stackoverflow.com/a/21067803
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}