1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 15:10:54 +00:00

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.
This commit is contained in:
Faissal Elamraoui 2016-06-01 14:30:29 +02:00
parent 01e7ff3774
commit b8250ebb72
16 changed files with 230 additions and 112 deletions

View File

@ -546,6 +546,8 @@ func init() {
} }
func createapi(cmd *Command, args []string) int { func createapi(cmd *Command, args []string) int {
ShowShortVersionBanner()
if len(args) < 1 { if len(args) < 1 {
ColorLog("[ERRO] Argument [appname] is missing\n") ColorLog("[ERRO] Argument [appname] is missing\n")
os.Exit(2) os.Exit(2)
@ -565,23 +567,25 @@ func createapi(cmd *Command, args []string) int {
} }
if conn == "" { 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"), WriteToFile(path.Join(apppath, "conf", "app.conf"),
strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1)) strings.Replace(apiconf, "{{.Appname}}", path.Base(args[0]), -1))
if conn != "" { if conn != "" {
fmt.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(apiMainconngo, "{{.Appname}}", packpath, -1)
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1) maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
if driver == "mysql" { if driver == "mysql" {
@ -603,39 +607,40 @@ func createapi(cmd *Command, args []string) int {
generateAppcode(string(driver), string(conn), "3", string(tables), path.Join(apppath, args[0])) generateAppcode(string(driver), string(conn), "3", string(tables), path.Join(apppath, args[0]))
} else { } else {
os.Mkdir(path.Join(apppath, "models"), 0755) 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) 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"), WriteToFile(path.Join(apppath, "controllers", "object.go"),
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1)) 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"), WriteToFile(path.Join(apppath, "controllers", "user.go"),
strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1)) 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"), WriteToFile(path.Join(apppath, "tests", "default_test.go"),
strings.Replace(apiTests, "{{.Appname}}", packpath, -1)) 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"), WriteToFile(path.Join(apppath, "routers", "router.go"),
strings.Replace(apirouter, "{{.Appname}}", packpath, -1)) 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) 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) 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") 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"), WriteToFile(path.Join(apppath, "main.go"),
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1)) strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
} }
ColorLog("[SUCC] New API successfully created!\n")
return 0 return 0
} }
@ -651,10 +656,10 @@ func checkEnv(appname string) (apppath, packpath string, err error) {
apppath = path.Join(gosrcpath, appname) apppath = path.Join(gosrcpath, appname)
if _, e := os.Stat(apppath); os.IsNotExist(e) == false { 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 return
} }
packpath = strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/") packpath = strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/")
return return
} }

View File

@ -46,6 +46,8 @@ func init() {
} }
func runBale(cmd *Command, args []string) int { func runBale(cmd *Command, args []string) int {
ShowShortVersionBanner()
err := loadConfig() err := loadConfig()
if err != nil { if err != nil {
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err) 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) ColorLog("[WARN] Skipped directory( %s )\n", p)
continue continue
} }
ColorLog("[INFO] Packing directory( %s )\n", p) ColorLog("[INFO] Packaging directory( %s )\n", p)
filepath.Walk(p, walkFn) filepath.Walk(p, walkFn)
} }

69
banner.go Normal file
View File

@ -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(),
})
}

11
fix.go
View File

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"fmt"
) )
var cmdFix = &Command{ var cmdFix = &Command{
@ -25,9 +26,12 @@ func init() {
} }
func runFix(cmd *Command, args []string) int { func runFix(cmd *Command, args []string) int {
ShowShortVersionBanner()
ColorLog("[INFO] Upgrading the application...\n")
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { 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 { filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() { if info.IsDir() {
@ -42,13 +46,14 @@ func runFix(cmd *Command, args []string) int {
if strings.HasSuffix(info.Name(), ".exe") { if strings.HasSuffix(info.Name(), ".exe") {
return nil return nil
} }
ColorLog("%s\n", path)
err = fixFile(path) err = fixFile(path)
fmt.Println("\tfix\t", path)
if err != nil { if err != nil {
ColorLog("fixFile:%s\n", err) ColorLog("[ERRO] Could not fix file: %s\n", err)
} }
return err return err
}) })
ColorLog("[INFO] Upgrade done!\n")
return 0 return 0
} }

11
g.go
View File

@ -14,7 +14,10 @@
package main package main
import "os" import (
"os"
"strings"
)
var cmdGenerate = &Command{ var cmdGenerate = &Command{
UsageLine: "generate [Command]", UsageLine: "generate [Command]",
@ -74,6 +77,8 @@ func init() {
} }
func generateCode(cmd *Command, args []string) int { func generateCode(cmd *Command, args []string) int {
ShowShortVersionBanner()
curpath, _ := os.Getwd() curpath, _ := os.Getwd()
if len(args) < 1 { if len(args) < 1 {
ColorLog("[ERRO] command is missing\n") ColorLog("[ERRO] command is missing\n")
@ -119,7 +124,6 @@ func generateCode(cmd *Command, args []string) int {
os.Exit(2) os.Exit(2)
} }
sname := args[1] sname := args[1]
ColorLog("[INFO] Using '%s' as scaffold name\n", sname)
generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String()) generateScaffold(sname, fields.String(), curpath, driver.String(), conn.String())
case "docs": case "docs":
generateDocs(curpath) generateDocs(curpath)
@ -192,7 +196,6 @@ func generateCode(cmd *Command, args []string) int {
os.Exit(2) os.Exit(2)
} }
sname := args[1] sname := args[1]
ColorLog("[INFO] Using '%s' as model name\n", sname)
generateModel(sname, fields.String(), curpath) generateModel(sname, fields.String(), curpath)
case "view": case "view":
if len(args) == 2 { if len(args) == 2 {
@ -206,6 +209,6 @@ func generateCode(cmd *Command, args []string) int {
default: default:
ColorLog("[ERRO] command is missing\n") ColorLog("[ERRO] command is missing\n")
} }
ColorLog("[SUCC] generate successfully created!\n") ColorLog("[SUCC] %s successfully generated!\n", strings.Title(gcmd))
return 0 return 0
} }

View File

@ -18,6 +18,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"fmt"
) )
// article // article
@ -58,7 +59,7 @@ func generateController(cname, crupath string) {
f.WriteString(content) f.WriteString(content)
// gofmt generated source code // gofmt generated source code
formatSourceCode(fpath) formatSourceCode(fpath)
ColorLog("[INFO] controller file generated: %s\n", fpath) fmt.Println("\tcreate\t", fpath)
} else { } else {
// error creating file // error creating file
ColorLog("[ERRO] Could not create controller file: %s\n", err) ColorLog("[ERRO] Could not create controller file: %s\n", err)

View File

@ -51,7 +51,7 @@ func generateMigration(mname, upsql, downsql, curpath string) {
f.WriteString(content) f.WriteString(content)
// gofmt generated source code // gofmt generated source code
formatSourceCode(fpath) formatSourceCode(fpath)
ColorLog("[INFO] Migration file generated: %s\n", fpath) fmt.Println("\tcreate\t", fpath)
} else { } else {
// error creating file // error creating file
ColorLog("[ERRO] Could not create migration file: %s\n", err) ColorLog("[ERRO] Could not create migration file: %s\n", err)

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"fmt"
) )
func generateModel(mname, fields, crupath string) { func generateModel(mname, fields, crupath string) {
@ -44,7 +45,7 @@ func generateModel(mname, fields, crupath string) {
f.WriteString(content) f.WriteString(content)
// gofmt generated source code // gofmt generated source code
formatSourceCode(fpath) formatSourceCode(fpath)
ColorLog("[INFO] model file generated: %s\n", fpath) fmt.Println("\tcreate\t", fpath)
} else { } else {
// error creating file // error creating file
ColorLog("[ERRO] Could not create model file: %s\n", err) ColorLog("[ERRO] Could not create model file: %s\n", err)

View File

@ -7,23 +7,23 @@ import (
func generateScaffold(sname, fields, crupath, driver, conn string) { func generateScaffold(sname, fields, crupath, driver, conn string) {
// generate model // 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() { if askForConfirmation() {
generateModel(sname, fields, crupath) generateModel(sname, fields, crupath)
} }
// generate controller // 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() { if askForConfirmation() {
generateController(sname, crupath) generateController(sname, crupath)
} }
// generate view // 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() { if askForConfirmation() {
generateView(sname, crupath) generateView(sname, crupath)
} }
// generate migration // 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() { if askForConfirmation() {
upsql := "" upsql := ""
downsql := "" downsql := ""
@ -34,7 +34,7 @@ func generateScaffold(sname, fields, crupath, driver, conn string) {
generateMigration(sname, upsql, downsql, crupath) generateMigration(sname, upsql, downsql, crupath)
} }
// run migration // 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() { if askForConfirmation() {
migrateUpdate(crupath, driver, conn) migrateUpdate(crupath, driver, conn)
} }
@ -48,12 +48,12 @@ func generateSQLFromFields(fields string) string {
for i, v := range fds { for i, v := range fds {
kv := strings.SplitN(v, ":", 2) kv := strings.SplitN(v, ":", 2)
if len(kv) != 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 "" return ""
} }
typ, tag := getSqlType(kv[1]) typ, tag := getSqlType(kv[1])
if typ == "" { 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 "" return ""
} }
if i == 0 && strings.ToLower(kv[0]) != "id" { if i == 0 && strings.ToLower(kv[0]) != "id" {

View File

@ -3,18 +3,20 @@ package main
import ( import (
"os" "os"
"path" "path"
"fmt"
) )
// recipe // recipe
// admin/recipe // admin/recipe
func generateView(vpath, crupath string) { func generateView(vpath, crupath string) {
ColorLog("[INFO] Generating view...\n")
absvpath := path.Join(crupath, "views", vpath) absvpath := path.Join(crupath, "views", vpath)
os.MkdirAll(absvpath, os.ModePerm) os.MkdirAll(absvpath, os.ModePerm)
cfile := path.Join(absvpath, "index.tpl") cfile := path.Join(absvpath, "index.tpl")
if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close() defer f.Close()
f.WriteString(cfile) f.WriteString(cfile)
ColorLog("[INFO] Created: %v\n", cfile) fmt.Println("\tcreate\t", cfile)
} else { } else {
ColorLog("[ERRO] Could not create view file: %s\n", err) ColorLog("[ERRO] Could not create view file: %s\n", err)
os.Exit(2) 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 { if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close() defer f.Close()
f.WriteString(cfile) f.WriteString(cfile)
ColorLog("[INFO] Created: %v\n", cfile) fmt.Println("\tcreate\t", cfile)
} else { } else {
ColorLog("[ERRO] Could not create view file: %s\n", err) ColorLog("[ERRO] Could not create view file: %s\n", err)
os.Exit(2) 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 { if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close() defer f.Close()
f.WriteString(cfile) f.WriteString(cfile)
ColorLog("[INFO] Created: %v\n", cfile) fmt.Println("\tcreate\t", cfile)
} else { } else {
ColorLog("[ERRO] Could not create view file: %s\n", err) ColorLog("[ERRO] Could not create view file: %s\n", err)
os.Exit(2) 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 { if f, err := os.OpenFile(cfile, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close() defer f.Close()
f.WriteString(cfile) f.WriteString(cfile)
ColorLog("[INFO] Created: %v\n", cfile) fmt.Println("\tcreate\t", cfile)
} else { } else {
ColorLog("[ERRO] Could not create view file: %s\n", err) ColorLog("[ERRO] Could not create view file: %s\n", err)
os.Exit(2) os.Exit(2)

View File

@ -255,6 +255,8 @@ func init() {
} }
func createhprose(cmd *Command, args []string) int { func createhprose(cmd *Command, args []string) int {
ShowShortVersionBanner()
curpath, _ := os.Getwd() curpath, _ := os.Getwd()
if len(args) > 1 { if len(args) > 1 {
cmd.Flag.Parse(args[1:]) cmd.Flag.Parse(args[1:])
@ -269,11 +271,14 @@ func createhprose(cmd *Command, args []string) int {
} }
if conn == "" { if conn == "" {
} }
ColorLog("[INFO] Creating Hprose application...\n")
os.MkdirAll(apppath, 0755) os.MkdirAll(apppath, 0755)
fmt.Println("create app folder:", apppath) fmt.Println("\tcreate\t", apppath)
os.Mkdir(path.Join(apppath, "conf"), 0755) os.Mkdir(path.Join(apppath, "conf"), 0755)
fmt.Println("create conf:", path.Join(apppath, "conf")) fmt.Println("\tcreate\t", path.Join(apppath, "conf"))
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf")) fmt.Println("\tcreate\t", 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)) 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 'conn'\n", conn)
ColorLog("[INFO] Using '%s' as 'tables'\n", tables) ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
generateHproseAppcode(string(driver), string(conn), "1", string(tables), path.Join(curpath, args[0])) generateHproseAppcode(string(driver), string(conn), "1", string(tables), path.Join(curpath, args[0]))
fmt.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(hproseMainconngo, "{{.Appname}}", packpath, -1)
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1) maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
maingoContent = strings.Replace(maingoContent, "{{HproseFunctionList}}", strings.Join(hproseAddFunctions, ""), -1) maingoContent = strings.Replace(maingoContent, "{{HproseFunctionList}}", strings.Join(hproseAddFunctions, ""), -1)
@ -301,17 +306,18 @@ func createhprose(cmd *Command, args []string) int {
) )
} else { } else {
os.Mkdir(path.Join(apppath, "models"), 0755) 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) 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) 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"), WriteToFile(path.Join(apppath, "main.go"),
strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1)) strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1))
} }
ColorLog("[SUCC] New Hprose application successfully created!\n")
return 0 return 0
} }

View File

@ -62,6 +62,8 @@ func init() {
// runMigration is the entry point for starting a migration // runMigration is the entry point for starting a migration
func runMigration(cmd *Command, args []string) int { func runMigration(cmd *Command, args []string) int {
ShowShortVersionBanner()
crupath, _ := os.Getwd() crupath, _ := os.Getwd()
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")

38
new.go
View File

@ -55,6 +55,8 @@ func init() {
} }
func createApp(cmd *Command, args []string) int { func createApp(cmd *Command, args []string) int {
ShowShortVersionBanner()
if len(args) != 1 { if len(args) != 1 {
ColorLog("[ERRO] Argument [appname] is missing\n") ColorLog("[ERRO] Argument [appname] is missing\n")
os.Exit(2) 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) 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) 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) 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) 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) 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) 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) 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) 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) 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) os.Mkdir(path.Join(apppath, "static", "img"), 0755)
fmt.Println(path.Join(apppath, "static", "img") + string(path.Separator)) fmt.Println("\tcreate\t", path.Join(apppath, "static", "img") + string(path.Separator))
fmt.Println(path.Join(apppath, "views") + string(path.Separator)) fmt.Println("\tcreate\t", path.Join(apppath, "views") + string(path.Separator))
os.Mkdir(path.Join(apppath, "views"), 0755) 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)) 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) 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) 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)) 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)) 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)) 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") ColorLog("[SUCC] New application successfully created!\n")

29
pack.go
View File

@ -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 added, err := wft.wak.compress(name, fpath, fi); added {
if verbose { if verbose {
fmt.Printf("Compressed: %s\n", name) fmt.Printf("\t+ Compressed: %s\n", name)
} }
wft.allfiles[name] = true wft.allfiles[name] = true
return err return err
@ -397,10 +397,10 @@ func (wft *zipWalk) compress(name, fpath string, fi os.FileInfo) (bool, error) {
func packDirectory(excludePrefix []string, excludeSuffix []string, func packDirectory(excludePrefix []string, excludeSuffix []string,
excludeRegexp []*regexp.Regexp, includePath ...string) (err error) { excludeRegexp []*regexp.Regexp, includePath ...string) (err error) {
fmt.Printf("exclude relpath prefix: %s\n", strings.Join(excludePrefix, ":")) ColorLog("Excluding relpath prefix: %s\n", strings.Join(excludePrefix, ":"))
fmt.Printf("exclude relpath suffix: %s\n", strings.Join(excludeSuffix, ":")) ColorLog("Excluding relpath suffix: %s\n", strings.Join(excludeSuffix, ":"))
if len(excludeRegexp) > 0 { 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) 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 { func packApp(cmd *Command, args []string) int {
ShowShortVersionBanner()
curPath, _ := os.Getwd() curPath, _ := os.Getwd()
thePath := "" thePath := ""
@ -493,17 +495,17 @@ func packApp(cmd *Command, args []string) int {
thePath, err := path.Abs(appPath) thePath, err := path.Abs(appPath)
if err != nil { 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 { 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 { 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) appName := path.Base(thePath)
@ -523,8 +525,7 @@ func packApp(cmd *Command, args []string) int {
os.Mkdir(tmpdir, 0700) os.Mkdir(tmpdir, 0700)
if build { if build {
fmt.Println("build", appName) ColorLog("Building application...\n")
var envs []string var envs []string
for _, env := range buildEnvs { for _, env := range buildEnvs {
parts := strings.SplitN(env, "=", 2) parts := strings.SplitN(env, "=", 2)
@ -546,7 +547,7 @@ func packApp(cmd *Command, args []string) int {
os.Setenv("GOOS", goos) os.Setenv("GOOS", goos)
os.Setenv("GOARCH", goarch) os.Setenv("GOARCH", goarch)
fmt.Println("GOOS", goos, "GOARCH", goarch) ColorLog("Env: GOOS=%s GOARCH=%s\n", goos, goarch)
binPath := path.Join(tmpdir, appName) binPath := path.Join(tmpdir, appName)
if goos == "windows" { if goos == "windows" {
@ -559,7 +560,7 @@ func packApp(cmd *Command, args []string) int {
} }
if verbose { if verbose {
fmt.Println("go ", strings.Join(args, " ")) fmt.Println("\t+ go", strings.Join(args, " "))
} }
execmd := exec.Command("go", args...) execmd := exec.Command("go", args...)
@ -572,7 +573,7 @@ func packApp(cmd *Command, args []string) int {
exitPrint(err.Error()) exitPrint(err.Error())
} }
fmt.Println("build success") ColorLog("Build successful\n")
} }
switch format { switch format {
@ -624,6 +625,6 @@ func packApp(cmd *Command, args []string) int {
exitPrint(err.Error()) exitPrint(err.Error())
} }
fmt.Printf("file write to `%s`\n", outputP) ColorLog("Writing to output: `%s`\n", outputP)
return 0 return 0
} }

13
run.go
View File

@ -15,11 +15,8 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec"
path "path/filepath" path "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -58,13 +55,7 @@ func init() {
var appname string var appname string
func runApp(cmd *Command, args []string) int { func runApp(cmd *Command, args []string) int {
fmt.Println("bee :" + version) ShowShortVersionBanner()
fmt.Println("beego :" + getbeegoVersion())
goversion, err := exec.Command("go", "version").Output()
if err != nil {
log.Fatal(err)
}
fmt.Println("Go :" + string(goversion))
exit := make(chan bool) exit := make(chan bool)
crupath, _ := os.Getwd() crupath, _ := os.Getwd()
@ -85,7 +76,7 @@ func runApp(cmd *Command, args []string) int {
} }
Debugf("current path:%s\n", crupath) Debugf("current path:%s\n", crupath)
err = loadConfig() err := loadConfig()
if err != nil { if err != nil {
ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err) ColorLog("[ERRO] Fail to parse bee.json[ %s ]\n", err)
} }

View File

@ -4,11 +4,10 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"os/exec"
path "path/filepath" path "path/filepath"
"regexp" "regexp"
"bytes"
) )
var cmdVersion = &Command{ 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() { func init() {
cmdVersion.Run = versionCmd cmdVersion.Run = versionCmd
} }
func versionCmd(cmd *Command, args []string) int { func versionCmd(cmd *Command, args []string) int {
fmt.Println("bee :" + version) ShowVerboseVersionBanner()
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))
return 0 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") gopath := os.Getenv("GOPATH")
re, err := regexp.Compile(`VERSION = "([0-9.]+)"`) re, err := regexp.Compile(`VERSION = "([0-9.]+)"`)
if err != nil { if err != nil {
return "" return ""
} }
if gopath == "" { if gopath == "" {
err = fmt.Errorf("you should set GOPATH in the env") err = fmt.Errorf("You should set GOPATH env variable")
return "" return ""
} }
wgopath := path.SplitList(gopath) wgopath := path.SplitList(gopath)
@ -60,11 +88,11 @@ func getbeegoVersion() string {
if os.IsNotExist(err) { if os.IsNotExist(err) {
continue continue
} }
ColorLog("[ERRO] get beego.go has error\n") ColorLog("[ERRO] Get `beego.go` has error\n")
} }
fd, err := os.Open(filename) fd, err := os.Open(filename)
if err != nil { if err != nil {
ColorLog("[ERRO] open beego.go has error\n") ColorLog("[ERRO] Open `beego.go` has error\n")
continue continue
} }
reader := bufio.NewReader(fd) 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"
} }