-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (85 loc) · 2.94 KB
/
main.go
File metadata and controls
101 lines (85 loc) · 2.94 KB
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
// wrote by yijian on 2024/11/05
package main
import (
"bufio"
"flag"
"fmt"
"os"
)
import (
"github.com/eyjian/sql2struct/s2s"
)
const Version string = "0.0.13"
var (
help = flag.Bool("h", false, "Display a help message and exit.")
version = flag.Bool("v", false, "Display version info and exit.")
sqlFile = flag.String("sf", "", "SQL file containing \"CREATE TABLE\".")
packageName = flag.String("package", "", "Package name.")
tablePrefix = flag.String("tp", "t_", "Prefix of table name.")
fieldPrefix = flag.String("fp", "f_", "Prefix of field name.")
withGorm = flag.Bool("gorm", true, "With gorm tag.")
withJson = flag.Bool("json", true, "With json tag.")
withDb = flag.Bool("db", true, "With db tag.")
withForm = flag.Bool("form", true, "With form tag.")
withTableNameFunc = flag.Bool("with-tablename-func", false, "Generate a function that takes the table name.")
jsonWithPrefix = flag.Bool("json-with-prefix", false, "Whether json tag retains prefix of field name.")
formWithPrefix = flag.Bool("form-with-prefix", false, "Whether from tag retains prefix of field name.")
customTags = flag.String("custom-tags", "", "Custom tags, separate multiple tags with commas, example: -tags=\"sql,-xorm,ent,reform\".")
withPointer = flag.Bool("pointer", false, "Use pointer types instead of object types.")
withTimePointer = flag.Bool("time-pointer", false, "Use pointer types instead of object types if type is time.Time.")
)
var (
buildTime string // build time
)
func main() {
flag.Parse()
if *help {
showUsage()
os.Exit(1)
}
if *version {
showVersion()
os.Exit(1)
}
if len(*sqlFile) == 0 {
fmt.Fprintf(os.Stderr, "Parameter --sf is not set.\n")
showUsage()
os.Exit(2)
}
// 打开文件
file, err := os.Open(*sqlFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Open %s error: %s.\n", *sqlFile, err.Error())
os.Exit(3)
}
defer file.Close()
sqlTable := s2s.NewSqlTable()
sqlTable.Version = Version
sqlTable.PackageName = *packageName
sqlTable.TablePrefix = *tablePrefix
sqlTable.FieldPrefix = *fieldPrefix
sqlTable.WithGorm = *withGorm
sqlTable.WithJson = *withJson
sqlTable.WithDb = *withDb
sqlTable.WithForm = *withForm
sqlTable.WithTableNameFunc = *withTableNameFunc
sqlTable.JsonWithPrefix = *jsonWithPrefix
sqlTable.FormWithPrefix = *formWithPrefix
sqlTable.CustomTags = *customTags
sqlTable.PointerType = *withPointer
sqlTable.TimePointerType = *withTimePointer
scanner := bufio.NewScanner(file) // 创建一个扫描器,用于按行读取文件
structStr, err := sqlTable.Sql2Struct(scanner)
if err != nil {
fmt.Fprintf(os.Stderr, "Parse %s error: %s.\n", *sqlFile, err.Error())
} else {
fmt.Fprintf(os.Stdout, "%s\n", structStr)
}
}
func showUsage() {
fmt.Fprintln(os.Stderr, "A tool to convert table creation SQL into Go struct, TAB is not supported in SQL file, only spaces are supported.")
flag.Usage()
}
func showVersion() {
fmt.Printf("Version: %s, build at %s\n", Version, buildTime)
}