-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
72 lines (62 loc) · 1.54 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
package main
import (
"context"
"io/fs"
"log"
"os"
"os/signal"
"syscall"
"github.com/stephenafamo/bob/gen"
"github.com/stephenafamo/bob/gen/bobgen-atlas/driver"
helpers "github.com/stephenafamo/bob/gen/bobgen-helpers"
"github.com/urfave/cli/v2"
)
func main() {
ctx, cancel := signal.NotifyContext(
context.Background(),
syscall.SIGINT,
syscall.SIGTERM,
)
defer cancel()
app := &cli.App{
Name: "bobgen-atlas",
Usage: "Generate models and factories from your Atlas schema files",
UsageText: "bobgen-atlas [-c FILE]",
Version: helpers.Version(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: helpers.DefaultConfigPath,
Usage: "Load configuration from `FILE`",
},
},
Action: run,
}
if err := app.RunContext(ctx, os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
config, driverConfig, err := helpers.GetConfigFromFile[driver.Config](c.String("config"), "atlas")
if err != nil {
return err
}
var modelTemplates []fs.FS
switch driverConfig.Dialect {
case "mysql":
modelTemplates = append(modelTemplates, gen.MySQLModelTemplates)
case "sqlite":
modelTemplates = append(modelTemplates, gen.SQLiteModelTemplates)
}
d := driver.New(driverConfig, os.DirFS(driverConfig.Dir))
outputs := helpers.DefaultOutputs(
driverConfig.Output, driverConfig.Pkgname, config.NoFactory,
&helpers.Templates{Models: modelTemplates},
)
state := &gen.State{
Config: config,
Outputs: outputs,
}
return gen.Run(c.Context, state, d)
}