-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (75 loc) · 1.39 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
package main
import (
_ "embed"
"log"
"os"
"path/filepath"
"strings"
"github.com/redcarbon-dev/nats-proto-pubgen/pkg/gen"
)
type renderFile struct {
path string
data string
}
func main() {
currentDirectory, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
files := iterate(currentDirectory)
base, err := gen.RenderBaseFile()
must(err)
files = append(files, renderFile{
path: "./pubs/model.go",
data: base,
})
for _, file := range files {
mustRenderFile(file.path, file.data)
}
}
func iterate(path string) (files []renderFile) {
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatalf(err.Error())
}
if strings.HasSuffix(info.Name(), ".proto") {
log.Printf("path: %v, File Name: %s\n", path, info.Name())
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
res, ok, err := gen.RenderReader(f)
if err != nil {
panic(err)
}
if ok {
files = append(files, renderFile{
path: "./pubs/" + info.Name() + ".pub.go",
data: res,
})
}
}
return nil
})
if err != nil {
panic(err)
}
return files
}
func must(err error) {
if err != nil {
panic(err)
}
}
func mustRenderFile(path string, content string) {
f, err := os.Create(path)
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.Write([]byte(content))
if err != nil {
panic(err)
}
}