1
0
mirror of https://github.com/beego/bee.git synced 2024-11-24 08:30:53 +00:00

getting params from commandline and hook up the actual function

This commit is contained in:
ZhengYang 2014-08-01 15:52:25 +08:00
parent 0a22533e04
commit 8e56c8528f
2 changed files with 27 additions and 18 deletions

20
g.go
View File

@ -20,7 +20,7 @@ var cmdGenerate = &Command{
UsageLine: "generate [Command]",
Short: "generate code based on application",
Long: `
bee generate model [modelfile] [dbconfig]
bee generate model driver [dbconfig]
generate model base on struct
bee generate controller [modelfile]
generate RESTFul controllers based on modelfile
@ -33,7 +33,19 @@ bee generate test [routerfile]
`,
}
var driver docValue
var conn docValue
var level docValue
func init() {
cmdGenerate.Run = generateCode
cmdGenerate.Flag.Var(&driver, "driver", "database driver: mysql, postgresql, etc.")
cmdGenerate.Flag.Var(&conn, "conn", "connection string used by the driver to connect to a database instance")
cmdGenerate.Flag.Var(&level, "level", "1 = models only; 2 = models and controllers; 3 = models, controllers and routers")
}
func generateCode(cmd *Command, args []string) {
cmd.Flag.Parse(args[1:])
curpath, _ := os.Getwd()
if len(args) < 1 {
ColorLog("[ERRO] command is missing\n")
@ -53,11 +65,7 @@ func generateCode(cmd *Command, args []string) {
case "docs":
generateDocs(curpath)
case "model":
generateModel("mysql", "root@tcp(127.0.0.1:3306)/sgfas?charset=utf8", curpath)
case "controller":
generateController("mysql", "", curpath)
case "router":
generateRouter("mysql", "", curpath)
generateModel(string(driver), string(conn), string(level), curpath)
default:
ColorLog("[ERRO] command is missing\n")
}

View File

@ -177,18 +177,19 @@ func (tag *OrmTag) String() string {
return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";"))
}
func generateModel(driver string, connStr string, currpath string) {
mode := O_MODEL
gen(driver, connStr, mode, currpath)
}
func generateController(driver string, connStr string, currpath string) {
mode := O_MODEL | O_CONTROLLER
gen(driver, connStr, mode, currpath)
}
func generateRouter(driver string, connStr string, currpath string) {
mode := O_MODEL | O_CONTROLLER | O_ROUTER
func generateModel(driver string, connStr string, level string, currpath string) {
var mode byte
if level == "1" {
mode = O_MODEL
} else if level == "2" {
mode = O_MODEL | O_CONTROLLER
} else if level == "3" {
mode = O_MODEL | O_CONTROLLER | O_ROUTER
} else {
ColorLog("[ERRO] Invalid 'level' option: %s\n", level)
ColorLog("[HINT] Level must be either 1, 2 or 3\n")
os.Exit(2)
}
gen(driver, connStr, mode, currpath)
}