-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
204 lines (177 loc) · 5.52 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"database/sql"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
_ "github.com/denisenkom/go-mssqldb"
"github.com/droundy/goopt"
_ "github.com/go-sql-driver/mysql"
"github.com/jimsmart/schema"
"github.com/jinzhu/inflection"
_ "github.com/lib/pq"
"github.com/serenize/snaker"
gtmpl "github.com/ziyeziye/gf-gen/template"
"github.com/ziyeziye/gf-gen/util"
)
var (
sqlType = goopt.String([]string{"--sqltype"}, "mysql", "sql database type such as mysql, postgres, etc.")
sqlConnStr = goopt.String([]string{"-c", "--connstr"}, "nil", "database connection string")
sqlDatabase = goopt.String([]string{"-d", "--database"}, "nil", "Database to for connection")
sqlTable = goopt.String([]string{"-t", "--table"}, "", "Table to build struct from")
prefix = goopt.String([]string{"--prefix"}, "", "Table prefix")
packageName = goopt.String([]string{"--package"}, "", "name to set for package")
jsonAnnotation = goopt.Flag([]string{"--json"}, []string{"--no-json"}, "Add json annotations (default)", "Disable json annotations")
gormAnnotation = goopt.Flag([]string{"--gorm"}, []string{}, "Add gorm annotations (tags)", "")
gureguTypes = goopt.Flag([]string{"--guregu"}, []string{}, "Add guregu null types", "")
rest = goopt.Flag([]string{"--rest"}, []string{}, "Enable generating RESTful api", "")
version = goopt.Flag([]string{"-v", "--version"}, []string{}, "Enable version output", "")
)
func init() {
// Setup goopts
goopt.Description = func() string {
return "ORM and RESTful API generator for Mysql"
}
goopt.Version = "v1.2"
goopt.Summary = `gf-gen [-v] --connstr "user:password@/dbname" --package pkgName --database databaseName --table tableName [--json] [--guregu]`
//Parse options
goopt.Parse(nil)
}
func main() {
if *version == true {
fmt.Println("The version number of framework-gen is " + goopt.Version)
return
}
// Username is required
if sqlConnStr == nil || *sqlConnStr == "" {
fmt.Println("sql connection string is required! Add it with --connstr=s")
return
}
if sqlDatabase == nil || *sqlDatabase == "" {
fmt.Println("Database can not be null")
return
}
var db, err = sql.Open(*sqlType, *sqlConnStr)
if err != nil {
fmt.Println("Error in open database: " + err.Error())
return
}
defer db.Close()
// parse or read tables
var tables []string
if *sqlTable != "" {
tables = strings.Split(*sqlTable, ",")
} else {
tables, err = schema.TableNames(db)
if err != nil {
fmt.Println("Error in fetching tables information from mysql information schema")
return
}
}
// if packageName is not set we need to default it
if packageName == nil || *packageName == "" {
*packageName = "framework"
}
os.Mkdir("model", 0777)
apiName := "api"
if *rest {
os.Mkdir(apiName, 0777)
}
t, err := getTemplate(gtmpl.ModelTmpl)
if err != nil {
fmt.Println("Error in loading models template: " + err.Error())
return
}
ct, err := getTemplate(gtmpl.ControllerTmpl)
if err != nil {
fmt.Println("Error in loading controller template: " + err.Error())
return
}
var structNames []string
// generate go files for each table
for _, tableName := range tables {
tableName := strings.Replace(tableName, *prefix, "", 1)
structName := util.FmtFieldName(tableName)
structName = inflection.Singular(structName)
structNames = append(structNames, structName)
modelInfo := util.GenerateStruct(db, *prefix, tableName, structName, "model", *jsonAnnotation, *gormAnnotation, *gureguTypes)
//fmt.Printf("%+v",tableName)
//os.Exit(0)
var buf bytes.Buffer
err = t.Execute(&buf, modelInfo)
if err != nil {
fmt.Println("Error in rendering models: " + err.Error())
return
}
data, err := format.Source(buf.Bytes())
if err != nil {
fmt.Println("Error in formating source: " + err.Error())
return
}
ioutil.WriteFile(filepath.Join("model", inflection.Singular(tableName)+".mod.go"), data, 0777)
if *rest {
//write api
buf.Reset()
err = ct.Execute(&buf, map[string]string{
"Package": *packageName,
"PackageName": *packageName + "/model",
"StructName": structName,
"TableName": tableName,
})
if err != nil {
fmt.Println("Error in rendering controller: " + err.Error())
return
}
data, err = format.Source(buf.Bytes())
if err != nil {
fmt.Println("Error in formating source: " + err.Error())
return
}
ioutil.WriteFile(filepath.Join(apiName, inflection.Singular(tableName)+".api.go"), data, 0777)
}
}
if *rest {
rt, err := getTemplate(gtmpl.RouterTmpl)
if err != nil {
fmt.Println("Error in lading router template")
return
}
var buf bytes.Buffer
err = rt.Execute(&buf, structNames)
if err != nil {
fmt.Println("Error in rendering router: " + err.Error())
return
}
data, err := format.Source(buf.Bytes())
if err != nil {
fmt.Println("Error in formating source: " + err.Error())
return
}
ioutil.WriteFile(filepath.Join(apiName, "router.go"), data, 0777)
}
}
func getTemplate(t string) (*template.Template, error) {
var funcMap = template.FuncMap{
"pluralize": inflection.Plural,
"title": strings.Title,
"toLower": strings.ToLower,
"toLowerCamelCase": camelToLowerCamel,
"toLowerUnderline": inflection.Singular,
"toSnakeCase": snaker.CamelToSnake,
}
tmpl, err := template.New("model").Funcs(funcMap).Parse(t)
if err != nil {
return nil, err
}
return tmpl, nil
}
func camelToLowerCamel(s string) string {
ss := strings.Split(s, "")
ss[0] = strings.ToLower(ss[0])
return strings.Join(ss, "")
}