-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
81 lines (73 loc) · 1.79 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
package main
import (
"bufio"
"dashboard-sanitizer/config"
sm "dashboard-sanitizer/model"
"flag"
"fmt"
"github.com/clarketm/json"
"github.com/olivere/ndjson"
"os"
)
func main() {
//generate command flags
esObjectFile := flag.String("source", "", "The Elastic dashboard object file in ndjson.")
outputFile := flag.String("output", "os_dashboard_objects.ndjson", "The OpenSearch compatible dashboard object file in ndjson.")
versionFlag := flag.Bool("version", false, "Prints the version number")
flag.Parse()
if *versionFlag {
fmt.Print(config.Version)
os.Exit(0)
}
if *esObjectFile == "" {
flag.PrintDefaults()
os.Exit(1)
}
file, err := os.Open(*esObjectFile)
if err != nil {
fmt.Print("Error reading source file: ", err)
os.Exit(1)
}
scanner := bufio.NewScanner(file)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
f, _ := os.Create(*outputFile)
defer func(f *os.File) {
err := f.Close()
if err != nil {
}
}(f)
outBuff := bufio.NewWriter(f)
writer := ndjson.NewWriter(outBuff)
counter := NewStatusCount()
for scanner.Scan() {
line := scanner.Text()
var do sm.DashboardObject
if err := json.Unmarshal([]byte(line), &do); err != nil {
fmt.Printf("Decode failed: %v", err)
return
}
if !do.IsCompatibleType() {
counter.RegisterSkipped(do)
continue
}
_ = do.MakeCompatibleToOS()
err := writer.Encode(do)
if err != nil {
fmt.Print(err)
return
}
//Flush after each line processed
err = outBuff.Flush()
if err != nil {
fmt.Print("Error writing to output file :", err)
}
counter.RegisterProcessed(do)
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return
}
fmt.Printf("\n %s file is sanitized and output available at %s \n", *esObjectFile, *outputFile)
counter.PrintStats()
}