mirror of
https://github.com/beego/bee.git
synced 2024-11-22 05:00:54 +00:00
Enhances the commands short and long description
This enhances the output of bee help/usage by using colored and bold text to highlight examples.
This commit is contained in:
parent
196e732e19
commit
e927a9193d
89
apiapp.go
89
apiapp.go
@ -24,39 +24,34 @@ import (
|
|||||||
var cmdApiapp = &Command{
|
var cmdApiapp = &Command{
|
||||||
// CustomFlags: true,
|
// CustomFlags: true,
|
||||||
UsageLine: "api [appname]",
|
UsageLine: "api [appname]",
|
||||||
Short: "create an API beego application",
|
Short: "Creates a Beego API application",
|
||||||
Long: `
|
Long: `
|
||||||
Create an API beego application.
|
The command 'api' creates a Beego API application.
|
||||||
|
|
||||||
bee api [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
|
{{"Example:"|bold}}
|
||||||
-tables: a list of table names separated by ',' (default is empty, indicating all tables)
|
$ bee api [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
|
||||||
-driver: [mysql | postgres | sqlite] (default: mysql)
|
|
||||||
-conn: the connection string used by the driver, the default is ''
|
|
||||||
e.g. for mysql: root:@tcp(127.0.0.1:3306)/test
|
|
||||||
e.g. for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
|
|
||||||
|
|
||||||
If 'conn' argument is empty, bee api creates an example API application,
|
If 'conn' argument is empty, the command will generate an example API application. Otherwise the command
|
||||||
when 'conn' argument is provided, bee api generates an API application based
|
will connect to your database and generate models based on the existing tables.
|
||||||
on the existing database.
|
|
||||||
|
|
||||||
The command 'api' creates a folder named [appname] and inside the folder deploy
|
The command 'api' creates a folder named [appname] with the following structure:
|
||||||
the following files/directories structure:
|
|
||||||
|
|
||||||
├── conf
|
|
||||||
│ └── app.conf
|
|
||||||
├── controllers
|
|
||||||
│ └── object.go
|
|
||||||
│ └── user.go
|
|
||||||
├── routers
|
|
||||||
│ └── router.go
|
|
||||||
├── tests
|
|
||||||
│ └── default_test.go
|
|
||||||
├── main.go
|
|
||||||
└── models
|
|
||||||
└── object.go
|
|
||||||
└── user.go
|
|
||||||
|
|
||||||
|
├── conf
|
||||||
|
│ └── app.conf
|
||||||
|
├── controllers
|
||||||
|
│ └── object.go
|
||||||
|
│ └── user.go
|
||||||
|
├── routers
|
||||||
|
│ └── router.go
|
||||||
|
├── tests
|
||||||
|
│ └── default_test.go
|
||||||
|
├── main.go
|
||||||
|
└── models
|
||||||
|
└── object.go
|
||||||
|
└── user.go
|
||||||
`,
|
`,
|
||||||
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: createapi,
|
||||||
}
|
}
|
||||||
|
|
||||||
var apiconf = `appname = {{.Appname}}
|
var apiconf = `appname = {{.Appname}}
|
||||||
@ -537,15 +532,13 @@ func TestGet(t *testing.T) {
|
|||||||
`
|
`
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdApiapp.Run = createapi
|
cmdApiapp.Flag.Var(&tables, "tables", "List of table names separated by a comma.")
|
||||||
cmdApiapp.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
cmdApiapp.Flag.Var(&driver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
||||||
cmdApiapp.Flag.Var(&tables, "tables", "specify tables to generate model")
|
cmdApiapp.Flag.Var(&conn, "conn", "Connection string used by the driver to connect to a database instance.")
|
||||||
cmdApiapp.Flag.Var(&driver, "driver", "database driver: mysql, postgresql, etc.")
|
|
||||||
cmdApiapp.Flag.Var(&conn, "conn", "connection string used by the driver to connect to a database instance")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createapi(cmd *Command, args []string) int {
|
func createapi(cmd *Command, args []string) int {
|
||||||
w := NewColorWriter(os.Stdout)
|
output := cmd.Out()
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
logger.Fatal("Argument [appname] is missing")
|
logger.Fatal("Argument [appname] is missing")
|
||||||
@ -568,19 +561,19 @@ func createapi(cmd *Command, args []string) int {
|
|||||||
logger.Info("Creating API...")
|
logger.Info("Creating API...")
|
||||||
|
|
||||||
os.MkdirAll(apppath, 0755)
|
os.MkdirAll(apppath, 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers"), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests"), "\x1b[0m")
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "conf", "app.conf"),
|
WriteToFile(path.Join(apppath, "conf", "app.conf"),
|
||||||
strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1))
|
strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1))
|
||||||
|
|
||||||
if conn != "" {
|
if conn != "" {
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
||||||
maingoContent := strings.Replace(apiMainconngo, "{{.Appname}}", packpath, -1)
|
maingoContent := strings.Replace(apiMainconngo, "{{.Appname}}", packpath, -1)
|
||||||
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
|
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
|
||||||
if driver == "mysql" {
|
if driver == "mysql" {
|
||||||
@ -602,33 +595,33 @@ func createapi(cmd *Command, args []string) int {
|
|||||||
generateAppcode(string(driver), string(conn), "3", string(tables), apppath)
|
generateAppcode(string(driver), string(conn), "3", string(tables), apppath)
|
||||||
} else {
|
} else {
|
||||||
os.Mkdir(path.Join(apppath, "models"), 0755)
|
os.Mkdir(path.Join(apppath, "models"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers")+string(path.Separator), "\x1b[0m")
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "object.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "object.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "controllers", "object.go"),
|
WriteToFile(path.Join(apppath, "controllers", "object.go"),
|
||||||
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "user.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "user.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "controllers", "user.go"),
|
WriteToFile(path.Join(apppath, "controllers", "user.go"),
|
||||||
strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1))
|
strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests", "default_test.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests", "default_test.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "tests", "default_test.go"),
|
WriteToFile(path.Join(apppath, "tests", "default_test.go"),
|
||||||
strings.Replace(apiTests, "{{.Appname}}", packpath, -1))
|
strings.Replace(apiTests, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers", "router.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers", "router.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "routers", "router.go"),
|
WriteToFile(path.Join(apppath, "routers", "router.go"),
|
||||||
strings.Replace(apirouter, "{{.Appname}}", packpath, -1))
|
strings.Replace(apirouter, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "models", "object.go"), apiModels)
|
WriteToFile(path.Join(apppath, "models", "object.go"), apiModels)
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2)
|
WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2)
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "main.go"),
|
WriteToFile(path.Join(apppath, "main.go"),
|
||||||
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
|
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
|
||||||
}
|
}
|
||||||
|
21
bale.go
21
bale.go
@ -28,22 +28,17 @@ import (
|
|||||||
|
|
||||||
var cmdBale = &Command{
|
var cmdBale = &Command{
|
||||||
UsageLine: "bale",
|
UsageLine: "bale",
|
||||||
Short: "packs non-Go files to Go source files",
|
Short: "Transforms non-Go files to Go source files",
|
||||||
Long: `
|
Long: `Bale command compress all the static files in to a single binary file.
|
||||||
Bale command compress all the static files in to a single binary file.
|
|
||||||
|
|
||||||
This is useful to not have to carry static files including js, css, images
|
This is useful to not have to carry static files including js, css, images and
|
||||||
and views when publishing a project.
|
views when deploying a Web application.
|
||||||
|
|
||||||
auto-generate unpack function to main package then run it during the runtime.
|
|
||||||
This is mainly used for zealots who are requiring 100% Go code.
|
|
||||||
|
|
||||||
|
It will auto-generate an unpack function to the main package then run it during the runtime.
|
||||||
|
This is mainly used for zealots who are requiring 100% Go code.
|
||||||
`,
|
`,
|
||||||
}
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: runBale,
|
||||||
func init() {
|
|
||||||
cmdBale.Run = runBale
|
|
||||||
cmdBale.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runBale(cmd *Command, args []string) int {
|
func runBale(cmd *Command, args []string) int {
|
||||||
|
14
fix.go
14
fix.go
@ -13,11 +13,11 @@ import (
|
|||||||
|
|
||||||
var cmdFix = &Command{
|
var cmdFix = &Command{
|
||||||
UsageLine: "fix",
|
UsageLine: "fix",
|
||||||
Short: "fix the beego application to make it compatible with beego 1.6",
|
Short: "Fixes your application by making it compatible with newer versions of Beego",
|
||||||
Long: `
|
Long: `As of {{"Beego 1.6"|bold}}, there are some backward compatibility issues.
|
||||||
As from beego1.6, there's some incompatible code with the old version.
|
|
||||||
|
|
||||||
bee fix help to upgrade the application to beego 1.6
|
The command 'fix' will try to solve those issues by upgrading your code base
|
||||||
|
to be compatible with Beego version 1.6+.
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +27,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runFix(cmd *Command, args []string) int {
|
func runFix(cmd *Command, args []string) int {
|
||||||
|
output := cmd.Out()
|
||||||
|
|
||||||
logger.Info("Upgrading the application...")
|
logger.Info("Upgrading the application...")
|
||||||
|
|
||||||
dir, err := os.Getwd()
|
dir, err := os.Getwd()
|
||||||
@ -48,13 +50,13 @@ func runFix(cmd *Command, args []string) int {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = fixFile(path)
|
err = fixFile(path)
|
||||||
fmt.Println("\tfix\t", path)
|
fmt.Fprintf(output, GreenBold("\tfix\t")+"%s\n", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Could not fix file: %s", err)
|
logger.Errorf("Could not fix file: %s", err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
logger.Success("Upgrade done!")
|
logger.Success("Upgrade Done!")
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
71
g.go
71
g.go
@ -20,45 +20,42 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var cmdGenerate = &Command{
|
var cmdGenerate = &Command{
|
||||||
UsageLine: "generate [Command]",
|
UsageLine: "generate [command]",
|
||||||
Short: "source code generator",
|
Short: "Source code generator",
|
||||||
Long: `
|
Long: `▶ {{"To scaffold out your entire application:"|bold}}
|
||||||
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 table fields. Format: field:type, ...
|
|
||||||
-driver: [mysql | postgres | 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=""]
|
$ bee generate scaffold [scaffoldname] [-fields="title:string,body:text"] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
||||||
generate RESTFul model based on fields
|
|
||||||
-fields: a list of table fields. Format: field:type, ...
|
|
||||||
|
|
||||||
bee generate controller [controllerfile]
|
▶ {{"To generate a Model based on fields:"|bold}}
|
||||||
generate RESTful controllers
|
|
||||||
|
|
||||||
bee generate view [viewpath]
|
$ bee generate model [modelname] [-fields="name:type"]
|
||||||
generate CRUD view in viewpath
|
|
||||||
|
|
||||||
bee generate migration [migrationfile] [-fields=""]
|
▶ {{"To generate a controller:"|bold}}
|
||||||
generate migration file for making database schema update
|
|
||||||
-fields: a list of table fields. Format: field:type, ...
|
|
||||||
|
|
||||||
bee generate docs
|
$ bee generate controller [controllerfile]
|
||||||
generate swagger doc file
|
|
||||||
|
|
||||||
bee generate test [routerfile]
|
▶ {{"To generate a CRUD view:"|bold}}
|
||||||
generate testcase
|
|
||||||
|
|
||||||
bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3]
|
$ bee generate view [viewpath]
|
||||||
generate appcode based on an existing database
|
|
||||||
-tables: a list of table names separated by ',', default is empty, indicating all tables
|
▶ {{"To generate a migration file for making database schema updates:"|bold}}
|
||||||
-driver: [mysql | postgres | sqlite], the default is mysql
|
|
||||||
-conn: the connection string used by the driver.
|
$ bee generate migration [migrationfile] [-fields="name:type"]
|
||||||
default for mysql: root:@tcp(127.0.0.1:3306)/test
|
|
||||||
default for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
|
▶ {{"To generate swagger doc file:"|bold}}
|
||||||
-level: [1 | 2 | 3], 1 = models; 2 = models,controllers; 3 = models,controllers,router
|
|
||||||
|
$ 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]
|
||||||
`,
|
`,
|
||||||
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: generateCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
var driver docValue
|
var driver docValue
|
||||||
@ -68,13 +65,11 @@ var tables docValue
|
|||||||
var fields docValue
|
var fields docValue
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdGenerate.Run = generateCode
|
cmdGenerate.Flag.Var(&tables, "tables", "List of table names separated by a comma.")
|
||||||
cmdGenerate.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
cmdGenerate.Flag.Var(&driver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
||||||
cmdGenerate.Flag.Var(&tables, "tables", "specify tables to generate model")
|
cmdGenerate.Flag.Var(&conn, "conn", "Connection string used by the driver to connect to a database instance.")
|
||||||
cmdGenerate.Flag.Var(&driver, "driver", "database driver: mysql, postgresql, etc.")
|
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(&conn, "conn", "connection string used by the driver to connect to a database instance")
|
cmdGenerate.Flag.Var(&fields, "fields", "List of table fields.")
|
||||||
cmdGenerate.Flag.Var(&level, "level", "1 = models only; 2 = models and controllers; 3 = models, controllers and routers")
|
|
||||||
cmdGenerate.Flag.Var(&fields, "fields", "specify the fields want to generate.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateCode(cmd *Command, args []string) int {
|
func generateCode(cmd *Command, args []string) int {
|
||||||
|
87
hproseapp.go
87
hproseapp.go
@ -1,19 +1,16 @@
|
|||||||
/**********************************************************\
|
// Copyright 2013 bee authors
|
||||||
| |
|
//
|
||||||
| hprose |
|
// 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
|
||||||
| Official WebSite: http://www.hprose.com/ |
|
// a copy of the License at
|
||||||
| http://www.hprose.org/ |
|
//
|
||||||
| |
|
// 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
|
||||||
* Build rpc application use Hprose base on beego *
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
* *
|
// License for the specific language governing permissions and limitations
|
||||||
* LastModified: Oct 31, 2016 *
|
// under the License.
|
||||||
* Author: Liu jian <laoliu@lanmv.com> *
|
|
||||||
* *
|
|
||||||
\**********************************************************/
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@ -27,30 +24,28 @@ import (
|
|||||||
var cmdHproseapp = &Command{
|
var cmdHproseapp = &Command{
|
||||||
// CustomFlags: true,
|
// CustomFlags: true,
|
||||||
UsageLine: "hprose [appname]",
|
UsageLine: "hprose [appname]",
|
||||||
Short: "create an rpc application use hprose base on beego framework",
|
Short: "Creates an RPC application based on Hprose and Beego frameworks",
|
||||||
Long: `
|
Long: `
|
||||||
create an rpc application use hprose base on beego framework
|
The command 'hprose' creates an RPC application based on both Beego and Hprose (http://hprose.com/).
|
||||||
|
|
||||||
bee hprose [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
|
{{"To scaffold out your application, use:"|bold}}
|
||||||
-tables: a list of table names separated by ',', default is empty, indicating all tables
|
|
||||||
-driver: [mysql | postgres | sqlite], the default is mysql
|
|
||||||
-conn: the connection string used by the driver, the default is ''
|
|
||||||
e.g. for mysql: root:@tcp(127.0.0.1:3306)/test
|
|
||||||
e.g. for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
|
|
||||||
|
|
||||||
if conn is empty will create a example rpc application. otherwise generate rpc application use hprose based on an existing database.
|
$ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
|
||||||
|
|
||||||
In the current path, will create a folder named [appname]
|
If 'conn' is empty, the command will generate a sample application. Otherwise the command
|
||||||
|
will connect to your database and generate models based on the existing tables.
|
||||||
|
|
||||||
In the appname folder has the follow struct:
|
The command 'hprose' creates a folder named [appname] with the following structure:
|
||||||
|
|
||||||
├── conf
|
├── conf
|
||||||
│ └── app.conf
|
│ └── app.conf
|
||||||
├── main.go
|
├── main.go
|
||||||
└── models
|
└── models
|
||||||
└── object.go
|
└── object.go
|
||||||
└── user.go
|
└── user.go
|
||||||
`,
|
`,
|
||||||
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: createhprose,
|
||||||
}
|
}
|
||||||
|
|
||||||
var hproseconf = `appname = {{.Appname}}
|
var hproseconf = `appname = {{.Appname}}
|
||||||
@ -297,15 +292,13 @@ func DeleteUser(uid string) {
|
|||||||
var hproseAddFunctions = []string{}
|
var hproseAddFunctions = []string{}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdHproseapp.Run = createhprose
|
cmdHproseapp.Flag.Var(&tables, "tables", "List of table names separated by a comma.")
|
||||||
cmdHproseapp.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
cmdHproseapp.Flag.Var(&driver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
||||||
cmdHproseapp.Flag.Var(&tables, "tables", "specify tables to generate model")
|
cmdHproseapp.Flag.Var(&conn, "conn", "Connection string used by the driver to connect to a database instance.")
|
||||||
cmdHproseapp.Flag.Var(&driver, "driver", "database driver: mysql, postgresql, etc.")
|
|
||||||
cmdHproseapp.Flag.Var(&conn, "conn", "connection string used by the driver to connect to a database instance")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createhprose(cmd *Command, args []string) int {
|
func createhprose(cmd *Command, args []string) int {
|
||||||
w := NewColorWriter(os.Stdout)
|
output := cmd.Out()
|
||||||
|
|
||||||
curpath, _ := os.Getwd()
|
curpath, _ := os.Getwd()
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
@ -324,10 +317,10 @@ func createhprose(cmd *Command, args []string) int {
|
|||||||
logger.Info("Creating Hprose application...")
|
logger.Info("Creating Hprose application...")
|
||||||
|
|
||||||
os.MkdirAll(apppath, 0755)
|
os.MkdirAll(apppath, 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "conf", "app.conf"),
|
WriteToFile(path.Join(apppath, "conf", "app.conf"),
|
||||||
strings.Replace(hproseconf, "{{.Appname}}", args[0], -1))
|
strings.Replace(hproseconf, "{{.Appname}}", args[0], -1))
|
||||||
|
|
||||||
@ -336,7 +329,7 @@ func createhprose(cmd *Command, args []string) int {
|
|||||||
logger.Infof("Using '%s' as 'conn'", conn)
|
logger.Infof("Using '%s' as 'conn'", conn)
|
||||||
logger.Infof("Using '%s' as 'tables'", tables)
|
logger.Infof("Using '%s' as 'tables'", tables)
|
||||||
generateHproseAppcode(string(driver), string(conn), "1", string(tables), path.Join(curpath, args[0]))
|
generateHproseAppcode(string(driver), string(conn), "1", string(tables), path.Join(curpath, args[0]))
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
||||||
maingoContent := strings.Replace(hproseMainconngo, "{{.Appname}}", packpath, -1)
|
maingoContent := strings.Replace(hproseMainconngo, "{{.Appname}}", packpath, -1)
|
||||||
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
|
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
|
||||||
maingoContent = strings.Replace(maingoContent, "{{HproseFunctionList}}", strings.Join(hproseAddFunctions, ""), -1)
|
maingoContent = strings.Replace(maingoContent, "{{HproseFunctionList}}", strings.Join(hproseAddFunctions, ""), -1)
|
||||||
@ -355,15 +348,15 @@ func createhprose(cmd *Command, args []string) int {
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
os.Mkdir(path.Join(apppath, "models"), 0755)
|
os.Mkdir(path.Join(apppath, "models"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m")
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "models", "object.go"), apiModels)
|
WriteToFile(path.Join(apppath, "models", "object.go"), apiModels)
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2)
|
WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2)
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "main.go"),
|
WriteToFile(path.Join(apppath, "main.go"),
|
||||||
strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1))
|
strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1))
|
||||||
}
|
}
|
||||||
|
41
migrate.go
41
migrate.go
@ -27,38 +27,35 @@ import (
|
|||||||
|
|
||||||
var cmdMigrate = &Command{
|
var cmdMigrate = &Command{
|
||||||
UsageLine: "migrate [Command]",
|
UsageLine: "migrate [Command]",
|
||||||
Short: "run database migrations",
|
Short: "Runs database migrations",
|
||||||
Long: `
|
Long: `The command 'migrate' allows you to run database migrations to keep it up-to-date.
|
||||||
bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
|
||||||
run all outstanding migrations
|
|
||||||
-driver: [mysql | postgres | sqlite] (default: mysql)
|
|
||||||
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
|
|
||||||
|
|
||||||
bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
▶ {{"To run all the migrations:"|bold}}
|
||||||
rollback the last migration operation
|
|
||||||
-driver: [mysql | postgres | sqlite] (default: mysql)
|
|
||||||
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
|
|
||||||
|
|
||||||
bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
$ bee migrate [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
||||||
rollback all migrations
|
|
||||||
-driver: [mysql | postgres | sqlite] (default: mysql)
|
|
||||||
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
|
|
||||||
|
|
||||||
bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
▶ {{"To rollback the last migration:"|bold}}
|
||||||
rollback all migrations and run them all again
|
|
||||||
-driver: [mysql | postgres | sqlite] (default: mysql)
|
$ bee migrate rollback [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
||||||
-conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test
|
|
||||||
|
▶ {{"To do a reset, which will rollback all the migrations:"|bold}}
|
||||||
|
|
||||||
|
$ bee migrate reset [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
||||||
|
|
||||||
|
▶ {{"To update your schema:"|bold}}
|
||||||
|
|
||||||
|
$ bee migrate refresh [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
||||||
`,
|
`,
|
||||||
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: runMigration,
|
||||||
}
|
}
|
||||||
|
|
||||||
var mDriver docValue
|
var mDriver docValue
|
||||||
var mConn docValue
|
var mConn docValue
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdMigrate.Run = runMigration
|
cmdMigrate.Flag.Var(&mDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
||||||
cmdMigrate.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
cmdMigrate.Flag.Var(&mConn, "conn", "Connection string used by the driver to connect to a database instance.")
|
||||||
cmdMigrate.Flag.Var(&mDriver, "driver", "database driver: mysql, postgres, sqlite, etc.")
|
|
||||||
cmdMigrate.Flag.Var(&mConn, "conn", "connection string used by the driver to connect to a database instance")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// runMigration is the entry point for starting a migration
|
// runMigration is the entry point for starting a migration
|
||||||
|
53
new.go
53
new.go
@ -23,16 +23,15 @@ import (
|
|||||||
|
|
||||||
var cmdNew = &Command{
|
var cmdNew = &Command{
|
||||||
UsageLine: "new [appname]",
|
UsageLine: "new [appname]",
|
||||||
Short: "Create a Beego application",
|
Short: "Creates a Beego application",
|
||||||
Long: `
|
Long: `
|
||||||
Creates a Beego application for the given app name in the current directory.
|
Creates a Beego application for the given app name in the current directory.
|
||||||
|
|
||||||
The command 'new' creates a folder named [appname] and inside the folder deploy
|
The command 'new' creates a folder named [appname] and generates the following structure:
|
||||||
the following files/directories structure:
|
|
||||||
|
|
||||||
|- main.go
|
|- main.go
|
||||||
|- conf
|
|- conf
|
||||||
|- app.conf
|
|- app.conf
|
||||||
|- controllers
|
|- controllers
|
||||||
|- default.go
|
|- default.go
|
||||||
|- models
|
|- models
|
||||||
@ -40,7 +39,7 @@ the following files/directories structure:
|
|||||||
|- router.go
|
|- router.go
|
||||||
|- tests
|
|- tests
|
||||||
|- default_test.go
|
|- default_test.go
|
||||||
|- static
|
|- static
|
||||||
|- js
|
|- js
|
||||||
|- css
|
|- css
|
||||||
|- img
|
|- img
|
||||||
@ -48,16 +47,12 @@ the following files/directories structure:
|
|||||||
index.tpl
|
index.tpl
|
||||||
|
|
||||||
`,
|
`,
|
||||||
}
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: createApp,
|
||||||
func init() {
|
|
||||||
cmdNew.Run = createApp
|
|
||||||
cmdNew.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createApp(cmd *Command, args []string) int {
|
func createApp(cmd *Command, args []string) int {
|
||||||
w := NewColorWriter(os.Stdout)
|
output := cmd.Out()
|
||||||
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
logger.Fatal("Argument [appname] is missing")
|
logger.Fatal("Argument [appname] is missing")
|
||||||
}
|
}
|
||||||
@ -78,43 +73,43 @@ func createApp(cmd *Command, args []string) int {
|
|||||||
logger.Info("Creating application...")
|
logger.Info("Creating application...")
|
||||||
|
|
||||||
os.MkdirAll(apppath, 0755)
|
os.MkdirAll(apppath, 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "models"), 0755)
|
os.Mkdir(path.Join(apppath, "models"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "static"), 0755)
|
os.Mkdir(path.Join(apppath, "static"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "static", "js"), 0755)
|
os.Mkdir(path.Join(apppath, "static", "js"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static", "js")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static", "js")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "static", "css"), 0755)
|
os.Mkdir(path.Join(apppath, "static", "css"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static", "css")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static", "css")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "static", "img"), 0755)
|
os.Mkdir(path.Join(apppath, "static", "img"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static", "img")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "static", "img")+string(path.Separator), "\x1b[0m")
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "views")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "views")+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "views"), 0755)
|
os.Mkdir(path.Join(apppath, "views"), 0755)
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", path.Base(args[0]), -1))
|
WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", path.Base(args[0]), -1))
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "default.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "controllers", "default.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "controllers", "default.go"), controllers)
|
WriteToFile(path.Join(apppath, "controllers", "default.go"), controllers)
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "views", "index.tpl"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "views", "index.tpl"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl)
|
WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl)
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers", "router.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers", "router.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", packpath, -1))
|
WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests", "default_test.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests", "default_test.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", packpath, -1))
|
WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
||||||
WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", packpath, -1))
|
WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
logger.Success("New application successfully created!")
|
logger.Success("New application successfully created!")
|
||||||
|
69
pack.go
69
pack.go
@ -36,26 +36,15 @@ import (
|
|||||||
var cmdPack = &Command{
|
var cmdPack = &Command{
|
||||||
CustomFlags: true,
|
CustomFlags: true,
|
||||||
UsageLine: "pack",
|
UsageLine: "pack",
|
||||||
Short: "Compress a beego project into a single file",
|
Short: "Compresses a Beego application into a single file",
|
||||||
Long: `
|
Long: `Pack is used to compress Beego applications into a tarball/zip file.
|
||||||
Pack is used to compress a beego project into a single file.
|
This eases the deployment by extracting directly the file to a server.
|
||||||
This eases the deployment by extracting the zip file to a server.
|
|
||||||
|
|
||||||
-p app path (default is the current path).
|
{{"Example:"|bold}}
|
||||||
-b build specify platform app (default: true).
|
$ bee pack -v -ba="-ldflags '-s -w'"
|
||||||
-ba additional args of go build
|
|
||||||
-be=[] additional ENV Variables of go build. eg: GOARCH=arm
|
|
||||||
-o compressed file output dir. default use current path
|
|
||||||
-f="" format: tar.gz, zip (default: tar.gz)
|
|
||||||
-exp="" relpath exclude prefix (default: .). use : as separator
|
|
||||||
-exs="" relpath exclude suffix (default: .go:.DS_Store:.tmp). use : as separator
|
|
||||||
all path use : as separator
|
|
||||||
-exr=[] file/directory name exclude by Regexp (default: ^).
|
|
||||||
-fs=false follow symlink (default: false).
|
|
||||||
-ss=false skip symlink (default: false)
|
|
||||||
default embed symlink into compressed file
|
|
||||||
-v=false verbose
|
|
||||||
`,
|
`,
|
||||||
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: packApp,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -71,7 +60,6 @@ var (
|
|||||||
buildEnvs ListOpts
|
buildEnvs ListOpts
|
||||||
verbose bool
|
verbose bool
|
||||||
format string
|
format string
|
||||||
w io.Writer
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListOpts []string
|
type ListOpts []string
|
||||||
@ -87,22 +75,19 @@ func (opts *ListOpts) Set(value string) error {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
fs := flag.NewFlagSet("pack", flag.ContinueOnError)
|
fs := flag.NewFlagSet("pack", flag.ContinueOnError)
|
||||||
fs.StringVar(&appPath, "p", "", "app path. default is current path")
|
fs.StringVar(&appPath, "p", "", "Set the application path. Defaults to the current path.")
|
||||||
fs.BoolVar(&build, "b", true, "build specify platform app")
|
fs.BoolVar(&build, "b", true, "Tell the command to do a build for the current platform. Defaults to true.")
|
||||||
fs.StringVar(&buildArgs, "ba", "", "additional args of go build")
|
fs.StringVar(&buildArgs, "ba", "", "Specify additional args for Go build.")
|
||||||
fs.Var(&buildEnvs, "be", "additional ENV Variables of go build. eg: GOARCH=arm")
|
fs.Var(&buildEnvs, "be", "Specify additional env variables for Go build. e.g. GOARCH=arm.")
|
||||||
fs.StringVar(&outputP, "o", "", "compressed file output dir. default use current path")
|
fs.StringVar(&outputP, "o", "", "Set the compressed file output path. Defaults to the current path.")
|
||||||
fs.StringVar(&format, "f", "tar.gz", "format. [ tar.gz / zip ]")
|
fs.StringVar(&format, "f", "tar.gz", "Set file format. Either tar.gz or zip. Defaults to tar.gz.")
|
||||||
fs.StringVar(&excludeP, "exp", ".", "path exclude prefix. use : as separator")
|
fs.StringVar(&excludeP, "exp", ".", "Set prefixes of paths to be excluded. Uses a column (:) as separator.")
|
||||||
fs.StringVar(&excludeS, "exs", ".go:.DS_Store:.tmp", "path exclude suffix. use : as separator")
|
fs.StringVar(&excludeS, "exs", ".go:.DS_Store:.tmp", "Set suffixes of paths to be excluded. Uses a column (:) as separator.")
|
||||||
fs.Var(&excludeR, "exr", "filename exclude by Regexp")
|
fs.Var(&excludeR, "exr", "Set a regular expression of files to be excluded.")
|
||||||
fs.BoolVar(&fsym, "fs", false, "follow symlink")
|
fs.BoolVar(&fsym, "fs", false, "Tell the command to follow symlinks. Defaults to false.")
|
||||||
fs.BoolVar(&ssym, "ss", false, "skip symlink")
|
fs.BoolVar(&ssym, "ss", false, "Tell the command to skip symlinks. Defaults to false.")
|
||||||
fs.BoolVar(&verbose, "v", false, "verbose")
|
fs.BoolVar(&verbose, "v", false, "Be more verbose during the operation. Defaults to false.")
|
||||||
cmdPack.Flag = *fs
|
cmdPack.Flag = *fs
|
||||||
cmdPack.Run = packApp
|
|
||||||
cmdPack.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
|
||||||
w = NewColorWriter(os.Stdout)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type walker interface {
|
type walker interface {
|
||||||
@ -127,6 +112,7 @@ type walkFileTree struct {
|
|||||||
excludeRegexp []*regexp.Regexp
|
excludeRegexp []*regexp.Regexp
|
||||||
excludeSuffix []string
|
excludeSuffix []string
|
||||||
allfiles map[string]bool
|
allfiles map[string]bool
|
||||||
|
output *io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wft *walkFileTree) setPrefix(prefix string) {
|
func (wft *walkFileTree) setPrefix(prefix string) {
|
||||||
@ -239,7 +225,7 @@ func (wft *walkFileTree) walkLeaf(fpath string, fi os.FileInfo, err error) error
|
|||||||
|
|
||||||
if added, err := wft.wak.compress(name, fpath, fi); added {
|
if added, err := wft.wak.compress(name, fpath, fi); added {
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Fprintf(w, "\t%s%scompressed%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", name, "\x1b[0m")
|
fmt.Fprintf(*wft.output, "\t%s%scompressed%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", name, "\x1b[0m")
|
||||||
}
|
}
|
||||||
wft.allfiles[name] = true
|
wft.allfiles[name] = true
|
||||||
return err
|
return err
|
||||||
@ -390,7 +376,7 @@ func (wft *zipWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) {
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func packDirectory(excludePrefix []string, excludeSuffix []string,
|
func packDirectory(output io.Writer, excludePrefix []string, excludeSuffix []string,
|
||||||
excludeRegexp []*regexp.Regexp, includePath ...string) (err error) {
|
excludeRegexp []*regexp.Regexp, includePath ...string) (err error) {
|
||||||
|
|
||||||
logger.Infof("Excluding relpath prefix: %s", strings.Join(excludePrefix, ":"))
|
logger.Infof("Excluding relpath prefix: %s", strings.Join(excludePrefix, ":"))
|
||||||
@ -408,6 +394,7 @@ func packDirectory(excludePrefix []string, excludeSuffix []string,
|
|||||||
|
|
||||||
if format == "zip" {
|
if format == "zip" {
|
||||||
walk := new(zipWalk)
|
walk := new(zipWalk)
|
||||||
|
walk.output = &output
|
||||||
zw := zip.NewWriter(w)
|
zw := zip.NewWriter(w)
|
||||||
defer func() {
|
defer func() {
|
||||||
zw.Close()
|
zw.Close()
|
||||||
@ -421,6 +408,7 @@ func packDirectory(excludePrefix []string, excludeSuffix []string,
|
|||||||
wft = walk
|
wft = walk
|
||||||
} else {
|
} else {
|
||||||
walk := new(tarWalk)
|
walk := new(tarWalk)
|
||||||
|
walk.output = &output
|
||||||
cw := gzip.NewWriter(w)
|
cw := gzip.NewWriter(w)
|
||||||
tw := tar.NewWriter(cw)
|
tw := tar.NewWriter(cw)
|
||||||
|
|
||||||
@ -450,6 +438,7 @@ func packDirectory(excludePrefix []string, excludeSuffix []string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func packApp(cmd *Command, args []string) int {
|
func packApp(cmd *Command, args []string) int {
|
||||||
|
output := cmd.Out()
|
||||||
curPath, _ := os.Getwd()
|
curPath, _ := os.Getwd()
|
||||||
thePath := ""
|
thePath := ""
|
||||||
|
|
||||||
@ -463,7 +452,7 @@ func packApp(cmd *Command, args []string) int {
|
|||||||
nArgs = append(nArgs, a)
|
nArgs = append(nArgs, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cmdPack.Flag.Parse(nArgs)
|
cmd.Flag.Parse(nArgs)
|
||||||
|
|
||||||
if path.IsAbs(appPath) == false {
|
if path.IsAbs(appPath) == false {
|
||||||
appPath = path.Join(curPath, appPath)
|
appPath = path.Join(curPath, appPath)
|
||||||
@ -532,7 +521,7 @@ func packApp(cmd *Command, args []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
fmt.Fprintf(w, "\t%s%s+ go %s%s%s\n", "\x1b[32m", "\x1b[1m", strings.Join(args, " "), "\x1b[21m", "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%s+ go %s%s%s\n", "\x1b[32m", "\x1b[1m", strings.Join(args, " "), "\x1b[21m", "\x1b[0m")
|
||||||
}
|
}
|
||||||
|
|
||||||
execmd := exec.Command("go", args...)
|
execmd := exec.Command("go", args...)
|
||||||
@ -545,7 +534,7 @@ func packApp(cmd *Command, args []string) int {
|
|||||||
logger.Fatal(err.Error())
|
logger.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Success("Build successful!")
|
logger.Success("Build Successful!")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch format {
|
switch format {
|
||||||
@ -594,7 +583,7 @@ func packApp(cmd *Command, args []string) int {
|
|||||||
|
|
||||||
logger.Infof("Writing to output: %s", outputP)
|
logger.Infof("Writing to output: %s", outputP)
|
||||||
|
|
||||||
err = packDirectory(exp, exs, exr, tmpdir, thePath)
|
err = packDirectory(output, exp, exs, exr, tmpdir, thePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err.Error())
|
logger.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
23
run.go
23
run.go
@ -24,12 +24,13 @@ import (
|
|||||||
|
|
||||||
var cmdRun = &Command{
|
var cmdRun = &Command{
|
||||||
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-vendor=true] [-e=folderToExclude] [-tags=goBuildTags] [-runmode=BEEGO_RUNMODE]",
|
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-vendor=true] [-e=folderToExclude] [-tags=goBuildTags] [-runmode=BEEGO_RUNMODE]",
|
||||||
Short: "run the app and start a Web server for development",
|
Short: "Run the application by starting a local development server",
|
||||||
Long: `
|
Long: `
|
||||||
Run command will supervise the file system of the beego project using inotify,
|
Run command will supervise the filesystem of the application for any changes, and recompile/restart it.
|
||||||
it will recompile and restart the app after any modifications.
|
|
||||||
|
|
||||||
`,
|
`,
|
||||||
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
||||||
|
Run: runApp,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -55,15 +56,13 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdRun.Run = runApp
|
cmdRun.Flag.Var(&mainFiles, "main", "Specify main go files.")
|
||||||
cmdRun.PreRun = func(cmd *Command, args []string) { ShowShortVersionBanner() }
|
cmdRun.Flag.Var(&gendoc, "gendoc", "Enable auto-generate the docs.")
|
||||||
cmdRun.Flag.Var(&mainFiles, "main", "specify main go files")
|
cmdRun.Flag.Var(&downdoc, "downdoc", "Enable auto-download of the swagger file if it does not exist.")
|
||||||
cmdRun.Flag.Var(&gendoc, "gendoc", "auto generate the docs")
|
cmdRun.Flag.Var(&excludedPaths, "e", "List of paths to exclude.")
|
||||||
cmdRun.Flag.Var(&downdoc, "downdoc", "auto download swagger file when not exist")
|
cmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Enable watch vendor folder")
|
||||||
cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].")
|
cmdRun.Flag.StringVar(&buildTags, "tags", "", "Set the build tags. See: https://golang.org/pkg/go/build/")
|
||||||
cmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Watch vendor folder")
|
cmdRun.Flag.StringVar(&runmode, "runmode", "", "Set the Beego run mode.")
|
||||||
cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)")
|
|
||||||
cmdRun.Flag.StringVar(&runmode, "runmode", "", "Set BEEGO_RUNMODE env variable.")
|
|
||||||
exit = make(chan bool)
|
exit = make(chan bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,9 +14,9 @@ import (
|
|||||||
|
|
||||||
var cmdVersion = &Command{
|
var cmdVersion = &Command{
|
||||||
UsageLine: "version",
|
UsageLine: "version",
|
||||||
Short: "prints the current Bee version",
|
Short: "Prints the current Bee version",
|
||||||
Long: `
|
Long: `
|
||||||
Prints the current Bee, Beego and Go version alongside the platform information
|
Prints the current Bee, Beego and Go version alongside the platform information.
|
||||||
|
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user