From d7b23aa54f26b6625989a60a2f16e72b02ba89a8 Mon Sep 17 00:00:00 2001 From: itcbx Date: Sat, 29 Aug 2015 15:14:21 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=97=B6=EF=BC=8C@Sucess=20=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E4=B8=AA=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原来的代码,当第三个参数有多个空格时,比如 // @Success 200 {string} delete success! 第三个参数会解析成success!,即会忽略第一个空格前的单词,修改后,可以正常解析。 --- g_docs.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/g_docs.go b/g_docs.go index ea2ef00..3db26a8 100644 --- a/g_docs.go +++ b/g_docs.go @@ -395,10 +395,13 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat tmp = make([]rune, 0) j += 1 start = false - continue - } else { - st[j] = strings.TrimSpace(ss[i+1:]) - break + if j == 1 { + continue + } else { + st[j] = strings.TrimSpace(ss[i+1:]) + break + + } } } else { start = true From 19b8add1c518b670e0a35f3130097433edd05039 Mon Sep 17 00:00:00 2001 From: Mark Mindenhall Date: Wed, 10 Feb 2016 12:39:02 -0700 Subject: [PATCH 2/8] Support go build tags (issue #149) --- run.go | 6 +++++- watch.go | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/run.go b/run.go index ec136b6..3d4da70 100644 --- a/run.go +++ b/run.go @@ -26,7 +26,7 @@ import ( ) var cmdRun = &Command{ - UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-e=Godeps -e=folderToExclude]", + UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true] [-e=Godeps -e=folderToExclude] [-tags=goBuildTags]", Short: "run the app and start a Web server for development", Long: ` Run command will supervise the file system of the beego project using inotify, @@ -43,12 +43,16 @@ var gendoc docValue // The flags list of the paths excluded from watching var excludedPaths strFlags +// Pass through to -tags arg of "go build" +var buildTags string + func init() { cmdRun.Run = runApp cmdRun.Flag.Var(&mainFiles, "main", "specify main go files") cmdRun.Flag.Var(&gendoc, "gendoc", "auto generate the docs") cmdRun.Flag.Var(&downdoc, "downdoc", "auto download swagger file when not exist") cmdRun.Flag.Var(&excludedPaths, "e", "Excluded paths[].") + cmdRun.Flag.StringVar(&buildTags, "tags", "", "Build tags (https://golang.org/pkg/go/build/)") } var appname string diff --git a/watch.go b/watch.go index f18dd3c..371598e 100644 --- a/watch.go +++ b/watch.go @@ -170,6 +170,9 @@ func Autobuild(files []string, isgenerate bool) { args := []string{"build"} args = append(args, "-o", appName) + if buildTags != "" { + args = append(args, "-tags", buildTags) + } args = append(args, files...) bcmd := exec.Command(cmdName, args...) From 6481a96ca4506c769f1c6b1794d32f6c91237aa0 Mon Sep 17 00:00:00 2001 From: Liut Date: Fri, 11 Mar 2016 23:39:17 +0800 Subject: [PATCH 3/8] =?UTF-8?q?allow=20any=20schema=20in=20PostgresDB;=20?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E9=80=89=E6=8B=A9=20PostgreSQL=EF=BC=8C?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E5=AE=9A=E5=88=B6=E7=9A=84=20schema;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g_appcode.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/g_appcode.go b/g_appcode.go index f5f6b0d..7860912 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -516,7 +516,9 @@ func (*MysqlDB) GetGoDataType(sqlType string) (goType string) { func (*PostgresDB) GetTableNames(db *sql.DB) (tables []string) { rows, err := db.Query(` SELECT table_name FROM information_schema.tables - WHERE table_catalog = current_database() and table_schema = 'public'`) + WHERE table_catalog = current_database() AND + table_type = 'BASE TABLE' AND + table_schema NOT IN ('pg_catalog', 'information_schema')`) if err != nil { ColorLog("[ERRO] Could not show tables: %s\n", err) ColorLog("[HINT] Check your connection string\n") @@ -551,8 +553,10 @@ func (*PostgresDB) GetConstraints(db *sql.DB, table *Table, blackList map[string INNER JOIN information_schema.constraint_column_usage cu ON cu.constraint_name = c.constraint_name WHERE - c.table_catalog = current_database() AND c.table_schema = 'public' AND c.table_name = $1 - AND u.table_catalog = current_database() AND u.table_schema = 'public' AND u.table_name = $2`, + c.table_catalog = current_database() AND c.table_schema NOT IN ('pg_catalog', 'information_schema') + AND c.table_name = $1 + AND u.table_catalog = current_database() AND u.table_schema NOT IN ('pg_catalog', 'information_schema') + AND u.table_name = $2`, table.Name, table.Name) // u.position_in_unique_constraint, if err != nil { ColorLog("[ERRO] Could not query INFORMATION_SCHEMA for PK/UK/FK information: %s\n", err) @@ -608,7 +612,8 @@ func (postgresDB *PostgresDB) GetColumns(db *sql.DB, table *Table, blackList map FROM information_schema.columns WHERE - table_catalog = current_database() AND table_schema = 'public' AND table_name = $1`, + table_catalog = current_database() AND table_schema NOT IN ('pg_catalog', 'information_schema') + AND table_name = $1`, table.Name) defer colDefRows.Close() for colDefRows.Next() { @@ -985,12 +990,12 @@ func getPackagePath(curpath string) (packpath string) { ColorLog("[ERRO] Can't generate application code outside of GOPATH '%s'\n", gopath) os.Exit(2) } - + if curpath == appsrcpath { ColorLog("[ERRO] Can't generate application code outside of application PATH \n") os.Exit(2) } - + packpath = strings.Join(strings.Split(curpath[len(appsrcpath)+1:], string(filepath.Separator)), "/") return } From 7210ac621d012af8282f6685cfb5baed48bd80da Mon Sep 17 00:00:00 2001 From: Sergey Lanzman Date: Sat, 12 Mar 2016 14:05:15 +0200 Subject: [PATCH 4/8] optimisation disable GC for go build , go list, go install +20% speed on large projects --- watch.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/watch.go b/watch.go index 371598e..7d966bd 100644 --- a/watch.go +++ b/watch.go @@ -136,6 +136,7 @@ func Autobuild(files []string, isgenerate bool) { icmd := exec.Command("go", "list", "./...") buf := bytes.NewBuffer([]byte("")) icmd.Stdout = buf + icmd.Env = append(os.Environ(), "GOGC=off") err = icmd.Run() if err == nil { list := strings.Split(buf.String(), "\n")[1:] @@ -146,6 +147,7 @@ func Autobuild(files []string, isgenerate bool) { icmd = exec.Command(cmdName, "install", pkg) icmd.Stdout = os.Stdout icmd.Stderr = os.Stderr + icmd.Env = append(os.Environ(), "GOGC=off") err = icmd.Run() if err != nil { break @@ -156,6 +158,7 @@ func Autobuild(files []string, isgenerate bool) { if isgenerate { icmd := exec.Command("bee", "generate", "docs") + icmd.Env = append(os.Environ(), "GOGC=off") icmd.Stdout = os.Stdout icmd.Stderr = os.Stderr icmd.Run() @@ -176,6 +179,7 @@ func Autobuild(files []string, isgenerate bool) { args = append(args, files...) bcmd := exec.Command(cmdName, args...) + bcmd.Env = append(os.Environ(), "GOGC=off") bcmd.Stdout = os.Stdout bcmd.Stderr = os.Stderr err = bcmd.Run() From e523bf5da4e717c6670522a86fcf1dbb7b821c4b Mon Sep 17 00:00:00 2001 From: Dmitri Logvinenko Date: Sun, 27 Mar 2016 16:42:26 +0300 Subject: [PATCH 5/8] Fix misspelling and improve command description --- fix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fix.go b/fix.go index 69d6584..3e1d3d6 100644 --- a/fix.go +++ b/fix.go @@ -12,7 +12,7 @@ import ( var cmdFix = &Command{ UsageLine: "fix", - Short: "fix the beego application to compatibel with beego 1.6", + Short: "fix the beego application to make it compatible with beego 1.6", Long: ` As from beego1.6, there's some incompatible code with the old version. From d312a60e4f4c8456c411d3cbb47914136dbd6158 Mon Sep 17 00:00:00 2001 From: Dmitry Burakov Date: Sat, 2 Apr 2016 21:37:58 +0300 Subject: [PATCH 6/8] changed required go version [issue 172] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b597b54..5bad9ee 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Bee is a command line tool facilitating development with beego framework. ## Requirements -- Go version >= 1.1. +- Go version >= 1.3. ## Installation From 01e7ff37749d3c060bb9bfcfcdbbc2294c6e86a6 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Thu, 26 May 2016 18:17:24 +0200 Subject: [PATCH 7/8] This fixes #201 --- apiapp.go | 64 +++++++++++++++------------------------------------- g_appcode.go | 10 ++++---- hproseapp.go | 10 ++++---- new.go | 51 +++++++++-------------------------------- 4 files changed, 39 insertions(+), 96 deletions(-) diff --git a/apiapp.go b/apiapp.go index 8ae376e..15ff035 100644 --- a/apiapp.go +++ b/apiapp.go @@ -546,14 +546,15 @@ func init() { } func createapi(cmd *Command, args []string) int { - curpath, _ := os.Getwd() if len(args) < 1 { ColorLog("[ERRO] Argument [appname] is missing\n") os.Exit(2) } + if len(args) > 1 { cmd.Flag.Parse(args[1:]) } + apppath, packpath, err := checkEnv(args[0]) if err != nil { fmt.Println(err) @@ -576,8 +577,8 @@ func createapi(cmd *Command, args []string) int { fmt.Println("create tests:", path.Join(apppath, "tests")) fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf")) - writetofile(path.Join(apppath, "conf", "app.conf"), - strings.Replace(apiconf, "{{.Appname}}", args[0], -1)) + WriteToFile(path.Join(apppath, "conf", "app.conf"), + strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1)) if conn != "" { fmt.Println("create main.go:", path.Join(apppath, "main.go")) @@ -588,7 +589,7 @@ func createapi(cmd *Command, args []string) int { } else if driver == "postgres" { maingoContent = strings.Replace(maingoContent, "{{.DriverPkg}}", `_ "github.com/lib/pq"`, -1) } - writetofile(path.Join(apppath, "main.go"), + WriteToFile(path.Join(apppath, "main.go"), strings.Replace( maingoContent, "{{.conn}}", @@ -599,7 +600,7 @@ func createapi(cmd *Command, args []string) int { ColorLog("[INFO] Using '%s' as 'driver'\n", driver) ColorLog("[INFO] Using '%s' as 'conn'\n", conn) ColorLog("[INFO] Using '%s' as 'tables'\n", tables) - generateAppcode(string(driver), string(conn), "3", string(tables), path.Join(curpath, args[0])) + generateAppcode(string(driver), string(conn), "3", string(tables), path.Join(apppath, args[0])) } else { os.Mkdir(path.Join(apppath, "models"), 0755) fmt.Println("create models:", path.Join(apppath, "models")) @@ -607,43 +608,38 @@ func createapi(cmd *Command, args []string) int { fmt.Println(path.Join(apppath, "routers") + string(path.Separator)) fmt.Println("create controllers object.go:", path.Join(apppath, "controllers", "object.go")) - writetofile(path.Join(apppath, "controllers", "object.go"), + WriteToFile(path.Join(apppath, "controllers", "object.go"), strings.Replace(apiControllers, "{{.Appname}}", packpath, -1)) fmt.Println("create controllers user.go:", path.Join(apppath, "controllers", "user.go")) - writetofile(path.Join(apppath, "controllers", "user.go"), + WriteToFile(path.Join(apppath, "controllers", "user.go"), strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1)) fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go")) - writetofile(path.Join(apppath, "tests", "default_test.go"), + WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(apiTests, "{{.Appname}}", packpath, -1)) fmt.Println("create routers router.go:", path.Join(apppath, "routers", "router.go")) - writetofile(path.Join(apppath, "routers", "router.go"), + WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(apirouter, "{{.Appname}}", packpath, -1)) fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go")) - writetofile(path.Join(apppath, "models", "object.go"), apiModels) + WriteToFile(path.Join(apppath, "models", "object.go"), apiModels) fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go")) - writetofile(path.Join(apppath, "models", "user.go"), apiModels2) + WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2) fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go")) - writetofile(path.Join(apppath, "docs", "doc.go"), "package docs") + WriteToFile(path.Join(apppath, "docs", "doc.go"), "package docs") fmt.Println("create main.go:", path.Join(apppath, "main.go")) - writetofile(path.Join(apppath, "main.go"), + WriteToFile(path.Join(apppath, "main.go"), strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1)) } return 0 } func checkEnv(appname string) (apppath, packpath string, err error) { - curpath, err := os.Getwd() - if err != nil { - return - } - gopath := os.Getenv("GOPATH") Debugf("gopath:%s", gopath) if gopath == "" { @@ -651,38 +647,14 @@ func checkEnv(appname string) (apppath, packpath string, err error) { return } - appsrcpath := "" - haspath := false - wgopath := path.SplitList(gopath) - for _, wg := range wgopath { - wg = path.Join(wg, "src") - - if strings.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) { - haspath = true - appsrcpath = wg - break - } - - wg, _ = path.EvalSymlinks(wg) - - if strings.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) { - haspath = true - appsrcpath = wg - break - } - } - - if !haspath { - err = fmt.Errorf("can't create application outside of GOPATH `%s`\n"+ - "you first should `cd $GOPATH%ssrc` then use create\n", gopath, string(path.Separator)) - return - } - apppath = path.Join(curpath, appname) + gosrcpath := path.Join(gopath, "src") + apppath = path.Join(gosrcpath, appname) if _, e := os.Stat(apppath); os.IsNotExist(e) == false { err = fmt.Errorf("path `%s` exists, can not create app without remove it\n", apppath) return } - packpath = strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/") + packpath = strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/") + return } diff --git a/g_appcode.go b/g_appcode.go index 7860912..fabec16 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -292,7 +292,7 @@ func generateAppcode(driver, connStr, level, tables, currpath string) { // Generate takes table, column and foreign key information from database connection // and generate corresponding golang source files -func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, currpath string) { +func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, apppath string) { db, err := sql.Open(dbms, connStr) if err != nil { ColorLog("[ERRO] Could not connect to %s database: %s, %s\n", dbms, connStr, err) @@ -304,11 +304,11 @@ func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, cu tableNames := trans.GetTableNames(db) tables := getTableObjects(tableNames, db, trans) mvcPath := new(MvcPath) - mvcPath.ModelPath = path.Join(currpath, "models") - mvcPath.ControllerPath = path.Join(currpath, "controllers") - mvcPath.RouterPath = path.Join(currpath, "routers") + mvcPath.ModelPath = path.Join(apppath, "models") + mvcPath.ControllerPath = path.Join(apppath, "controllers") + mvcPath.RouterPath = path.Join(apppath, "routers") createPaths(mode, mvcPath) - pkgPath := getPackagePath(currpath) + pkgPath := getPackagePath(apppath) writeSourceFiles(pkgPath, tables, mode, mvcPath, selectedTableNames) } else { ColorLog("[ERRO] Generating app code from %s database is not supported yet.\n", dbms) diff --git a/hproseapp.go b/hproseapp.go index 5167c31..40abc47 100644 --- a/hproseapp.go +++ b/hproseapp.go @@ -274,7 +274,7 @@ func createhprose(cmd *Command, args []string) int { os.Mkdir(path.Join(apppath, "conf"), 0755) fmt.Println("create conf:", path.Join(apppath, "conf")) fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf")) - writetofile(path.Join(apppath, "conf", "app.conf"), + WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(hproseconf, "{{.Appname}}", args[0], -1)) if conn != "" { @@ -291,7 +291,7 @@ func createhprose(cmd *Command, args []string) int { } else if driver == "postgres" { maingoContent = strings.Replace(maingoContent, "{{.DriverPkg}}", `_ "github.com/lib/pq"`, -1) } - writetofile(path.Join(apppath, "main.go"), + WriteToFile(path.Join(apppath, "main.go"), strings.Replace( maingoContent, "{{.conn}}", @@ -304,13 +304,13 @@ func createhprose(cmd *Command, args []string) int { fmt.Println("create models:", path.Join(apppath, "models")) fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go")) - writetofile(path.Join(apppath, "models", "object.go"), apiModels) + WriteToFile(path.Join(apppath, "models", "object.go"), apiModels) fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go")) - writetofile(path.Join(apppath, "models", "user.go"), apiModels2) + WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2) fmt.Println("create main.go:", path.Join(apppath, "main.go")) - writetofile(path.Join(apppath, "main.go"), + WriteToFile(path.Join(apppath, "main.go"), strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1)) } return 0 diff --git a/new.go b/new.go index bba644d..3c6fff9 100644 --- a/new.go +++ b/new.go @@ -55,7 +55,6 @@ func init() { } func createApp(cmd *Command, args []string) int { - curpath, _ := os.Getwd() if len(args) != 1 { ColorLog("[ERRO] Argument [appname] is missing\n") os.Exit(2) @@ -65,40 +64,12 @@ func createApp(cmd *Command, args []string) int { Debugf("gopath:%s", gopath) if gopath == "" { ColorLog("[ERRO] $GOPATH not found\n") - ColorLog("[HINT] Set $GOPATH in your environment vairables\n") - os.Exit(2) - } - haspath := false - appsrcpath := "" - - wgopath := path.SplitList(gopath) - for _, wg := range wgopath { - - wg = path.Join(wg, "src") - - if strings.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) { - haspath = true - appsrcpath = wg - break - } - - wg, _ = path.EvalSymlinks(wg) - - if strings.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) { - haspath = true - appsrcpath = wg - break - } - - } - - if !haspath { - ColorLog("[ERRO] Unable to create an application outside of $GOPATH%ssrc(%s%ssrc)\n", string(path.Separator), gopath, string(path.Separator)) - ColorLog("[HINT] Change your work directory by `cd ($GOPATH%ssrc)`\n", string(path.Separator)) + ColorLog("[HINT] Set $GOPATH in your environment variables\n") os.Exit(2) } - apppath := path.Join(curpath, args[0]) + gosrcpath := path.Join(gopath, "src") // User's workspace + apppath := path.Join(gosrcpath, args[0]) if isExist(apppath) { ColorLog("[ERRO] Path (%s) already exists\n", apppath) @@ -133,22 +104,22 @@ func createApp(cmd *Command, args []string) int { fmt.Println(path.Join(apppath, "views") + string(path.Separator)) os.Mkdir(path.Join(apppath, "views"), 0755) fmt.Println(path.Join(apppath, "conf", "app.conf")) - writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1)) + WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", path.Base(args[0]), -1)) fmt.Println(path.Join(apppath, "controllers", "default.go")) - writetofile(path.Join(apppath, "controllers", "default.go"), controllers) + WriteToFile(path.Join(apppath, "controllers", "default.go"), controllers) fmt.Println(path.Join(apppath, "views", "index.tpl")) - writetofile(path.Join(apppath, "views", "index.tpl"), indextpl) + WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl) fmt.Println(path.Join(apppath, "routers", "router.go")) - writetofile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1)) + WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) fmt.Println(path.Join(apppath, "tests", "default_test.go")) - writetofile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1)) + WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) fmt.Println(path.Join(apppath, "main.go")) - writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1)) + WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) ColorLog("[SUCC] New application successfully created!\n") return 0 @@ -336,11 +307,11 @@ var indextpl = ` ` -func writetofile(filename, content string) { +func WriteToFile(filename, content string) { f, err := os.Create(filename) + defer f.Close() if err != nil { panic(err) } - defer f.Close() f.WriteString(content) } From b8250ebb726fc7ff797476a1b8d56dfc09f0f0ad Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Wed, 1 Jun 2016 14:30:29 +0200 Subject: [PATCH 8/8] New Bee banner Added a new Bee banner using ASCII art. When bee is invoked, the banner is displayed with the version number (short banner). A More verbose Banner is added to 'bee version' which shows more information about the host and Go runtime. --- apiapp.go | 53 ++++++++++++++++++++----------------- bale.go | 4 ++- banner.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ fix.go | 11 +++++--- g.go | 11 +++++--- g_controllers.go | 3 ++- g_migration.go | 2 +- g_model.go | 3 ++- g_scaffold.go | 14 +++++----- g_views.go | 10 ++++--- hproseapp.go | 22 +++++++++------ migrate.go | 2 ++ new.go | 38 +++++++++++++------------- pack.go | 29 ++++++++++---------- run.go | 13 ++------- version.go | 58 +++++++++++++++++++++++++++++----------- 16 files changed, 230 insertions(+), 112 deletions(-) create mode 100644 banner.go diff --git a/apiapp.go b/apiapp.go index 15ff035..ee1f1d0 100644 --- a/apiapp.go +++ b/apiapp.go @@ -546,6 +546,8 @@ func init() { } func createapi(cmd *Command, args []string) int { + ShowShortVersionBanner() + if len(args) < 1 { ColorLog("[ERRO] Argument [appname] is missing\n") os.Exit(2) @@ -565,23 +567,25 @@ func createapi(cmd *Command, args []string) int { } if conn == "" { } - os.MkdirAll(apppath, 0755) - fmt.Println("create app folder:", apppath) - os.Mkdir(path.Join(apppath, "conf"), 0755) - fmt.Println("create conf:", path.Join(apppath, "conf")) - os.Mkdir(path.Join(apppath, "controllers"), 0755) - fmt.Println("create controllers:", path.Join(apppath, "controllers")) - os.Mkdir(path.Join(apppath, "docs"), 0755) - fmt.Println("create docs:", path.Join(apppath, "docs")) - os.Mkdir(path.Join(apppath, "tests"), 0755) - fmt.Println("create tests:", path.Join(apppath, "tests")) - fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf")) + ColorLog("[INFO] Creating API...\n") + + os.MkdirAll(apppath, 0755) + fmt.Println("\tcreate\t", apppath) + os.Mkdir(path.Join(apppath, "conf"), 0755) + fmt.Println("\tcreate\t", path.Join(apppath, "conf")) + os.Mkdir(path.Join(apppath, "controllers"), 0755) + fmt.Println("\tcreate\t", path.Join(apppath, "controllers")) + os.Mkdir(path.Join(apppath, "docs"), 0755) + fmt.Println("\tcreate\t", path.Join(apppath, "docs")) + os.Mkdir(path.Join(apppath, "tests"), 0755) + fmt.Println("\tcreate\t", path.Join(apppath, "tests")) + fmt.Println("\tcreate\t", path.Join(apppath, "conf", "app.conf")) WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1)) if conn != "" { - fmt.Println("create main.go:", path.Join(apppath, "main.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "main.go")) maingoContent := strings.Replace(apiMainconngo, "{{.Appname}}", packpath, -1) maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1) if driver == "mysql" { @@ -603,39 +607,40 @@ func createapi(cmd *Command, args []string) int { generateAppcode(string(driver), string(conn), "3", string(tables), path.Join(apppath, args[0])) } else { os.Mkdir(path.Join(apppath, "models"), 0755) - fmt.Println("create models:", path.Join(apppath, "models")) + fmt.Println("\tcreate\t", path.Join(apppath, "models")) os.Mkdir(path.Join(apppath, "routers"), 0755) - fmt.Println(path.Join(apppath, "routers") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "routers") + string(path.Separator)) - fmt.Println("create controllers object.go:", path.Join(apppath, "controllers", "object.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "controllers", "object.go")) WriteToFile(path.Join(apppath, "controllers", "object.go"), strings.Replace(apiControllers, "{{.Appname}}", packpath, -1)) - fmt.Println("create controllers user.go:", path.Join(apppath, "controllers", "user.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "controllers", "user.go")) WriteToFile(path.Join(apppath, "controllers", "user.go"), strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1)) - fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "tests", "default_test.go")) WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(apiTests, "{{.Appname}}", packpath, -1)) - fmt.Println("create routers router.go:", path.Join(apppath, "routers", "router.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "routers", "router.go")) WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(apirouter, "{{.Appname}}", packpath, -1)) - fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "models", "object.go")) WriteToFile(path.Join(apppath, "models", "object.go"), apiModels) - fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "models", "user.go")) WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2) - fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "docs", "doc.go")) WriteToFile(path.Join(apppath, "docs", "doc.go"), "package docs") - fmt.Println("create main.go:", path.Join(apppath, "main.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "main.go")) WriteToFile(path.Join(apppath, "main.go"), strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1)) } + ColorLog("[SUCC] New API successfully created!\n") return 0 } @@ -651,10 +656,10 @@ func checkEnv(appname string) (apppath, packpath string, err error) { apppath = path.Join(gosrcpath, appname) if _, e := os.Stat(apppath); os.IsNotExist(e) == false { - err = fmt.Errorf("path `%s` exists, can not create app without remove it\n", apppath) + err = fmt.Errorf("Cannot create application without removing `%s` first.", apppath) + ColorLog("[ERRO] Path `%s` already exists\n", apppath) return } packpath = strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/") - return } diff --git a/bale.go b/bale.go index 336d813..5f1f803 100644 --- a/bale.go +++ b/bale.go @@ -46,6 +46,8 @@ func init() { } func runBale(cmd *Command, args []string) int { + ShowShortVersionBanner() + err := loadConfig() if err != nil { ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err) @@ -60,7 +62,7 @@ func runBale(cmd *Command, args []string) int { ColorLog("[WARN] Skipped directory( %s )\n", p) continue } - ColorLog("[INFO] Packing directory( %s )\n", p) + ColorLog("[INFO] Packaging directory( %s )\n", p) filepath.Walk(p, walkFn) } diff --git a/banner.go b/banner.go new file mode 100644 index 0000000..d04293b --- /dev/null +++ b/banner.go @@ -0,0 +1,69 @@ +package main + +import ( + "io" + "io/ioutil" + "os" + "runtime" + "text/template" + "time" +) + +type vars struct { + GoVersion string + GOOS string + GOARCH string + NumCPU int + GOPATH string + GOROOT string + Compiler string + BeeVersion string + BeegoVersion string +} + +func Now(layout string) string { + return time.Now().Format(layout) +} + +// Init load the banner and prints it to output +// All errors are ignored, the application will not +// print the banner in case of error. +func InitBanner(out io.Writer, in io.Reader) { + if in == nil { + ColorLog("[ERRO] The input is nil\n") + os.Exit(2) + } + + banner, err := ioutil.ReadAll(in) + if err != nil { + ColorLog("[ERRO] Error trying to read the banner\n") + ColorLog("[HINT] %v\n", err) + os.Exit(2) + } + + show(out, string(banner)) +} + +func show(out io.Writer, content string) { + t, err := template.New("banner"). + Funcs(template.FuncMap{"Now": Now}). + Parse(content) + + if err != nil { + ColorLog("[ERRO] Cannot parse the banner template\n") + ColorLog("[HINT] %v\n", err) + os.Exit(2) + } + + t.Execute(out, vars{ + runtime.Version(), + runtime.GOOS, + runtime.GOARCH, + runtime.NumCPU(), + os.Getenv("GOPATH"), + runtime.GOROOT(), + runtime.Compiler, + version, + getBeegoVersion(), + }) +} diff --git a/fix.go b/fix.go index 855fc0a..5e7487e 100644 --- a/fix.go +++ b/fix.go @@ -8,6 +8,7 @@ import ( "path/filepath" "regexp" "strings" + "fmt" ) var cmdFix = &Command{ @@ -25,9 +26,12 @@ func init() { } func runFix(cmd *Command, args []string) int { + ShowShortVersionBanner() + + ColorLog("[INFO] Upgrading the application...\n") dir, err := os.Getwd() if err != nil { - ColorLog("GetCurrent Path:%s\n", err) + ColorLog("[ERRO] GetCurrent Path:%s\n", err) } filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if info.IsDir() { @@ -42,13 +46,14 @@ func runFix(cmd *Command, args []string) int { if strings.HasSuffix(info.Name(), ".exe") { return nil } - ColorLog("%s\n", path) err = fixFile(path) + fmt.Println("\tfix\t", path) if err != nil { - ColorLog("fixFile:%s\n", err) + ColorLog("[ERRO] Could not fix file: %s\n", err) } return err }) + ColorLog("[INFO] Upgrade done!\n") return 0 } diff --git a/g.go b/g.go index e0eb265..567e247 100644 --- a/g.go +++ b/g.go @@ -14,7 +14,10 @@ package main -import "os" +import ( + "os" + "strings" +) var cmdGenerate = &Command{ UsageLine: "generate [Command]", @@ -74,6 +77,8 @@ func init() { } func generateCode(cmd *Command, args []string) int { + ShowShortVersionBanner() + curpath, _ := os.Getwd() if len(args) < 1 { ColorLog("[ERRO] command is missing\n") @@ -119,7 +124,6 @@ func generateCode(cmd *Command, args []string) int { os.Exit(2) } sname := args[1] - ColorLog("[INFO] Using '%s' as scaffold name\n", sname) generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String()) case "docs": generateDocs(curpath) @@ -192,7 +196,6 @@ func generateCode(cmd *Command, args []string) int { os.Exit(2) } sname := args[1] - ColorLog("[INFO] Using '%s' as model name\n", sname) generateModel(sname, fields.String(), curpath) case "view": if len(args) == 2 { @@ -206,6 +209,6 @@ func generateCode(cmd *Command, args []string) int { default: ColorLog("[ERRO] command is missing\n") } - ColorLog("[SUCC] generate successfully created!\n") + ColorLog("[SUCC] %s successfully generated!\n", strings.Title(gcmd)) return 0 } diff --git a/g_controllers.go b/g_controllers.go index 49b5fd9..a4815c0 100644 --- a/g_controllers.go +++ b/g_controllers.go @@ -18,6 +18,7 @@ import ( "os" "path" "strings" + "fmt" ) // article @@ -58,7 +59,7 @@ func generateController(cname, crupath string) { f.WriteString(content) // gofmt generated source code formatSourceCode(fpath) - ColorLog("[INFO] controller file generated: %s\n", fpath) + fmt.Println("\tcreate\t", fpath) } else { // error creating file ColorLog("[ERRO] Could not create controller file: %s\n", err) diff --git a/g_migration.go b/g_migration.go index d0d296d..ee98aa2 100644 --- a/g_migration.go +++ b/g_migration.go @@ -51,7 +51,7 @@ func generateMigration(mname, upsql, downsql, curpath string) { f.WriteString(content) // gofmt generated source code formatSourceCode(fpath) - ColorLog("[INFO] Migration file generated: %s\n", fpath) + fmt.Println("\tcreate\t", fpath) } else { // error creating file ColorLog("[ERRO] Could not create migration file: %s\n", err) diff --git a/g_model.go b/g_model.go index 859164f..3a45ea1 100644 --- a/g_model.go +++ b/g_model.go @@ -5,6 +5,7 @@ import ( "os" "path" "strings" + "fmt" ) func generateModel(mname, fields, crupath string) { @@ -44,7 +45,7 @@ func generateModel(mname, fields, crupath string) { f.WriteString(content) // gofmt generated source code formatSourceCode(fpath) - ColorLog("[INFO] model file generated: %s\n", fpath) + fmt.Println("\tcreate\t", fpath) } else { // error creating file ColorLog("[ERRO] Could not create model file: %s\n", err) diff --git a/g_scaffold.go b/g_scaffold.go index a9df9d8..f1e7f24 100644 --- a/g_scaffold.go +++ b/g_scaffold.go @@ -7,23 +7,23 @@ import ( func generateScaffold(sname, fields, crupath, driver, conn string) { // generate model - ColorLog("[INFO] Do you want me to create a %v model? [yes|no]] ", sname) + ColorLog("[INFO] Do you want to create a %v model? [yes|no]] ", sname) if askForConfirmation() { generateModel(sname, fields, crupath) } // generate controller - ColorLog("[INFO] Do you want me to create a %v controller? [yes|no]] ", sname) + ColorLog("[INFO] Do you want to create a %v controller? [yes|no]] ", sname) if askForConfirmation() { generateController(sname, crupath) } // generate view - ColorLog("[INFO] Do you want me to create views for this %v resource? [yes|no]] ", sname) + ColorLog("[INFO] Do you want to create views for this %v resource? [yes|no]] ", sname) if askForConfirmation() { generateView(sname, crupath) } // generate migration - ColorLog("[INFO] Do you want me to create a %v migration and schema for this resource? [yes|no]] ", sname) + ColorLog("[INFO] Do you want to create a %v migration and schema for this resource? [yes|no]] ", sname) if askForConfirmation() { upsql := "" downsql := "" @@ -34,7 +34,7 @@ func generateScaffold(sname, fields, crupath, driver, conn string) { generateMigration(sname, upsql, downsql, crupath) } // run migration - ColorLog("[INFO] Do you want to go ahead and migrate the database? [yes|no]] ") + ColorLog("[INFO] Do you want to migrate the database? [yes|no]] ") if askForConfirmation() { migrateUpdate(crupath, driver, conn) } @@ -48,12 +48,12 @@ func generateSQLFromFields(fields string) string { for i, v := range fds { kv := strings.SplitN(v, ":", 2) if len(kv) != 2 { - ColorLog("[ERRO] the fields format is wrong. should key:type,key:type " + v + "\n") + ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n") return "" } typ, tag := getSqlType(kv[1]) if typ == "" { - ColorLog("[ERRO] the fields format is wrong. should key:type,key:type " + v + "\n") + ColorLog("[ERRO] Fields format is wrong. Should be: key:type,key:type " + v + "\n") return "" } if i == 0 && strings.ToLower(kv[0]) != "id" { diff --git a/g_views.go b/g_views.go index 413c86f..47c252c 100644 --- a/g_views.go +++ b/g_views.go @@ -3,18 +3,20 @@ package main import ( "os" "path" + "fmt" ) // recipe // admin/recipe func generateView(vpath, crupath string) { + ColorLog("[INFO] Generating view...\n") absvpath := path.Join(crupath, "views", vpath) os.MkdirAll(absvpath, os.ModePerm) cfile := path.Join(absvpath, "index.tpl") if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer f.Close() f.WriteString(cfile) - ColorLog("[INFO] Created: %v\n", cfile) + fmt.Println("\tcreate\t", cfile) } else { ColorLog("[ERRO] Could not create view file: %s\n", err) os.Exit(2) @@ -23,7 +25,7 @@ func generateView(vpath, crupath string) { if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer f.Close() f.WriteString(cfile) - ColorLog("[INFO] Created: %v\n", cfile) + fmt.Println("\tcreate\t", cfile) } else { ColorLog("[ERRO] Could not create view file: %s\n", err) os.Exit(2) @@ -32,7 +34,7 @@ func generateView(vpath, crupath string) { if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer f.Close() f.WriteString(cfile) - ColorLog("[INFO] Created: %v\n", cfile) + fmt.Println("\tcreate\t", cfile) } else { ColorLog("[ERRO] Could not create view file: %s\n", err) os.Exit(2) @@ -41,7 +43,7 @@ func generateView(vpath, crupath string) { if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer f.Close() f.WriteString(cfile) - ColorLog("[INFO] Created: %v\n", cfile) + fmt.Println("\tcreate\t", cfile) } else { ColorLog("[ERRO] Could not create view file: %s\n", err) os.Exit(2) diff --git a/hproseapp.go b/hproseapp.go index 40abc47..8200de6 100644 --- a/hproseapp.go +++ b/hproseapp.go @@ -255,6 +255,8 @@ func init() { } func createhprose(cmd *Command, args []string) int { + ShowShortVersionBanner() + curpath, _ := os.Getwd() if len(args) > 1 { cmd.Flag.Parse(args[1:]) @@ -269,11 +271,14 @@ func createhprose(cmd *Command, args []string) int { } if conn == "" { } + + ColorLog("[INFO] Creating Hprose application...\n") + os.MkdirAll(apppath, 0755) - fmt.Println("create app folder:", apppath) + fmt.Println("\tcreate\t", apppath) os.Mkdir(path.Join(apppath, "conf"), 0755) - fmt.Println("create conf:", path.Join(apppath, "conf")) - fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf")) + fmt.Println("\tcreate\t", path.Join(apppath, "conf")) + fmt.Println("\tcreate\t", path.Join(apppath, "conf", "app.conf")) WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(hproseconf, "{{.Appname}}", args[0], -1)) @@ -282,7 +287,7 @@ func createhprose(cmd *Command, args []string) int { ColorLog("[INFO] Using '%s' as 'conn'\n", conn) ColorLog("[INFO] Using '%s' as 'tables'\n", tables) generateHproseAppcode(string(driver), string(conn), "1", string(tables), path.Join(curpath, args[0])) - fmt.Println("create main.go:", path.Join(apppath, "main.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "main.go")) maingoContent := strings.Replace(hproseMainconngo, "{{.Appname}}", packpath, -1) maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1) maingoContent = strings.Replace(maingoContent, "{{HproseFunctionList}}", strings.Join(hproseAddFunctions, ""), -1) @@ -301,17 +306,18 @@ func createhprose(cmd *Command, args []string) int { ) } else { os.Mkdir(path.Join(apppath, "models"), 0755) - fmt.Println("create models:", path.Join(apppath, "models")) + fmt.Println("\tcreate\t", path.Join(apppath, "models")) - fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "models", "object.go")) WriteToFile(path.Join(apppath, "models", "object.go"), apiModels) - fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "models", "user.go")) WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2) - fmt.Println("create main.go:", path.Join(apppath, "main.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "main.go")) WriteToFile(path.Join(apppath, "main.go"), strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1)) } + ColorLog("[SUCC] New Hprose application successfully created!\n") return 0 } diff --git a/migrate.go b/migrate.go index 2e3d8b2..3dcc69b 100644 --- a/migrate.go +++ b/migrate.go @@ -62,6 +62,8 @@ func init() { // runMigration is the entry point for starting a migration func runMigration(cmd *Command, args []string) int { + ShowShortVersionBanner() + crupath, _ := os.Getwd() gopath := os.Getenv("GOPATH") diff --git a/new.go b/new.go index 3c6fff9..3a7e87c 100644 --- a/new.go +++ b/new.go @@ -55,6 +55,8 @@ func init() { } func createApp(cmd *Command, args []string) int { + ShowShortVersionBanner() + if len(args) != 1 { ColorLog("[ERRO] Argument [appname] is missing\n") os.Exit(2) @@ -79,46 +81,46 @@ func createApp(cmd *Command, args []string) int { } } - fmt.Println("[INFO] Creating application...") + ColorLog("[INFO] Creating application...\n") os.MkdirAll(apppath, 0755) - fmt.Println(apppath + string(path.Separator)) + fmt.Println("\tcreate\t", apppath + string(path.Separator)) os.Mkdir(path.Join(apppath, "conf"), 0755) - fmt.Println(path.Join(apppath, "conf") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "conf") + string(path.Separator)) os.Mkdir(path.Join(apppath, "controllers"), 0755) - fmt.Println(path.Join(apppath, "controllers") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "controllers") + string(path.Separator)) os.Mkdir(path.Join(apppath, "models"), 0755) - fmt.Println(path.Join(apppath, "models") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "models") + string(path.Separator)) os.Mkdir(path.Join(apppath, "routers"), 0755) - fmt.Println(path.Join(apppath, "routers") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "routers") + string(path.Separator)) os.Mkdir(path.Join(apppath, "tests"), 0755) - fmt.Println(path.Join(apppath, "tests") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "tests") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static"), 0755) - fmt.Println(path.Join(apppath, "static") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "static") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "js"), 0755) - fmt.Println(path.Join(apppath, "static", "js") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "static", "js") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "css"), 0755) - fmt.Println(path.Join(apppath, "static", "css") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "static", "css") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "img"), 0755) - fmt.Println(path.Join(apppath, "static", "img") + string(path.Separator)) - fmt.Println(path.Join(apppath, "views") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "static", "img") + string(path.Separator)) + fmt.Println("\tcreate\t", path.Join(apppath, "views") + string(path.Separator)) os.Mkdir(path.Join(apppath, "views"), 0755) - fmt.Println(path.Join(apppath, "conf", "app.conf")) + fmt.Println("\tcreate\t", path.Join(apppath, "conf", "app.conf")) WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", path.Base(args[0]), -1)) - fmt.Println(path.Join(apppath, "controllers", "default.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "controllers", "default.go")) WriteToFile(path.Join(apppath, "controllers", "default.go"), controllers) - fmt.Println(path.Join(apppath, "views", "index.tpl")) + fmt.Println("\tcreate\t", path.Join(apppath, "views", "index.tpl")) WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl) - fmt.Println(path.Join(apppath, "routers", "router.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "routers", "router.go")) WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) - fmt.Println(path.Join(apppath, "tests", "default_test.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "tests", "default_test.go")) WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) - fmt.Println(path.Join(apppath, "main.go")) + fmt.Println("\tcreate\t", path.Join(apppath, "main.go")) WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) ColorLog("[SUCC] New application successfully created!\n") diff --git a/pack.go b/pack.go index 9d66088..c08bded 100644 --- a/pack.go +++ b/pack.go @@ -242,7 +242,7 @@ func (wft *walkFileTree) walkLeaf(fpath string, fi os.FileInfo, err error) error if added, err := wft.wak.compress(name, fpath, fi); added { if verbose { - fmt.Printf("Compressed: %s\n", name) + fmt.Printf("\t+ Compressed: %s\n", name) } wft.allfiles[name] = true return err @@ -397,10 +397,10 @@ func (wft *zipWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) { func packDirectory(excludePrefix []string, excludeSuffix []string, excludeRegexp []*regexp.Regexp, includePath ...string) (err error) { - fmt.Printf("exclude relpath prefix: %s\n", strings.Join(excludePrefix, ":")) - fmt.Printf("exclude relpath suffix: %s\n", strings.Join(excludeSuffix, ":")) + ColorLog("Excluding relpath prefix: %s\n", strings.Join(excludePrefix, ":")) + ColorLog("Excluding relpath suffix: %s\n", strings.Join(excludeSuffix, ":")) if len(excludeRegexp) > 0 { - fmt.Printf("exclude filename regex: `%s`\n", strings.Join(excludeR, "`, `")) + ColorLog("Excluding filename regex: `%s`\n", strings.Join(excludeR, "`, `")) } w, err := os.OpenFile(outputP, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) @@ -472,6 +472,8 @@ func isBeegoProject(thePath string) bool { } func packApp(cmd *Command, args []string) int { + ShowShortVersionBanner() + curPath, _ := os.Getwd() thePath := "" @@ -493,17 +495,17 @@ func packApp(cmd *Command, args []string) int { thePath, err := path.Abs(appPath) if err != nil { - exitPrint(fmt.Sprintf("wrong app path: %s", thePath)) + exitPrint(fmt.Sprintf("Wrong app path: %s", thePath)) } if stat, err := os.Stat(thePath); os.IsNotExist(err) || stat.IsDir() == false { - exitPrint(fmt.Sprintf("not exist app path: %s", thePath)) + exitPrint(fmt.Sprintf("App path does not exist: %s", thePath)) } if isBeegoProject(thePath) == false { - exitPrint(fmt.Sprintf("not support non beego project")) + exitPrint(fmt.Sprintf("Bee does not support non Beego project")) } - fmt.Printf("app path: %s\n", thePath) + ColorLog("Packaging application: %s\n", thePath) appName := path.Base(thePath) @@ -523,8 +525,7 @@ func packApp(cmd *Command, args []string) int { os.Mkdir(tmpdir, 0700) if build { - fmt.Println("build", appName) - + ColorLog("Building application...\n") var envs []string for _, env := range buildEnvs { parts := strings.SplitN(env, "=", 2) @@ -546,7 +547,7 @@ func packApp(cmd *Command, args []string) int { os.Setenv("GOOS", goos) os.Setenv("GOARCH", goarch) - fmt.Println("GOOS", goos, "GOARCH", goarch) + ColorLog("Env: GOOS=%s GOARCH=%s\n", goos, goarch) binPath := path.Join(tmpdir, appName) if goos == "windows" { @@ -559,7 +560,7 @@ func packApp(cmd *Command, args []string) int { } if verbose { - fmt.Println("go ", strings.Join(args, " ")) + fmt.Println("\t+ go", strings.Join(args, " ")) } execmd := exec.Command("go", args...) @@ -572,7 +573,7 @@ func packApp(cmd *Command, args []string) int { exitPrint(err.Error()) } - fmt.Println("build success") + ColorLog("Build successful\n") } switch format { @@ -624,6 +625,6 @@ func packApp(cmd *Command, args []string) int { exitPrint(err.Error()) } - fmt.Printf("file write to `%s`\n", outputP) + ColorLog("Writing to output: `%s`\n", outputP) return 0 } diff --git a/run.go b/run.go index 3d4da70..1200e4c 100644 --- a/run.go +++ b/run.go @@ -15,11 +15,8 @@ package main import ( - "fmt" "io/ioutil" - "log" "os" - "os/exec" path "path/filepath" "runtime" "strings" @@ -58,13 +55,7 @@ func init() { var appname string func runApp(cmd *Command, args []string) int { - fmt.Println("bee :" + version) - fmt.Println("beego :" + getbeegoVersion()) - goversion, err := exec.Command("go", "version").Output() - if err != nil { - log.Fatal(err) - } - fmt.Println("Go :" + string(goversion)) + ShowShortVersionBanner() exit := make(chan bool) crupath, _ := os.Getwd() @@ -85,7 +76,7 @@ func runApp(cmd *Command, args []string) int { } Debugf("current path:%s\n", crupath) - err = loadConfig() + err := loadConfig() if err != nil { ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err) } diff --git a/version.go b/version.go index 23c461f..43931e1 100644 --- a/version.go +++ b/version.go @@ -4,11 +4,10 @@ import ( "bufio" "fmt" "io" - "log" "os" - "os/exec" path "path/filepath" "regexp" + "bytes" ) var cmdVersion = &Command{ @@ -25,30 +24,59 @@ bee version `, } +const verboseVersionBanner = +`______ +| ___ \ +| |_/ / ___ ___ +| ___ \ / _ \ / _ \ +| |_/ /| __/| __/ +\____/ \___| \___| v{{ .BeeVersion }} + +Beego : {{ .BeegoVersion }} +GoVersion : {{ .GoVersion }} +GOOS : {{ .GOOS }} +GOARCH : {{ .GOARCH }} +NumCPU : {{ .NumCPU }} +GOPATH : {{ .GOPATH }} +GOROOT : {{ .GOROOT }} +Compiler : {{ .Compiler }} +Date : {{ Now "Monday, 2 Jan 2006" }} +` + +const shortVersionBanner = +`______ +| ___ \ +| |_/ / ___ ___ +| ___ \ / _ \ / _ \ +| |_/ /| __/| __/ +\____/ \___| \___| v{{ .BeeVersion }} +` + func init() { cmdVersion.Run = versionCmd } func versionCmd(cmd *Command, args []string) int { - fmt.Println("bee :" + version) - fmt.Println("beego :" + getbeegoVersion()) - //fmt.Println("Go :" + runtime.Version()) - goversion, err := exec.Command("go", "version").Output() - if err != nil { - log.Fatal(err) - } - fmt.Println("Go :" + string(goversion)) + ShowVerboseVersionBanner() return 0 } -func getbeegoVersion() string { +func ShowVerboseVersionBanner() { + InitBanner(os.Stdout, bytes.NewBufferString(verboseVersionBanner)) +} + +func ShowShortVersionBanner() { + InitBanner(os.Stdout, bytes.NewBufferString(shortVersionBanner)) +} + +func getBeegoVersion() string { gopath := os.Getenv("GOPATH") re, err := regexp.Compile(`VERSION = "([0-9.]+)"`) if err != nil { return "" } if gopath == "" { - err = fmt.Errorf("you should set GOPATH in the env") + err = fmt.Errorf("You should set GOPATH env variable") return "" } wgopath := path.SplitList(gopath) @@ -60,11 +88,11 @@ func getbeegoVersion() string { if os.IsNotExist(err) { continue } - ColorLog("[ERRO] get beego.go has error\n") + ColorLog("[ERRO] Get `beego.go` has error\n") } fd, err := os.Open(filename) if err != nil { - ColorLog("[ERRO] open beego.go has error\n") + ColorLog("[ERRO] Open `beego.go` has error\n") continue } reader := bufio.NewReader(fd) @@ -84,5 +112,5 @@ func getbeegoVersion() string { } } - return "you don't install beego,install first: github.com/astaxie/beego" + return "Beego not installed. Please install it first: https://github.com/astaxie/beego" }