From 01e7ff37749d3c060bb9bfcfcdbbc2294c6e86a6 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Thu, 26 May 2016 18:17:24 +0200 Subject: [PATCH 1/2] 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 2/2] 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" }