-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (57 loc) · 1.52 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
package main
import (
"encoding/json"
"fmt"
"github.com/PaluMacil/decisive-oak/analysis"
"github.com/PaluMacil/decisive-oak/parse"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
func main() {
files, err := filepath.Glob("data/*.data.txt")
if err != nil {
fmt.Printf("finding data files: %v", err)
os.Exit(1)
}
for _, filename := range files {
fmt.Println("opening", filename)
sample, err := parse.FromFile(filename)
if err != nil {
fmt.Printf("parsing %s: %v", filename, err)
os.Exit(1)
}
jsonData, err := json.MarshalIndent(sample, "", " ")
if err != nil {
fmt.Printf("marshalling sample to JSON: %v", err)
os.Exit(1)
}
outFilename := strings.TrimSuffix(filename, path.Ext(filename)) + ".json"
outFilename = path.Join("out", filepath.Base(outFilename))
err = ioutil.WriteFile(outFilename, jsonData, 0644)
if err != nil {
fmt.Printf("writing output file: %v", err)
os.Exit(1)
}
rootNode, err := analysis.BuildTree(sample)
if err != nil {
fmt.Printf("building tree failed: %s", err.Error())
os.Exit(1)
}
jsonData, err = json.MarshalIndent(rootNode, "", " ")
if err != nil {
fmt.Printf("marshalling tree to JSON: %v", err)
os.Exit(1)
}
treeFilename := strings.TrimSuffix(filename, path.Ext(filename)) + ".tree.json"
treeFilename = path.Join("out", filepath.Base(treeFilename))
err = ioutil.WriteFile(treeFilename, jsonData, 0644)
if err != nil {
fmt.Printf("writing tree file: %v", err)
os.Exit(1)
}
fmt.Printf("Wrote %s\n\n", treeFilename)
}
}