mirror of
https://github.com/beego/bee.git
synced 2024-11-23 01:30:55 +00:00
add onsite migrate function for scafolding feature & minor code refactor
This commit is contained in:
parent
abc40f79a8
commit
12cfffe4b2
26
g.go
26
g.go
@ -20,9 +20,11 @@ var cmdGenerate = &Command{
|
|||||||
UsageLine: "generate [Command]",
|
UsageLine: "generate [Command]",
|
||||||
Short: "generate code based on application",
|
Short: "generate code based on application",
|
||||||
Long: `
|
Long: `
|
||||||
bee generate scaffold [scaffoldname] [-fields=""]
|
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。
|
The generate scaffold command will do a number of things for you.
|
||||||
-fields: a list of database fields.
|
-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"
|
example: bee generate scaffold post -fields="title:string,body:text"
|
||||||
|
|
||||||
bee generate model [modelname] [-fields=""]
|
bee generate model [modelname] [-fields=""]
|
||||||
@ -90,7 +92,23 @@ func generateCode(cmd *Command, args []string) {
|
|||||||
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"\"]\n")
|
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"\"]\n")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
err := loadConfig()
|
||||||
|
if err != nil {
|
||||||
|
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
|
||||||
|
}
|
||||||
cmd.Flag.Parse(args[2:])
|
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 == "" {
|
if fields == "" {
|
||||||
ColorLog("[ERRO] Wrong number of arguments\n")
|
ColorLog("[ERRO] Wrong number of arguments\n")
|
||||||
ColorLog("[HINT] Usage: bee generate scaffold [scaffoldname] [-fields=\"title:string,body:text\"]\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]
|
sname := args[1]
|
||||||
ColorLog("[INFO] Using '%s' as scaffold name\n", sname)
|
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":
|
case "docs":
|
||||||
generateDocs(curpath)
|
generateDocs(curpath)
|
||||||
case "appcode":
|
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 'conn'\n", conn)
|
||||||
ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
|
ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
|
||||||
ColorLog("[INFO] Using '%s' as 'level'\n", level)
|
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":
|
case "migration":
|
||||||
if len(args) == 2 {
|
if len(args) == 2 {
|
||||||
mname := args[1]
|
mname := args[1]
|
||||||
|
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
func generateScaffold(sname, fields, crupath string) {
|
func generateScaffold(sname, fields, crupath, driver, conn string) {
|
||||||
// generate model
|
// generate model
|
||||||
ColorLog("[INFO] Do you want me to create a %v model? [yes|no]] ", sname)
|
ColorLog("[INFO] Do you want me to create a %v model? [yes|no]] ", sname)
|
||||||
if askForConfirmation() {
|
if askForConfirmation() {
|
||||||
@ -27,7 +27,7 @@ func generateScaffold(sname, fields, crupath string) {
|
|||||||
// run migration
|
// run migration
|
||||||
ColorLog("[INFO] Do you want to go ahead and migrate the database? [yes|no]] ")
|
ColorLog("[INFO] Do you want to go ahead and migrate the database? [yes|no]] ")
|
||||||
if askForConfirmation() {
|
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))
|
ColorLog("[INFO] All done! Don't forget to add beego.Router(\"/%v\" ,&controllers.%vController{}) to routers/route.go\n", sname, strings.Title(sname))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user