-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_generate.go
105 lines (86 loc) · 3.59 KB
/
cmd_generate.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"embed"
"flag"
"log/slog"
"path/filepath"
"github.com/google/subcommands"
"github.com/jacobbrewer1/goschema/pkg/generation"
)
//go:embed templates/*.tmpl
var defaultTemplates embed.FS
type generateCmd struct {
// templatesLocation is the location of the templates to use.
templatesLocation string
// outputLocation is the location to write the generated files to.
outputLocation string
// sqlLocation is the location of the SQL files to use.
sqlLocation string
// fileExtensionPrefix is the prefix to add to the generated file extension.
fileExtensionPrefix string
// defaultTemplates is whether to use the binary templates.
defaultTemplates bool
}
func (g *generateCmd) Name() string {
return "generate"
}
func (g *generateCmd) Synopsis() string {
return "Generate GO types from a MySQL schema"
}
func (g *generateCmd) Usage() string {
return `generate:
Generate GO types from a MySQL schema.
`
}
func (g *generateCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&g.templatesLocation, "templates", "./templates/*.tmpl", "The location of the templates to use.")
f.StringVar(&g.outputLocation, "out", ".", "The location to write the generated files to.")
f.StringVar(&g.sqlLocation, "sql", "./schemas/*.sql", "The location of the SQL files to use.")
f.StringVar(&g.fileExtensionPrefix, "extension", "xo", "The prefix to add to the generated file extension.")
f.BoolVar(&g.defaultTemplates, "default", true, "Whether to use the default templates.")
}
func (g *generateCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
var err error
g.outputLocation, err = filepath.Abs(g.outputLocation)
if err != nil {
slog.Error("Error getting absolute path", slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
// Load the SQL file locations as abs.
g.sqlLocation, err = filepath.Abs(g.sqlLocation)
if err != nil {
slog.Error("Error getting absolute path", slog.String("sqlLocation", g.sqlLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
if err := generation.GoimportsInstallIfNeeded(); err != nil {
slog.Error("Error installing goimports", slog.String("error", err.Error()))
return subcommands.ExitFailure
}
tables, err := generation.LoadSQL(g.sqlLocation)
if err != nil {
slog.Error("Error loading SQL", slog.String("templatesLocation", g.templatesLocation), slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
} else if len(tables) == 0 {
slog.Info("No tables found", slog.String("sqlLocation", g.sqlLocation))
return subcommands.ExitFailure
}
if g.defaultTemplates {
err = generation.RenderWithTemplates(defaultTemplates, tables, g.outputLocation, g.fileExtensionPrefix)
if err != nil {
slog.Error("Error rendering default templates", slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
} else {
err = generation.RenderTemplates(tables, g.templatesLocation, g.outputLocation, g.fileExtensionPrefix)
if err != nil {
slog.Error("Error rendering templates", slog.String("templatesLocation", g.templatesLocation), slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
}
if err := generation.FmtTemplates(g.outputLocation); err != nil {
slog.Error("Error formatting templates", slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}