1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 05:00:54 +00:00

do not remove folders

This commit is contained in:
ZhengYang 2014-08-01 17:42:04 +08:00
parent 789132d622
commit 348bc3edcb
2 changed files with 38 additions and 13 deletions

14
g.go
View File

@ -52,7 +52,6 @@ func init() {
} }
func generateCode(cmd *Command, args []string) { func generateCode(cmd *Command, args []string) {
cmd.Flag.Parse(args[1:])
curpath, _ := os.Getwd() curpath, _ := os.Getwd()
if len(args) < 1 { if len(args) < 1 {
ColorLog("[ERRO] command is missing\n") ColorLog("[ERRO] command is missing\n")
@ -72,6 +71,19 @@ func generateCode(cmd *Command, args []string) {
case "docs": case "docs":
generateDocs(curpath) generateDocs(curpath)
case "model": case "model":
cmd.Flag.Parse(args[1:])
if driver == "" {
driver = "mysql"
}
if conn == "" {
conn = "root:@tcp(127.0.0.1:3306)/test"
}
if level == "" {
level = "1"
}
ColorLog("[INFO] Using '%s' as 'driver'\n", driver)
ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
ColorLog("[INFO] Using '%s' as 'level'\n", level)
generateModel(string(driver), string(conn), string(level), curpath) generateModel(string(driver), string(conn), string(level), curpath)
default: default:
ColorLog("[ERRO] command is missing\n") ColorLog("[ERRO] command is missing\n")

View File

@ -198,7 +198,8 @@ func generateModel(driver string, connStr string, level string, currpath string)
func gen(dbms string, connStr string, mode byte, currpath string) { func gen(dbms string, connStr string, mode byte, currpath string) {
db, err := sql.Open(dbms, connStr) db, err := sql.Open(dbms, connStr)
if err != nil { if err != nil {
fmt.Printf("error opening database: %v\n", err) ColorLog("[ERRO] Could not connect to %s: %s\n", dbms, connStr)
os.Exit(2)
} }
defer db.Close() defer db.Close()
tableNames := getTableNames(db) tableNames := getTableNames(db)
@ -213,12 +214,18 @@ func gen(dbms string, connStr string, mode byte, currpath string) {
// getTables gets a list table names in current database // getTables gets a list table names in current database
func getTableNames(db *sql.DB) (tables []string) { func getTableNames(db *sql.DB) (tables []string) {
rows, _ := db.Query("SHOW TABLES") rows, err := db.Query("SHOW TABLES")
if err != nil {
ColorLog("[ERRO] Could not show tables\n")
ColorLog("[HINT] Check your connection string\n")
os.Exit(2)
}
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var name string var name string
if err := rows.Scan(&name); err != nil { if err := rows.Scan(&name); err != nil {
fmt.Printf("error showing tables: %v\n", err) ColorLog("[ERRO] Could not show tables\n")
os.Exit(2)
} }
tables = append(tables, name) tables = append(tables, name)
} }
@ -261,12 +268,14 @@ func getConstraints(db *sql.DB, table *Table, blackList map[string]bool) {
c.table_schema = database() AND c.table_name = ? AND u.table_schema = database() AND u.table_name = ?`, c.table_schema = database() AND c.table_name = ? AND u.table_schema = database() AND u.table_name = ?`,
table.Name, table.Name) // u.position_in_unique_constraint, table.Name, table.Name) // u.position_in_unique_constraint,
if err != nil { if err != nil {
fmt.Printf("constraint query error: %v\n", err) ColorLog("[ERRO] Could not query INFORMATION_SCHEMA for PK/UK/FK information\n")
os.Exit(2)
} }
for rows.Next() { for rows.Next() {
var constraintTypeBytes, columnNameBytes, refTableSchemaBytes, refTableNameBytes, refColumnNameBytes, refOrdinalPosBytes []byte var constraintTypeBytes, columnNameBytes, refTableSchemaBytes, refTableNameBytes, refColumnNameBytes, refOrdinalPosBytes []byte
if err := rows.Scan(&constraintTypeBytes, &columnNameBytes, &refTableSchemaBytes, &refTableNameBytes, &refColumnNameBytes, &refOrdinalPosBytes); err != nil { if err := rows.Scan(&constraintTypeBytes, &columnNameBytes, &refTableSchemaBytes, &refTableNameBytes, &refColumnNameBytes, &refOrdinalPosBytes); err != nil {
fmt.Println("constraint error: %v\n", err) ColorLog("[ERRO] Could not read INFORMATION_SCHEMA for PK/UK/FK information\n")
os.Exit(2)
} }
constraintType, columnName, refTableSchema, refTableName, refColumnName, refOrdinalPos := constraintType, columnName, refTableSchema, refTableName, refColumnName, refOrdinalPos :=
string(constraintTypeBytes), string(columnNameBytes), string(refTableSchemaBytes), string(constraintTypeBytes), string(columnNameBytes), string(refTableSchemaBytes),
@ -310,7 +319,8 @@ func getColumns(db *sql.DB, table *Table, blackList map[string]bool) {
// datatype as bytes so that SQL <null> values can be retrieved // datatype as bytes so that SQL <null> values can be retrieved
var colNameBytes, dataTypeBytes, columnTypeBytes, isNullableBytes, columnDefaultBytes, extraBytes []byte var colNameBytes, dataTypeBytes, columnTypeBytes, isNullableBytes, columnDefaultBytes, extraBytes []byte
if err := colDefRows.Scan(&colNameBytes, &dataTypeBytes, &columnTypeBytes, &isNullableBytes, &columnDefaultBytes, &extraBytes); err != nil { if err := colDefRows.Scan(&colNameBytes, &dataTypeBytes, &columnTypeBytes, &isNullableBytes, &columnDefaultBytes, &extraBytes); err != nil {
fmt.Printf("column error: %v\n", err) ColorLog("[ERRO] Could not query INFORMATION_SCHEMA for column information\n")
os.Exit(2)
} }
colName, dataType, columnType, isNullable, columnDefault, extra := colName, dataType, columnType, isNullable, columnDefault, extra :=
string(colNameBytes), string(dataTypeBytes), string(columnTypeBytes), string(isNullableBytes), string(columnDefaultBytes), string(extraBytes) string(colNameBytes), string(dataTypeBytes), string(columnTypeBytes), string(isNullableBytes), string(columnDefaultBytes), string(extraBytes)
@ -380,15 +390,15 @@ func getColumns(db *sql.DB, table *Table, blackList map[string]bool) {
// deleteAndRecreatePaths removes several directories completely // deleteAndRecreatePaths removes several directories completely
func deleteAndRecreatePaths(mode byte, paths *MvcPath) { func deleteAndRecreatePaths(mode byte, paths *MvcPath) {
if (mode & O_MODEL) == O_MODEL { if (mode & O_MODEL) == O_MODEL {
os.RemoveAll(paths.ModelPath) //os.RemoveAll(paths.ModelPath)
os.Mkdir(paths.ModelPath, 0777) os.Mkdir(paths.ModelPath, 0777)
} }
if (mode & O_CONTROLLER) == O_CONTROLLER { if (mode & O_CONTROLLER) == O_CONTROLLER {
os.RemoveAll(paths.ControllerPath) //os.RemoveAll(paths.ControllerPath)
os.Mkdir(paths.ControllerPath, 0777) os.Mkdir(paths.ControllerPath, 0777)
} }
if (mode & O_ROUTER) == O_ROUTER { if (mode & O_ROUTER) == O_ROUTER {
os.RemoveAll(paths.RouterPath) //os.RemoveAll(paths.RouterPath)
os.Mkdir(paths.RouterPath, 0777) os.Mkdir(paths.RouterPath, 0777)
} }
} }
@ -424,7 +434,8 @@ func writeModelFiles(tables []*Table, mPath string) {
fileStr := strings.Replace(template, "{{modelStruct}}", tb.String(), 1) fileStr := strings.Replace(template, "{{modelStruct}}", tb.String(), 1)
fileStr = strings.Replace(fileStr, "{{modelName}}", camelCase(tb.Name), -1) fileStr = strings.Replace(fileStr, "{{modelName}}", camelCase(tb.Name), -1)
if _, err := f.WriteString(fileStr); err != nil { if _, err := f.WriteString(fileStr); err != nil {
fmt.Printf("error writing file(%s): %v", fpath, err) ColorLog("[ERRO] Could not write model file to %s\n", fpath)
os.Exit(2)
} }
formatAndFixImports(fpath) formatAndFixImports(fpath)
} }
@ -442,7 +453,8 @@ func writeControllerFiles(tables []*Table, cPath string) {
defer f.Close() defer f.Close()
fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1) fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
if _, err := f.WriteString(fileStr); err != nil { if _, err := f.WriteString(fileStr); err != nil {
fmt.Printf("error writing file(%s): %v", fpath, err) ColorLog("[ERRO] Could not write controller file to %s\n", fpath)
os.Exit(2)
} }
formatAndFixImports(fpath) formatAndFixImports(fpath)
} }
@ -466,7 +478,8 @@ func writeRouterFile(tables []*Table, rPath string) {
f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
defer f.Close() defer f.Close()
if _, err := f.WriteString(routerStr); err != nil { if _, err := f.WriteString(routerStr); err != nil {
fmt.Println("error writing file(%s): %v", fpath, err) ColorLog("[ERRO] Could not write router file to %s\n", fpath)
os.Exit(2)
} }
formatAndFixImports(fpath) formatAndFixImports(fpath)
} }