forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (93 loc) · 2.83 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
)
func copyTemplatesToDest(templatesPath, name, filesetPath, module, fileset string) error {
template := path.Join(templatesPath, name)
c, err := ioutil.ReadFile(template)
if err != nil {
return err
}
c = bytes.Replace(c, []byte("{module}"), []byte(module), -1)
c = bytes.Replace(c, []byte("{fileset}"), []byte(fileset), -1)
dest := path.Join(filesetPath, name)
err = ioutil.WriteFile(dest, c, os.ModePerm)
if err != nil {
return fmt.Errorf("cannot copy template: %v", err)
}
return nil
}
func generateModule(module, fileset, modulePath, beatsPath string) error {
p := path.Join(modulePath, "module", module)
if _, err := os.Stat(p); os.IsExist(err) {
return fmt.Errorf("module already exists: %s at %s", module, p)
}
d := path.Join(p, "_meta", "kibana", "default")
err := os.MkdirAll(d, 0750)
if err != nil {
return err
}
templatesPath := path.Join(beatsPath, "scripts", "module")
filesToCopy := []string{path.Join("fields.yml"), path.Join("docs.asciidoc")}
for _, f := range filesToCopy {
err := copyTemplatesToDest(templatesPath, f, p, module, "")
if err != nil {
return err
}
}
return nil
}
func generateFileset(module, fileset, modulePath, beatsPath string) error {
filesetPath := path.Join(modulePath, "module", module, fileset)
if _, err := os.Stat(filesetPath); os.IsExist(err) {
return fmt.Errorf("fileset already exists: %s", fileset)
}
dirsToCreate := []string{"", "_meta", "test", "config", "ingest"}
for _, d := range dirsToCreate {
p := path.Join(filesetPath, d)
err := os.Mkdir(p, 0750)
if err != nil {
return err
}
}
templatesPath := path.Join(beatsPath, "scripts", "module", "fileset")
filesToCopy := []string{path.Join("config", "config.yml"), path.Join("ingest", "pipeline.json"), "manifest.yml"}
for _, f := range filesToCopy {
err := copyTemplatesToDest(templatesPath, f, filesetPath, module, fileset)
if err != nil {
return err
}
}
return nil
}
func main() {
module := flag.String("module", "", "Name of the module")
fileset := flag.String("fileset", "", "Name of the fileset")
modulePath := flag.String("path", ".", "Path to the generated fileset")
beatsPath := flag.String("beats_path", ".", "Path to elastic/beats")
flag.Parse()
if *module == "" {
fmt.Println("Missing parameter: module")
os.Exit(1)
}
if *fileset == "" {
fmt.Println("Missing parameter: fileset")
os.Exit(1)
}
err := generateModule(*module, *fileset, *modulePath, *beatsPath)
if err != nil {
fmt.Printf("Cannot generate module: %v\n", err)
os.Exit(2)
}
err = generateFileset(*module, *fileset, *modulePath, *beatsPath)
if err != nil {
fmt.Printf("Cannot generate fileset: %v\n", err)
os.Exit(3)
}
fmt.Println("New module was generated. After setting up Grok pattern in pipeline.json, please generate fields.yml")
}