bee/g.go

110 lines
3.4 KiB
Go
Raw Normal View History

2014-06-19 13:09:17 +00:00
// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
import "os"
var cmdGenerate = &Command{
UsageLine: "generate [Command]",
Short: "generate code based on application",
Long: `
2014-08-07 08:39:28 +00:00
bee generate model [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test] [-level=1]
generate model based on an existing database
2014-08-07 08:39:28 +00:00
-tables: a list of table names separated by ',', default is empty, indicating all tables
-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
-level: [1 | 2 | 3], 1 = model; 2 = models,controller; 3 = models,controllers,router
bee generate migration [filename]
generate migration file for making database schema update
2014-08-01 06:49:30 +00:00
bee generate controller [modelfile]
2014-08-01 02:15:32 +00:00
generate RESTFul controllers based on modelfile
2014-08-01 06:49:30 +00:00
bee generate router [controllerfile]
2014-08-01 02:15:32 +00:00
generate router based on controllerfile
2014-08-01 06:49:30 +00:00
bee generate docs
2014-08-01 02:15:32 +00:00
generate swagger doc file
2014-08-01 06:49:30 +00:00
bee generate test [routerfile]
2014-08-01 02:15:32 +00:00
generate testcase
`,
}
var driver docValue
var conn docValue
var level docValue
2014-08-07 08:39:28 +00:00
var tables docValue
func init() {
cmdGenerate.Run = generateCode
2014-08-07 08:39:28 +00:00
cmdGenerate.Flag.Var(&tables, "tables", "specify tables to generate model")
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) {
curpath, _ := os.Getwd()
if len(args) < 1 {
ColorLog("[ERRO] command is missing\n")
os.Exit(2)
}
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
ColorLog("[ERRO] $GOPATH not found\n")
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
os.Exit(2)
}
gcmd := args[0]
switch gcmd {
case "docs":
generateDocs(curpath)
2014-07-31 10:46:03 +00:00
case "model":
2014-08-01 09:42:04 +00:00
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)
2014-08-07 08:39:28 +00:00
ColorLog("[INFO] Using '%s' as 'tables'", tables)
2014-08-01 09:42:04 +00:00
ColorLog("[INFO] Using '%s' as 'level'\n", level)
2014-08-07 08:39:28 +00:00
generateModel(string(driver), string(conn), string(level), string(tables), curpath)
case "migration":
if len(args) == 2 {
2014-08-07 05:52:28 +00:00
mname := args[1]
ColorLog("[INFO] Using '%s' as migration name\n", mname)
generateMigration(mname, curpath)
} else {
ColorLog("[ERRO] Wrong number of arguments\n")
2014-08-07 02:48:58 +00:00
ColorLog("[HINT] Usage: bee generate migration [filename]\n")
os.Exit(2)
}
default:
ColorLog("[ERRO] command is missing\n")
}
ColorLog("[SUCC] generate successfully created!\n")
}