1
0
mirror of https://github.com/beego/bee.git synced 2024-12-22 20:20:49 +00:00

add onsite migrate function for scafolding feature & minor code refactor

This commit is contained in:
ZhengYang 2014-08-14 14:53:28 +08:00
parent abc40f79a8
commit 12cfffe4b2
2 changed files with 27 additions and 9 deletions

32
g.go
View File

@ -20,13 +20,15 @@ var cmdGenerate = &Command{
UsageLine: "generate [Command]",
Short: "generate code based on application",
Long: `
bee generate scaffold [scaffoldname] [-fields=""]
The generate scaffold command will do a number of things for you
-fields: a list of database fields.
example: bee generate scaffold post -fields="title:string,body:text"
bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
The generate scaffold command will do a number of things for you.
-fields: a list of database fields.
-driver: [mysql | postgresql | sqlite], the default is mysql
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
example: bee generate scaffold post -fields="title:string,body:text"
bee generate model [modelname] [-fields=""]
generate RESTFul model based on fields
generate RESTFul model based on fields
bee generate controller [controllerfile]
generate RESTFul controllers
@ -90,7 +92,23 @@ func generateCode(cmd *Command, args []string) {
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"\"]\n")
os.Exit(2)
}
err := loadConfig()
if err != nil {
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
}
cmd.Flag.Parse(args[2:])
if driver == "" {
driver = docValue(conf.Database.Driver)
if driver == "" {
driver = "mysql"
}
}
if conn == "" {
conn = docValue(conf.Database.Conn)
if conn == "" {
conn = "root:@tcp(127.0.0.1:3306)/test"
}
}
if fields == "" {
ColorLog("[ERRO] Wrong number of arguments\n")
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"title:string,body:text\"]\n")
@ -98,7 +116,7 @@ func generateCode(cmd *Command, args []string) {
}
sname := args[1]
ColorLog("[INFO] Using '%s' as scaffold name\n", sname)
generateScaffold(sname, fields.String(), curpath)
generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String())
case "docs":
generateDocs(curpath)
case "appcode":
@ -127,7 +145,7 @@ func generateCode(cmd *Command, args []string) {
ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
ColorLog("[INFO] Using '%s' as 'level'\n", level)
generateAppcode(string(driver), string(conn), string(level), string(tables), curpath)
generateAppcode(driver.String(), conn.String(), level.String(), tables.String(), curpath)
case "migration":
if len(args) == 2 {
mname := args[1]

View File

@ -2,7 +2,7 @@ package main
import "strings"
func generateScaffold(sname, fields, crupath string) {
func generateScaffold(sname, fields, crupath, driver, conn string) {
// generate model
ColorLog("[INFO] Do you want me to create a %v model? [yes|no]] ", sname)
if askForConfirmation() {
@ -27,7 +27,7 @@ func generateScaffold(sname, fields, crupath string) {
// run migration
ColorLog("[INFO] Do you want to go ahead and migrate the database? [yes|no]] ")
if askForConfirmation() {
// @todo
migrateUpdate(crupath, driver, conn)
}
ColorLog("[INFO] All done! Don't forget to add beego.Router(\"/%v\" ,&controllers.%vController{}) to routers/route.go\n", sname, strings.Title(sname))
}