forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_template.go
61 lines (49 loc) · 1.45 KB
/
index_template.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/elastic/beats/libbeat/template"
"github.com/elastic/beats/libbeat/version"
)
// main generates index templates for the beats
func main() {
beatVersion := version.GetDefaultVersion()
index := flag.String("index", "", "Base index name. Normally {beat_name} (required)")
output := flag.String("output", "", "Required: Full path to the output file (required)")
version := flag.String("es.version", beatVersion, "Elasticsearch version")
file := flag.String("file", "", "Path to fields.yml file")
flag.Parse()
if len(*file) == 0 {
fmt.Fprintf(os.Stderr, "File path cannot be empty")
os.Exit(1)
}
if *index == "" {
fmt.Fprintf(os.Stderr, "index is empty. It must be set.")
os.Exit(1)
}
if _, err := os.Stat(*file); err != nil {
fmt.Fprintf(os.Stderr, "Error during loading -file %s with error: %s", *file, err)
os.Exit(1)
}
// Make it compatible with the sem versioning
if *version == "2x" {
*version = "2.0.0"
}
tmpl, err := template.New(beatVersion, *version, *index)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating template: %+v", err)
os.Exit(1)
}
templateString, err := tmpl.Load(*file)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating template: %+v", err)
os.Exit(1)
}
err = ioutil.WriteFile(*output, []byte(templateString.StringToPrint()), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing output: %s", err)
os.Exit(1)
}
}