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.
|
|
|
|
|
2014-06-18 04:19:03 +00:00
|
|
|
package main
|
|
|
|
|
2016-06-01 12:30:29 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
2014-06-18 04:19:03 +00:00
|
|
|
|
|
|
|
var cmdGenerate = &Command{
|
2016-12-03 10:54:41 +00:00
|
|
|
UsageLine: "generate [command]",
|
|
|
|
Short: "Source code generator",
|
|
|
|
Long: `▶ {{"To scaffold out your entire application:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate scaffold [scaffoldname] [-fields="title:string,body:text"] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
|
|
|
|
|
|
|
▶ {{"To generate a Model based on fields:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate model [modelname] [-fields="name:type"]
|
|
|
|
|
|
|
|
▶ {{"To generate a controller:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate controller [controllerfile]
|
|
|
|
|
|
|
|
▶ {{"To generate a CRUD view:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate view [viewpath]
|
|
|
|
|
|
|
|
▶ {{"To generate a migration file for making database schema updates:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate migration [migrationfile] [-fields="name:type"]
|
|
|
|
|
|
|
|
▶ {{"To generate swagger doc file:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate docs
|
|
|
|
|
|
|
|
▶ {{"To generate a test case:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate test [routerfile]
|
|
|
|
|
|
|
|
▶ {{"To generate appcode based on an existing database:"|bold}}
|
|
|
|
|
|
|
|
$ bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3]
|
2014-06-18 04:19:03 +00:00
|
|
|
`,
|
2016-12-03 10:54:41 +00:00
|
|
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
|
|
|
Run: generateCode,
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 07:52:25 +00:00
|
|
|
var driver docValue
|
|
|
|
var conn docValue
|
|
|
|
var level docValue
|
2014-08-07 08:39:28 +00:00
|
|
|
var tables docValue
|
2014-08-11 09:01:00 +00:00
|
|
|
var fields docValue
|
2014-08-01 07:52:25 +00:00
|
|
|
|
|
|
|
func init() {
|
2016-12-03 10:54:41 +00:00
|
|
|
cmdGenerate.Flag.Var(&tables, "tables", "List of table names separated by a comma.")
|
|
|
|
cmdGenerate.Flag.Var(&driver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
|
|
|
cmdGenerate.Flag.Var(&conn, "conn", "Connection string used by the driver to connect to a database instance.")
|
|
|
|
cmdGenerate.Flag.Var(&level, "level", "Either 1, 2 or 3. i.e. 1=models; 2=models and controllers; 3=models, controllers and routers.")
|
|
|
|
cmdGenerate.Flag.Var(&fields, "fields", "List of table fields.")
|
2014-08-01 07:52:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 09:38:51 +00:00
|
|
|
func generateCode(cmd *Command, args []string) int {
|
2016-08-01 09:42:16 +00:00
|
|
|
currpath, _ := os.Getwd()
|
2014-06-18 04:19:03 +00:00
|
|
|
if len(args) < 1 {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("Command is missing")
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 09:42:16 +00:00
|
|
|
gps := GetGOPATHs()
|
|
|
|
if len(gps) == 0 {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("GOPATH environment variable is not set or empty")
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
2016-11-13 14:14:48 +00:00
|
|
|
|
2016-08-01 09:42:16 +00:00
|
|
|
gopath := gps[0]
|
2016-11-13 14:14:48 +00:00
|
|
|
|
2016-11-14 17:02:29 +00:00
|
|
|
logger.Debugf("GOPATH: %s", __FILE__(), __LINE__(), gopath)
|
2014-06-18 04:19:03 +00:00
|
|
|
|
|
|
|
gcmd := args[0]
|
|
|
|
switch gcmd {
|
2014-08-11 09:01:00 +00:00
|
|
|
case "scaffold":
|
|
|
|
if len(args) < 2 {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("Wrong number of arguments. Run: bee help generate")
|
2014-08-11 09:01:00 +00:00
|
|
|
}
|
2016-11-13 14:14:48 +00:00
|
|
|
// Load the configuration
|
2014-08-14 06:53:28 +00:00
|
|
|
err := loadConfig()
|
|
|
|
if err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatalf("Failed to load configuration: %s", err)
|
2014-08-14 06:53:28 +00:00
|
|
|
}
|
2014-08-11 09:01:00 +00:00
|
|
|
cmd.Flag.Parse(args[2:])
|
2014-08-14 06:53:28 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 09:01:00 +00:00
|
|
|
if fields == "" {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Hint("fields option should not be empty, i.e. -fields=\"title:string,body:text\"")
|
|
|
|
logger.Fatal("Wrong number of arguments. Run: bee help generate")
|
2014-08-11 09:01:00 +00:00
|
|
|
}
|
|
|
|
sname := args[1]
|
2016-08-01 09:42:16 +00:00
|
|
|
generateScaffold(sname, fields.String(), currpath, driver.String(), conn.String())
|
2014-06-18 04:19:03 +00:00
|
|
|
case "docs":
|
2016-08-01 09:42:16 +00:00
|
|
|
generateDocs(currpath)
|
2014-08-11 09:01:00 +00:00
|
|
|
case "appcode":
|
2016-11-13 14:14:48 +00:00
|
|
|
// Load the configuration
|
2014-08-13 06:48:20 +00:00
|
|
|
err := loadConfig()
|
|
|
|
if err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatalf("Failed to load configuration: %s", err)
|
2014-08-13 06:48:20 +00:00
|
|
|
}
|
2014-08-01 09:42:04 +00:00
|
|
|
cmd.Flag.Parse(args[1:])
|
|
|
|
if driver == "" {
|
2014-08-13 06:48:20 +00:00
|
|
|
driver = docValue(conf.Database.Driver)
|
|
|
|
if driver == "" {
|
|
|
|
driver = "mysql"
|
|
|
|
}
|
2014-08-01 09:42:04 +00:00
|
|
|
}
|
|
|
|
if conn == "" {
|
2014-08-13 06:48:20 +00:00
|
|
|
conn = docValue(conf.Database.Conn)
|
|
|
|
if conn == "" {
|
2014-08-21 04:22:49 +00:00
|
|
|
if driver == "mysql" {
|
|
|
|
conn = "root:@tcp(127.0.0.1:3306)/test"
|
|
|
|
} else if driver == "postgres" {
|
|
|
|
conn = "postgres://postgres:postgres@127.0.0.1:5432/postgres"
|
|
|
|
}
|
2014-08-13 06:48:20 +00:00
|
|
|
}
|
2014-08-01 09:42:04 +00:00
|
|
|
}
|
|
|
|
if level == "" {
|
2014-08-13 06:13:07 +00:00
|
|
|
level = "3"
|
2014-08-01 09:42:04 +00:00
|
|
|
}
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Infof("Using '%s' as 'driver'", driver)
|
|
|
|
logger.Infof("Using '%s' as 'conn'", conn)
|
|
|
|
logger.Infof("Using '%s' as 'tables'", tables)
|
|
|
|
logger.Infof("Using '%s' as 'level'", level)
|
2016-08-01 09:42:16 +00:00
|
|
|
generateAppcode(driver.String(), conn.String(), level.String(), tables.String(), currpath)
|
2014-08-07 02:47:06 +00:00
|
|
|
case "migration":
|
2014-08-18 03:51:57 +00:00
|
|
|
if len(args) < 2 {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("Wrong number of arguments. Run: bee help generate")
|
2014-08-07 02:47:06 +00:00
|
|
|
}
|
2014-08-18 03:51:57 +00:00
|
|
|
cmd.Flag.Parse(args[2:])
|
|
|
|
mname := args[1]
|
2016-11-13 14:14:48 +00:00
|
|
|
|
|
|
|
logger.Infof("Using '%s' as migration name", mname)
|
|
|
|
|
2014-08-18 03:51:57 +00:00
|
|
|
upsql := ""
|
2014-08-18 04:21:21 +00:00
|
|
|
downsql := ""
|
2014-08-18 03:51:57 +00:00
|
|
|
if fields != "" {
|
2016-07-23 18:05:44 +00:00
|
|
|
dbMigrator := newDBDriver()
|
|
|
|
upsql = dbMigrator.generateCreateUp(mname)
|
|
|
|
downsql = dbMigrator.generateCreateDown(mname)
|
2014-08-18 03:51:57 +00:00
|
|
|
}
|
2016-08-01 09:42:16 +00:00
|
|
|
generateMigration(mname, upsql, downsql, currpath)
|
2014-08-08 17:37:55 +00:00
|
|
|
case "controller":
|
|
|
|
if len(args) == 2 {
|
|
|
|
cname := args[1]
|
2016-08-01 09:42:16 +00:00
|
|
|
generateController(cname, currpath)
|
2014-08-08 17:37:55 +00:00
|
|
|
} else {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("Wrong number of arguments. Run: bee help generate")
|
2014-08-11 09:01:00 +00:00
|
|
|
}
|
|
|
|
case "model":
|
|
|
|
if len(args) < 2 {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("Wrong number of arguments. Run: bee help generate")
|
2014-08-11 09:01:00 +00:00
|
|
|
}
|
|
|
|
cmd.Flag.Parse(args[2:])
|
|
|
|
if fields == "" {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Hint("fields option should not be empty, i.e. -fields=\"title:string,body:text\"")
|
|
|
|
logger.Fatal("Wrong number of arguments. Run: bee help generate")
|
2014-08-08 17:37:55 +00:00
|
|
|
}
|
2014-08-11 09:01:00 +00:00
|
|
|
sname := args[1]
|
2016-08-01 09:42:16 +00:00
|
|
|
generateModel(sname, fields.String(), currpath)
|
2014-08-11 02:01:56 +00:00
|
|
|
case "view":
|
|
|
|
if len(args) == 2 {
|
|
|
|
cname := args[1]
|
2016-08-01 09:42:16 +00:00
|
|
|
generateView(cname, currpath)
|
2014-08-11 02:01:56 +00:00
|
|
|
} else {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("Wrong number of arguments. Run: bee help generate")
|
2014-08-11 02:01:56 +00:00
|
|
|
}
|
2014-06-18 04:19:03 +00:00
|
|
|
default:
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatal("Command is missing")
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Successf("%s successfully generated!", strings.Title(gcmd))
|
2014-08-15 09:38:51 +00:00
|
|
|
return 0
|
2014-06-18 04:19:03 +00:00
|
|
|
}
|