-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
85 lines (68 loc) · 1.67 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
package main
import (
"flag"
"fmt"
"kumquat/repository"
"kumquat/store"
"kumquat/template"
"log/slog"
"os"
)
func main() {
var repo repository.Repository
repo, err := repository.NewSQLiteRepository()
if err != nil {
slog.Error("Unable to create repository", "err", err)
panic(err)
}
defer repo.Close() //nolint:errcheck
inDir := flag.String("in", "sampledata", "directory path to read Kubernetes resources")
flag.Parse()
err = repository.LoadYAMLFromDirectoryTree(os.DirFS("."), *inDir, repo)
if err != nil {
slog.Error("Unable to load directory tree", "err", err)
panic(err)
}
tplrs, err := repo.Query(
/* sql */ `SELECT template.data AS tpl FROM "` + template.TemplateResourceType + `" AS template`,
)
if err != nil {
slog.Error("Unable to find template", "err", err)
panic(err)
}
// Process every Template
templates := make([]*template.Template, 0, len(tplrs.Results))
for _, tplrs := range tplrs.Results {
tplres := tplrs["tpl"]
t, err := template.NewTemplate(tplres)
if err != nil {
fmt.Printf("%v\n", err)
continue
}
templates = append(templates, t)
fmt.Printf("Loaded Template %s\n", t.Name())
}
for _, t := range templates {
o, err := t.Evaluate(repo)
if err != nil {
fmt.Printf("%v\n", err)
continue
}
generateOutput(o)
}
}
func generateOutput(o *template.TemplateOutput) {
// for loop over data
for i := 0; i < o.Output.ResourceCount(); i++ {
out, err := o.Output.ResultString(i)
if err != nil {
panic(err)
}
fileName := o.FileNames[i]
// use WriteFile function in store package to write the output to a file
err = store.WriteToFile(fileName, "", out)
if err != nil {
panic(err)
}
}
}