This commit is contained in:
Sergey Lanzman 2016-07-23 02:05:01 +03:00
parent b022ab3277
commit bc963e0070
12 changed files with 93 additions and 101 deletions

12
bale.go
View File

@ -68,7 +68,7 @@ func runBale(cmd *Command, args []string) int {
// Generate auto-uncompress function.
buf := new(bytes.Buffer)
buf.WriteString(fmt.Sprintf(_BALE_HEADER, conf.Bale.Import,
buf.WriteString(fmt.Sprintf(BaleHeader, conf.Bale.Import,
strings.Join(resFiles, "\",\n\t\t\""),
strings.Join(resFiles, ",\n\t\tbale.R")))
@ -90,7 +90,7 @@ func runBale(cmd *Command, args []string) int {
}
const (
_BALE_HEADER = `package main
BaleHeader = `package main
import(
"os"
@ -178,7 +178,7 @@ func walkFn(resPath string, info os.FileInfo, err error) error {
defer fw.Close()
// Write header.
fmt.Fprintf(fw, _HEADER, resPath)
fmt.Fprintf(fw, Header, resPath)
// Copy and compress data.
gz := gzip.NewWriter(&ByteWriter{Writer: fw})
@ -186,7 +186,7 @@ func walkFn(resPath string, info os.FileInfo, err error) error {
gz.Close()
// Write footer.
fmt.Fprint(fw, _FOOTER)
fmt.Fprint(fw, Footer)
resFiles = append(resFiles, resPath)
return nil
@ -202,7 +202,7 @@ func filterSuffix(name string) bool {
}
const (
_HEADER = `package bale
Header = `package bale
import(
"bytes"
@ -212,7 +212,7 @@ import(
func R%s() []byte {
gz, err := gzip.NewReader(bytes.NewBuffer([]byte{`
_FOOTER = `
Footer = `
}))
if err != nil {

View File

@ -19,7 +19,7 @@ import (
"os"
)
const CONF_VER = 0
const ConfVer = 0
var defaultConf = `{
"version": 0,
@ -91,7 +91,7 @@ func loadConfig() error {
}
// Check format version.
if conf.Version != CONF_VER {
if conf.Version != ConfVer {
ColorLog("[WARN] Your bee.json is out-of-date, please update!\n")
ColorLog("[HINT] Compare bee.json under bee source code path and yours\n")
}

View File

@ -29,9 +29,9 @@ import (
)
const (
O_MODEL byte = 1 << iota
O_CONTROLLER
O_ROUTER
OModel byte = 1 << iota
OController
ORouter
)
// DbTransformer has method to reverse engineer a database schema to restful api code
@ -259,11 +259,11 @@ func generateAppcode(driver, connStr, level, tables, currpath string) {
var mode byte
switch level {
case "1":
mode = O_MODEL
mode = OModel
case "2":
mode = O_MODEL | O_CONTROLLER
mode = OModel | OController
case "3":
mode = O_MODEL | O_CONTROLLER | O_ROUTER
mode = OModel | OController | ORouter
default:
ColorLog("[ERRO] Invalid 'level' option: %s\n", level)
ColorLog("[HINT] Level must be either 1, 2 or 3\n")
@ -505,10 +505,9 @@ func (*MysqlDB) GetGoDataType(sqlType string) (goType string) {
typeMapping = typeMappingMysql
if v, ok := typeMapping[sqlType]; ok {
return v
} else {
ColorLog("[ERRO] data type (%s) not found!\n", sqlType)
os.Exit(2)
}
ColorLog("[ERRO] data type (%s) not found!\n", sqlType)
os.Exit(2)
return goType
}
@ -692,22 +691,21 @@ func (postgresDB *PostgresDB) GetColumns(db *sql.DB, table *Table, blackList map
func (*PostgresDB) GetGoDataType(sqlType string) (goType string) {
if v, ok := typeMappingPostgres[sqlType]; ok {
return v
} else {
ColorLog("[ERRO] data type (%s) not found!\n", sqlType)
os.Exit(2)
}
ColorLog("[ERRO] data type (%s) not found!\n", sqlType)
os.Exit(2)
return goType
}
// deleteAndRecreatePaths removes several directories completely
func createPaths(mode byte, paths *MvcPath) {
if (mode & O_MODEL) == O_MODEL {
if (mode & OModel) == OModel {
os.Mkdir(paths.ModelPath, 0777)
}
if (mode & O_CONTROLLER) == O_CONTROLLER {
if (mode & OController) == OController {
os.Mkdir(paths.ControllerPath, 0777)
}
if (mode & O_ROUTER) == O_ROUTER {
if (mode & ORouter) == ORouter {
os.Mkdir(paths.RouterPath, 0777)
}
}
@ -716,15 +714,15 @@ func createPaths(mode byte, paths *MvcPath) {
// It will wipe the following directories and recreate them:./models, ./controllers, ./routers
// Newly geneated files will be inside these folders.
func writeSourceFiles(pkgPath string, tables []*Table, mode byte, paths *MvcPath, selectedTables map[string]bool) {
if (O_MODEL & mode) == O_MODEL {
if (OModel & mode) == OModel {
ColorLog("[INFO] Creating model files...\n")
writeModelFiles(tables, paths.ModelPath, selectedTables)
}
if (O_CONTROLLER & mode) == O_CONTROLLER {
if (OController & mode) == OController {
ColorLog("[INFO] Creating controller files...\n")
writeControllerFiles(tables, paths.ControllerPath, selectedTables, pkgPath)
}
if (O_ROUTER & mode) == O_ROUTER {
if (ORouter & mode) == ORouter {
ColorLog("[INFO] Creating router files...\n")
writeRouterFile(tables, paths.RouterPath, selectedTables, pkgPath)
}
@ -764,9 +762,9 @@ func writeModelFiles(tables []*Table, mPath string, selectedTables map[string]bo
}
template := ""
if tb.Pk == "" {
template = STRUCT_MODEL_TPL
template = StructModelTPL
} else {
template = MODEL_TPL
template = ModelTPL
}
fileStr := strings.Replace(template, "{{modelStruct}}", tb.String(), 1)
fileStr = strings.Replace(fileStr, "{{modelName}}", camelCase(tb.Name), -1)
@ -825,7 +823,7 @@ func writeControllerFiles(tables []*Table, cPath string, selectedTables map[stri
continue
}
}
fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
fileStr := strings.Replace(CtrlTPL, "{{ctrlName}}", camelCase(tb.Name), -1)
fileStr = strings.Replace(fileStr, "{{pkgPath}}", pkgPath, -1)
if _, err := f.WriteString(fileStr); err != nil {
ColorLog("[ERRO] Could not write controller file to %s\n", fpath)
@ -851,13 +849,13 @@ func writeRouterFile(tables []*Table, rPath string, selectedTables map[string]bo
continue
}
// add name spaces
nameSpace := strings.Replace(NAMESPACE_TPL, "{{nameSpace}}", tb.Name, -1)
nameSpace := strings.Replace(NamespaceTPL, "{{nameSpace}}", tb.Name, -1)
nameSpace = strings.Replace(nameSpace, "{{ctrlName}}", camelCase(tb.Name), -1)
nameSpaces = append(nameSpaces, nameSpace)
}
// add export controller
fpath := path.Join(rPath, "router.go")
routerStr := strings.Replace(ROUTER_TPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1)
routerStr := strings.Replace(RouterTPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1)
routerStr = strings.Replace(routerStr, "{{pkgPath}}", pkgPath, 1)
var f *os.File
var err error
@ -1001,12 +999,12 @@ func getPackagePath(curpath string) (packpath string) {
}
const (
STRUCT_MODEL_TPL = `package models
StructModelTPL = `package models
{{importTimePkg}}
{{modelStruct}}
`
MODEL_TPL = `package models
ModelTPL = `package models
import (
"errors"
@ -1150,7 +1148,7 @@ func Delete{{modelName}}(id int) (err error) {
return
}
`
CTRL_TPL = `package controllers
CtrlTPL = `package controllers
import (
"{{pkgPath}}/models"
@ -1316,7 +1314,7 @@ func (c *{{ctrlName}}Controller) Delete() {
c.ServeJSON()
}
`
ROUTER_TPL = `// @APIVersion 1.0.0
RouterTPL = `// @APIVersion 1.0.0
// @Title beego Test API
// @Description beego has a very cool tools to autogenerate documents for your API
// @Contact astaxie@gmail.com
@ -1338,7 +1336,7 @@ func init() {
beego.AddNamespace(ns)
}
`
NAMESPACE_TPL = `
NamespaceTPL = `
beego.NSNamespace("/{{nameSpace}}",
beego.NSInclude(
&controllers.{{ctrlName}}Controller{},

View File

@ -393,7 +393,7 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat
if j == 0 || j == 1 {
st[j] = string(tmp)
tmp = make([]rune, 0)
j += 1
j++
start = false
if j == 1 {
continue
@ -655,16 +655,14 @@ func typeAnalyser(f *ast.Field) (isSlice bool, realType string) {
}
if star, ok := arr.Elt.(*ast.StarExpr); ok {
return true, fmt.Sprint(star.X)
} else {
return true, fmt.Sprint(arr.Elt)
}
} else {
switch t := f.Type.(type) {
case *ast.StarExpr:
return false, fmt.Sprint(t.X)
}
return false, fmt.Sprint(f.Type)
return true, fmt.Sprint(arr.Elt)
}
switch t := f.Type.(type) {
case *ast.StarExpr:
return false, fmt.Sprint(t.X)
}
return false, fmt.Sprint(f.Type)
}
func isBasicType(Type string) bool {
@ -688,7 +686,7 @@ var basicTypes = []string{
}
// regexp get json tag
func grepJsonTag(tag string) string {
func grepJSONTag(tag string) string {
r, _ := regexp.Compile(`json:"([^"]*)"`)
matches := r.FindAllStringSubmatch(tag, -1)
if len(matches) > 0 {

View File

@ -31,11 +31,11 @@ func generateHproseAppcode(driver, connStr, level, tables, currpath string) {
var mode byte
switch level {
case "1":
mode = O_MODEL
mode = OModel
case "2":
mode = O_MODEL | O_CONTROLLER
mode = OModel | OController
case "3":
mode = O_MODEL | O_CONTROLLER | O_ROUTER
mode = OModel | OController | ORouter
default:
ColorLog("[ERRO] Invalid 'level' option: %s\n", level)
ColorLog("[HINT] Level must be either 1, 2 or 3\n")
@ -90,7 +90,7 @@ func genHprose(dbms, connStr string, mode byte, selectedTableNames map[string]bo
// It will wipe the following directories and recreate them:./models, ./controllers, ./routers
// Newly geneated files will be inside these folders.
func writeHproseSourceFiles(pkgPath string, tables []*Table, mode byte, paths *MvcPath, selectedTables map[string]bool) {
if (O_MODEL & mode) == O_MODEL {
if (OModel & mode) == OModel {
ColorLog("[INFO] Creating model files...\n")
writeHproseModelFiles(tables, paths.ModelPath, selectedTables)
}
@ -130,10 +130,10 @@ func writeHproseModelFiles(tables []*Table, mPath string, selectedTables map[str
}
template := ""
if tb.Pk == "" {
template = HPROSE_STRUCT_MODEL_TPL
template = HproseStructModelTPL
} else {
template = HPROSE_MODEL_TPL
hproseAddFunctions = append(hproseAddFunctions, strings.Replace(HPROSE_ADDFUNCTION, "{{modelName}}", camelCase(tb.Name), -1))
template = HproseModelTPL
hproseAddFunctions = append(hproseAddFunctions, strings.Replace(HproseAddFunction, "{{modelName}}", camelCase(tb.Name), -1))
}
fileStr := strings.Replace(template, "{{modelStruct}}", tb.String(), 1)
fileStr = strings.Replace(fileStr, "{{modelName}}", camelCase(tb.Name), -1)
@ -157,7 +157,7 @@ func writeHproseModelFiles(tables []*Table, mPath string, selectedTables map[str
}
const (
HPROSE_ADDFUNCTION = `
HproseAddFunction = `
// publish about {{modelName}} function
service.AddFunction("Add{{modelName}}", models.Add{{modelName}})
service.AddFunction("Get{{modelName}}ById", models.Get{{modelName}}ById)
@ -166,12 +166,12 @@ const (
service.AddFunction("Delete{{modelName}}", models.Delete{{modelName}})
`
HPROSE_STRUCT_MODEL_TPL = `package models
HproseStructModelTPL = `package models
{{importTimePkg}}
{{modelStruct}}
`
HPROSE_MODEL_TPL = `package models
HproseModelTPL = `package models
import (
"errors"

View File

@ -23,15 +23,15 @@ import (
)
const (
M_PATH = "migrations"
M_DATE_FORMAT = "20060102_150405"
MPath = "migrations"
MDateFormat = "20060102_150405"
)
// generateMigration generates migration file template for database schema update.
// The generated file template consists of an up() method for updating schema and
// a down() method for reverting the update.
func generateMigration(mname, upsql, downsql, curpath string) {
migrationFilePath := path.Join(curpath, "database", M_PATH)
migrationFilePath := path.Join(curpath, "database", MPath)
if _, err := os.Stat(migrationFilePath); os.IsNotExist(err) {
// create migrations directory
if err := os.MkdirAll(migrationFilePath, 0777); err != nil {
@ -40,11 +40,11 @@ func generateMigration(mname, upsql, downsql, curpath string) {
}
}
// create file
today := time.Now().Format(M_DATE_FORMAT)
today := time.Now().Format(MDateFormat)
fpath := path.Join(migrationFilePath, fmt.Sprintf("%s_%s.go", today, mname))
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close()
content := strings.Replace(MIGRATION_TPL, "{{StructName}}", camelCase(mname)+"_"+today, -1)
content := strings.Replace(MigrationTPL, "{{StructName}}", camelCase(mname)+"_"+today, -1)
content = strings.Replace(content, "{{CurrTime}}", today, -1)
content = strings.Replace(content, "{{UpSQL}}", upsql, -1)
content = strings.Replace(content, "{{DownSQL}}", downsql, -1)
@ -59,7 +59,7 @@ func generateMigration(mname, upsql, downsql, curpath string) {
}
}
const MIGRATION_TPL = `package main
const MigrationTPL = `package main
import (
"github.com/astaxie/beego/migration"

View File

@ -2,10 +2,10 @@ package main
import (
"errors"
"fmt"
"os"
"path"
"strings"
"fmt"
)
func generateModel(mname, fields, crupath string) {
@ -16,7 +16,7 @@ func generateModel(mname, fields, crupath string) {
i := strings.LastIndex(p[:len(p)-1], "/")
packageName = p[i+1 : len(p)-1]
}
modelStruct, err, hastime := getStruct(modelName, fields)
modelStruct, hastime, err := getStruct(modelName, fields)
if err != nil {
ColorLog("[ERRO] Could not genrate models struct: %s\n", err)
os.Exit(2)
@ -53,9 +53,9 @@ func generateModel(mname, fields, crupath string) {
}
}
func getStruct(structname, fields string) (string, error, bool) {
func getStruct(structname, fields string) (string, bool, error) {
if fields == "" {
return "", errors.New("fields can't empty"), false
return "", false, errors.New("fields can't empty")
}
hastime := false
structStr := "type " + structname + " struct{\n"
@ -63,11 +63,11 @@ func getStruct(structname, fields string) (string, error, bool) {
for i, v := range fds {
kv := strings.SplitN(v, ":", 2)
if len(kv) != 2 {
return "", errors.New("the filds format is wrong. should key:type,key:type " + v), false
return "", false, errors.New("the filds format is wrong. should key:type,key:type " + v)
}
typ, tag, hastimeinner := getType(kv[1])
if typ == "" {
return "", errors.New("the filds format is wrong. should key:type,key:type " + v), false
return "", false, errors.New("the filds format is wrong. should key:type,key:type " + v)
}
if i == 0 && strings.ToLower(kv[0]) != "id" {
structStr = structStr + "Id int64 `orm:\"auto\"`\n"
@ -78,7 +78,7 @@ func getStruct(structname, fields string) (string, error, bool) {
structStr = structStr + camelString(kv[0]) + " " + typ + " " + tag + "\n"
}
structStr += "}\n"
return structStr, nil, hastime
return structStr, hastime, nil
}
// fields support type
@ -89,9 +89,8 @@ func getType(ktype string) (kt, tag string, hasTime bool) {
case "string":
if len(kv) == 2 {
return "string", "`orm:\"size(" + kv[1] + ")\"`", false
} else {
return "string", "`orm:\"size(128)\"`", false
}
return "string", "`orm:\"size(128)\"`", false
case "text":
return "string", "`orm:\"type(longtext)\"`", false
case "auto":

View File

@ -54,11 +54,11 @@ func generateSQLFromFields(fields string) string {
typ, tag := "", ""
switch driver {
case "mysql":
typ, tag = getSqlTypeMysql(kv[1])
typ, tag = getSQLTypeMysql(kv[1])
case "postgres":
typ, tag = getSqlTypePostgresql(kv[1])
typ, tag = getSQLTypePostgresql(kv[1])
default:
typ, tag = getSqlTypeMysql(kv[1])
typ, tag = getSQLTypeMysql(kv[1])
}
if typ == "" {
ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n")
@ -90,15 +90,14 @@ func generateSQLFromFields(fields string) string {
return sql
}
func getSqlTypeMysql(ktype string) (tp, tag string) {
func getSQLTypeMysql(ktype string) (tp, tag string) {
kv := strings.SplitN(ktype, ":", 2)
switch kv[0] {
case "string":
if len(kv) == 2 {
return "varchar(" + kv[1] + ") NOT NULL", ""
} else {
return "varchar(128) NOT NULL", ""
}
return "varchar(128) NOT NULL", ""
case "text":
return "longtext NOT NULL", ""
case "auto":
@ -121,15 +120,14 @@ func getSqlTypeMysql(ktype string) (tp, tag string) {
return "", ""
}
func getSqlTypePostgresql(ktype string) (tp, tag string) {
func getSQLTypePostgresql(ktype string) (tp, tag string) {
kv := strings.SplitN(ktype, ":", 2)
switch kv[0] {
case "string":
if len(kv) == 2 {
return "char(" + kv[1] + ") NOT NULL", ""
} else {
return "TEXT NOT NULL", ""
}
return "TEXT NOT NULL", ""
case "text":
return "TEXT NOT NULL", ""
case "auto", "pk":

View File

@ -167,23 +167,23 @@ func migrate(goal, crupath, driver, connStr string) {
// checkForSchemaUpdateTable checks the existence of migrations table.
// It checks for the proper table structures and creates the table using MYSQL_MIGRATION_DDL if it does not exist.
func checkForSchemaUpdateTable(db *sql.DB, driver string) {
showTableSql := showMigrationsTableSql(driver)
if rows, err := db.Query(showTableSql); err != nil {
showTableSQL := showMigrationsTableSQL(driver)
if rows, err := db.Query(showTableSQL); err != nil {
ColorLog("[ERRO] Could not show migrations table: %s\n", err)
os.Exit(2)
} else if !rows.Next() {
// no migrations table, create anew
createTableSql := createMigrationsTableSql(driver)
createTableSQL := createMigrationsTableSQL(driver)
ColorLog("[INFO] Creating 'migrations' table...\n")
if _, err := db.Query(createTableSql); err != nil {
if _, err := db.Query(createTableSQL); err != nil {
ColorLog("[ERRO] Could not create migrations table: %s\n", err)
os.Exit(2)
}
}
// checking that migrations table schema are expected
selectTableSql := selectMigrationsTableSql(driver)
if rows, err := db.Query(selectTableSql); err != nil {
selectTableSQL := selectMigrationsTableSQL(driver)
if rows, err := db.Query(selectTableSQL); err != nil {
ColorLog("[ERRO] Could not show columns of migrations table: %s\n", err)
os.Exit(2)
} else {
@ -219,7 +219,7 @@ func checkForSchemaUpdateTable(db *sql.DB, driver string) {
}
}
func showMigrationsTableSql(driver string) string {
func showMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
return "SHOW TABLES LIKE 'migrations'"
@ -230,18 +230,18 @@ func showMigrationsTableSql(driver string) string {
}
}
func createMigrationsTableSql(driver string) string {
func createMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
return MYSQL_MIGRATION_DDL
return MYSQLMigrationDDL
case "postgres":
return POSTGRES_MIGRATION_DDL
return POSTGRESMigrationDDL
default:
return MYSQL_MIGRATION_DDL
return MYSQLMigrationDDL
}
}
func selectMigrationsTableSql(driver string) string {
func selectMigrationsTableSQL(driver string) string {
switch driver {
case "mysql":
return "DESC migrations"
@ -290,7 +290,7 @@ func writeMigrationSourceFile(dir, source, driver, connStr string, latestTime in
ColorLog("[ERRO] Could not create file: %s\n", err)
os.Exit(2)
} else {
content := strings.Replace(MIGRATION_MAIN_TPL, "{{DBDriver}}", driver, -1)
content := strings.Replace(MigrationMainTPL, "{{DBDriver}}", driver, -1)
content = strings.Replace(content, "{{ConnStr}}", connStr, -1)
content = strings.Replace(content, "{{LatestTime}}", strconv.FormatInt(latestTime, 10), -1)
content = strings.Replace(content, "{{LatestName}}", latestName, -1)
@ -369,7 +369,7 @@ func formatShellOutput(o string) {
}
const (
MIGRATION_MAIN_TPL = `package main
MigrationMainTPL = `package main
import(
"os"
@ -408,7 +408,7 @@ func main(){
}
`
MYSQL_MIGRATION_DDL = `
MYSQLMigrationDDL = `
CREATE TABLE migrations (
id_migration int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'surrogate key',
name varchar(255) DEFAULT NULL COMMENT 'migration name, unique',
@ -420,7 +420,7 @@ CREATE TABLE migrations (
) ENGINE=InnoDB DEFAULT CHARSET=utf8
`
POSTGRES_MIGRATION_DDL = `
POSTGRESMigrationDDL = `
CREATE TYPE migrations_status AS ENUM('update', 'rollback');
CREATE TABLE migrations (

View File

@ -246,9 +246,8 @@ func (wft *walkFileTree) walkLeaf(fpath string, fi os.FileInfo, err error) error
}
wft.allfiles[name] = true
return err
} else {
return err
}
return err
}
func (wft *walkFileTree) iterDirectory(fpath string, fi os.FileInfo) error {

2
run.go
View File

@ -114,7 +114,7 @@ func runApp(cmd *Command, args []string) int {
if downdoc == "true" {
if _, err := os.Stat(path.Join(crupath, "swagger")); err != nil {
if os.IsNotExist(err) {
downloadFromUrl(swaggerlink, "swagger.zip")
downloadFromURL(swaggerlink, "swagger.zip")
unzipAndDelete("swagger.zip", "swagger")
}
}

View File

@ -59,7 +59,7 @@ func init() {
func runDocs(cmd *Command, args []string) int {
if isDownload == "true" {
downloadFromUrl(swaggerlink, "swagger.zip")
downloadFromURL(swaggerlink, "swagger.zip")
err := unzipAndDelete("swagger.zip", "swagger")
if err != nil {
fmt.Println("has err exet unzipAndDelete", err)
@ -77,7 +77,7 @@ func runDocs(cmd *Command, args []string) int {
return 0
}
func downloadFromUrl(url, fileName string) {
func downloadFromURL(url, fileName string) {
fmt.Println("Downloading", url, "to", fileName)
output, err := os.Create(fileName)