-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
75 lines (64 loc) · 1.31 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"ariga.io/atlas/sql/schema"
)
var (
// inject by ldflags
BuildVersion = ""
BuildRevision = ""
BuildTimestamp = ""
)
func main() {
log.SetPrefix("atlas-hcl-gen-go: ")
log.SetFlags(0)
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
var hclPath, outPath, target, tag, pkg string
flag.StringVar(&hclPath, "i", "", "input file path")
flag.StringVar(&outPath, "o", "", "output file path")
flag.StringVar(&target, "t", "mysql", "target database")
flag.StringVar(&tag, "tag", "db", "tag name")
flag.StringVar(&pkg, "package", "main", "package name")
flag.Parse()
b, err := os.ReadFile(hclPath)
if err != nil {
return err
}
// parse hcl schema
ev, err := toSchemaEvaluatorFunc(strings.ToLower(target))
if err != nil {
return err
}
var s schema.Schema
if err := hclBytesFunc(ev)(b, &s, nil); err != nil {
return err
}
// generate go code
pb, err := generate(s, input{
hclPath: hclPath,
outPath: outPath,
pkg: pkg,
tag: tag,
})
if err != nil {
return fmt.Errorf("failed to generate: %w", err)
}
// save to file
f, err := os.Create(outPath)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write(pb); err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}