From 12cfffe4b218a0e0b538242f9f350559ea577aa9 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 14 Aug 2014 14:53:28 +0800 Subject: [PATCH 1/2] add onsite migrate function for scafolding feature & minor code refactor --- g.go | 32 +++++++++++++++++++++++++------- g_scaffold.go | 4 ++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/g.go b/g.go index 2ca735d..9a04626 100644 --- a/g.go +++ b/g.go @@ -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] diff --git a/g_scaffold.go b/g_scaffold.go index 8ec54ef..66ef8a3 100644 --- a/g_scaffold.go +++ b/g_scaffold.go @@ -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)) } From 08692c15d3624b06239641b768aac4a3161e80c0 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 14 Aug 2014 14:57:57 +0800 Subject: [PATCH 2/2] polish help messages --- g.go | 4 ++-- migrate.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/g.go b/g.go index 9a04626..2523e08 100644 --- a/g.go +++ b/g.go @@ -20,7 +20,7 @@ var cmdGenerate = &Command{ UsageLine: "generate [Command]", Short: "generate code based on application", Long: ` -bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test] +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 @@ -45,7 +45,7 @@ bee generate docs bee generate test [routerfile] generate testcase -bee generate appcode [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test] [-level=3] +bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3] generate appcode based on an existing database -tables: a list of table names separated by ',', default is empty, indicating all tables -driver: [mysql | postgresql | sqlite], the default is mysql diff --git a/migrate.go b/migrate.go index 4b2754b..ebc04fa 100644 --- a/migrate.go +++ b/migrate.go @@ -29,22 +29,22 @@ var cmdMigrate = &Command{ UsageLine: "migrate [Command]", Short: "run database migrations", Long: ` -bee migrate +bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] run all outstanding migrations -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 -bee migrate rollback +bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] rollback the last migration operation -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 -bee migrate reset +bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] rollback all migrations -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 -bee migrate refresh +bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] rollback all migrations and run them all again -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