-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (57 loc) · 1.29 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
package main
import (
_ "embed"
"fmt"
"io"
"os"
"text/template"
"ariga.io/atlas-go-sdk/tmplrun"
"github.com/alecthomas/kong"
"golang.org/x/tools/go/packages"
)
// LoadCmd is a command to load models
type LoadCmd struct {
Path string `help:"path to schema package" required:""`
Dialect string `help:"dialect to use" enum:"mysql,sqlite3,postgres" required:""`
out io.Writer
}
var (
//go:embed loader.tmpl
loader string
loaderTmpl = template.Must(template.New("loader").Parse(loader))
)
func main() {
var cli struct {
Load LoadCmd `cmd:""`
}
ctx := kong.Parse(&cli)
if err := ctx.Run(); err != nil {
fmt.Fprintln(os.Stderr, err) // nolint: errcheck
os.Exit(1)
}
}
func (c *LoadCmd) Run() error {
cfg := &packages.Config{Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedModule | packages.NeedDeps}
pkgs, err := packages.Load(cfg, c.Path)
if err != nil {
return err
}
p := Payload{
Dialect: c.Dialect,
Imports: []string{pkgs[0].PkgPath},
}
s, err := tmplrun.New("beegoschema", loaderTmpl).Run(p)
if err != nil {
return err
}
if c.out == nil {
c.out = os.Stdout
}
_, err = fmt.Fprintln(c.out, s)
return err
}
// Payload is the data passed to the loader template.
type Payload struct {
Dialect string
Imports []string
}