-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
81 lines (77 loc) · 2.13 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 (
"flag"
"log"
"os"
"path/filepath"
"go.einride.tech/spanner-aip/internal/codegen"
"go.einride.tech/spanner-aip/internal/codegen/databasecodegen"
"go.einride.tech/spanner-aip/internal/codegen/descriptorcodegen"
"go.einride.tech/spanner-aip/internal/config"
"gopkg.in/yaml.v3"
)
const generatedBy = "spanner-aip-go"
func main() {
log.SetFlags(0)
configFilePath := flag.String("config", "spanner.yaml", "config file")
flag.Parse()
if flag.Arg(0) != "generate" {
log.Fatal("usage: spanner-aip-go generate [-config <config>]")
}
configFile, err := os.Open(*configFilePath)
if err != nil {
log.Panic(err)
}
defer func() {
if err := configFile.Close(); err != nil {
log.Panic(err)
}
}()
var codeGenerationConfig config.CodeGenerationConfig
if err := yaml.NewDecoder(configFile).Decode(&codeGenerationConfig); err != nil {
log.Panic(err)
}
for _, databaseConfig := range codeGenerationConfig.Databases {
db, err := databaseConfig.LoadDatabase()
if err != nil {
log.Panic(err)
}
if err := os.MkdirAll(databaseConfig.Package.Path, 0o775); err != nil {
log.Panic(err)
}
filename := filepath.Join(databaseConfig.Package.Path, "descriptor_gen.go")
f := codegen.NewFile(codegen.FileConfig{
Filename: filename,
Package: databaseConfig.Package.Name,
GeneratedBy: generatedBy,
})
descriptorcodegen.DatabaseDescriptorCodeGenerator{
Database: db,
}.GenerateCode(f)
content, err := f.Content()
if err != nil {
log.Panic(err)
}
if err := os.WriteFile(filename, content, 0o600); err != nil {
log.Panic(err)
}
log.Println("wrote:", filename)
if len(db.Tables) > 0 {
filename := filepath.Join(databaseConfig.Package.Path, "database_gen.go")
f := codegen.NewFile(codegen.FileConfig{
Filename: filename,
Package: databaseConfig.Package.Name,
GeneratedBy: generatedBy,
})
databasecodegen.DatabaseCodeGenerator{Database: db}.GenerateCode(f)
content, err := f.Content()
if err != nil {
log.Panic(err)
}
if err := os.WriteFile(filename, content, 0o600); err != nil {
log.Panic(err)
}
log.Println("wrote:", filename)
}
}
}