From 0163d03646e5040c192b6d85ccd9072a7672a8ef Mon Sep 17 00:00:00 2001 From: qiantao Date: Tue, 26 May 2020 17:58:56 +0800 Subject: [PATCH] fix api hprose project --- cmd/commands/api/apiapp.go | 52 ++++++++++++++++++++++++++--- cmd/commands/hprose/hprose.go | 59 ++++++++++++++++++++++++++++++--- cmd/commands/migrate/migrate.go | 16 ++++----- cmd/commands/new/new.go | 9 +++-- generate/g_appcode.go | 35 ++++++++++++++++++- 5 files changed, 147 insertions(+), 24 deletions(-) diff --git a/cmd/commands/api/apiapp.go b/cmd/commands/api/apiapp.go index 8f9865f..ce3efe9 100644 --- a/cmd/commands/api/apiapp.go +++ b/cmd/commands/api/apiapp.go @@ -16,8 +16,10 @@ package apiapp import ( "fmt" + "github.com/beego/bee/logger/colors" "os" path "path/filepath" + "runtime" "strings" "github.com/beego/bee/cmd/commands" @@ -35,7 +37,7 @@ var CmdApiapp = &commands.Command{ The command 'api' creates a Beego API application. {{"Example:"|bold}} - $ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] + $ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-module=true] [-beego=v1.12.1] If 'conn' argument is empty, the command will generate an example API application. Otherwise the command will connect to your database and generate models based on the existing tables. @@ -43,6 +45,7 @@ var CmdApiapp = &commands.Command{ The command 'api' creates a folder named [appname] with the following structure: ├── main.go + ├── go.mod ├── {{"conf"|foldername}} │ └── app.conf ├── {{"controllers"|foldername}} @@ -103,6 +106,13 @@ func main() { beego.Run() } +` +var goMod = ` +module %s + +go %s + +require github.com/astaxie/beego %s ` var apirouter = `// @APIVersion 1.0.0 @@ -533,11 +543,15 @@ func TestGet(t *testing.T) { } ` +var module utils.DocValue +var beegoVersion utils.DocValue func init() { CmdApiapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.") CmdApiapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.") CmdApiapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.") + CmdApiapp.Flag.Var(&module, "module", "Support go modules") + CmdApiapp.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by -module=true") commands.AvailableCommands = append(commands.AvailableCommands, CmdApiapp) } @@ -548,14 +562,38 @@ func createAPI(cmd *commands.Command, args []string) int { beeLogger.Log.Fatal("Argument [appname] is missing") } - if len(args) > 1 { - err := cmd.Flag.Parse(args[1:]) + if len(args) >= 2 { + cmd.Flag.Parse(args[1:]) + } else { + module = "false" + } + appPath := `` + packPath := `` + var err error + if module != `true` { + beeLogger.Log.Info("generate api project support GOPATH") + version.ShowShortVersionBanner() + appPath, packPath, err = utils.CheckEnv(args[0]) if err != nil { - beeLogger.Log.Error(err.Error()) + beeLogger.Log.Fatalf("%s", err) + } + } else { + beeLogger.Log.Info("generate api project support go modules.") + appPath = path.Join(utils.GetBeeWorkPath(), args[0]) + packPath = args[0] + if beegoVersion.String() == `` { + beegoVersion.Set(`v1.12.1`) + } + } + + if utils.IsExist(appPath) { + beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), appPath) + beeLogger.Log.Warn(colors.Bold("Do you want to overwrite it? [Yes|No] ")) + if !utils.AskForConfirmation() { + os.Exit(2) } } - appPath, packPath, err := utils.CheckEnv(args[0]) appName := path.Base(args[0]) if err != nil { beeLogger.Log.Fatalf("%s", err) @@ -567,6 +605,10 @@ func createAPI(cmd *commands.Command, args []string) int { beeLogger.Log.Info("Creating API...") os.MkdirAll(appPath, 0755) + if module == `true` { //generate first for calc model name + fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "go.mod"), "\x1b[0m") + utils.WriteToFile(path.Join(appPath, "go.mod"), fmt.Sprintf(goMod, packPath, runtime.Version()[2:], beegoVersion.String())) + } fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath, "\x1b[0m") os.Mkdir(path.Join(appPath, "conf"), 0755) fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf"), "\x1b[0m") diff --git a/cmd/commands/hprose/hprose.go b/cmd/commands/hprose/hprose.go index 1d50581..3246bfb 100644 --- a/cmd/commands/hprose/hprose.go +++ b/cmd/commands/hprose/hprose.go @@ -1,7 +1,9 @@ package hprose import ( + "github.com/beego/bee/logger/colors" "os" + "runtime" "fmt" "path" @@ -11,7 +13,7 @@ import ( "github.com/beego/bee/cmd/commands/api" "github.com/beego/bee/cmd/commands/version" "github.com/beego/bee/generate" - beeLogger "github.com/beego/bee/logger" + "github.com/beego/bee/logger" "github.com/beego/bee/utils" ) @@ -24,7 +26,7 @@ var CmdHproseapp = &commands.Command{ {{"To scaffold out your application, use:"|bold}} - $ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] + $ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-module=true] [-beego=v1.12.1] If 'conn' is empty, the command will generate a sample application. Otherwise the command will connect to your database and generate models based on the existing tables. @@ -32,6 +34,7 @@ var CmdHproseapp = &commands.Command{ The command 'hprose' creates a folder named [appname] with the following structure: ├── main.go + ├── go.mod ├── {{"conf"|foldername}} │ └── app.conf └── {{"models"|foldername}} @@ -42,10 +45,23 @@ var CmdHproseapp = &commands.Command{ Run: createhprose, } +var goMod = ` +module %s + +go %s + +require github.com/astaxie/beego %s +` + +var module utils.DocValue +var beegoVersion utils.DocValue + func init() { CmdHproseapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.") CmdHproseapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.") CmdHproseapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.") + CmdHproseapp.Flag.Var(&module, "module", "Support go modules") + CmdHproseapp.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by -module=true") commands.AvailableCommands = append(commands.AvailableCommands, CmdHproseapp) } @@ -60,16 +76,49 @@ func createhprose(cmd *commands.Command, args []string) int { if len(args) > 1 { cmd.Flag.Parse(args[1:]) } - apppath, packpath, err := utils.CheckEnv(args[0]) - if err != nil { - beeLogger.Log.Fatalf("%s", err) + + if len(args) >= 2 { + cmd.Flag.Parse(args[1:]) + } else { + module = "false" } + apppath := `` + packpath := `` + var err error + if module != `true` { + beeLogger.Log.Info("generate api project support GOPATH") + version.ShowShortVersionBanner() + apppath, packpath, err = utils.CheckEnv(args[0]) + if err != nil { + beeLogger.Log.Fatalf("%s", err) + } + } else { + beeLogger.Log.Info("generate api project support go modules.") + apppath = path.Join(utils.GetBeeWorkPath(), args[0]) + packpath = args[0] + if beegoVersion.String() == `` { + beegoVersion.Set(`v1.12.1`) + } + } + + if utils.IsExist(apppath) { + beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), apppath) + beeLogger.Log.Warn(colors.Bold("Do you want to overwrite it? [Yes|No] ")) + if !utils.AskForConfirmation() { + os.Exit(2) + } + } + if generate.SQLDriver == "" { generate.SQLDriver = "mysql" } beeLogger.Log.Info("Creating Hprose application...") os.MkdirAll(apppath, 0755) + if module == `true` { //generate first for calc model name + fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "go.mod"), "\x1b[0m") + utils.WriteToFile(path.Join(apppath, "go.mod"), fmt.Sprintf(goMod, packpath, runtime.Version()[2:], beegoVersion.String())) + } fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m") os.Mkdir(path.Join(apppath, "conf"), 0755) fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m") diff --git a/cmd/commands/migrate/migrate.go b/cmd/commands/migrate/migrate.go index 71e93cd..b2ee14b 100644 --- a/cmd/commands/migrate/migrate.go +++ b/cmd/commands/migrate/migrate.go @@ -71,14 +71,14 @@ func init() { func RunMigration(cmd *commands.Command, args []string) int { currpath, _ := os.Getwd() - gps := utils.GetGOPATHs() - if len(gps) == 0 { - beeLogger.Log.Fatal("GOPATH environment variable is not set or empty") - } - - gopath := gps[0] - - beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath) + //gps := utils.GetGOPATHs() + //if len(gps) == 0 { + // beeLogger.Log.Fatal("GOPATH environment variable is not set or empty") + //} + // + //gopath := gps[0] + // + //beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath) // Getting command line arguments if len(args) != 0 { diff --git a/cmd/commands/new/new.go b/cmd/commands/new/new.go index 6427d0f..2a716cb 100644 --- a/cmd/commands/new/new.go +++ b/cmd/commands/new/new.go @@ -302,6 +302,10 @@ func CreateApp(cmd *commands.Command, args []string) int { beeLogger.Log.Info("Creating application...") os.MkdirAll(appPath, 0755) + if module == `true` { + fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "go.mod"), "\x1b[0m") + utils.WriteToFile(path.Join(appPath, "go.mod"), fmt.Sprintf(goMod, packPath, runtime.Version()[2:], beegoVersion.String())) + } fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath+string(path.Separator), "\x1b[0m") os.Mkdir(path.Join(appPath, "conf"), 0755) fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf")+string(path.Separator), "\x1b[0m") @@ -341,11 +345,6 @@ func CreateApp(cmd *commands.Command, args []string) int { fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "main.go"), "\x1b[0m") utils.WriteToFile(path.Join(appPath, "main.go"), strings.Replace(maingo, "{{.Appname}}", packPath, -1)) - - if module == `true` { - fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "go.mod"), "\x1b[0m") - utils.WriteToFile(path.Join(appPath, "go.mod"), fmt.Sprintf(goMod, packPath, runtime.Version()[2:], beegoVersion.String())) - } beeLogger.Log.Success("New application successfully created!") return 0 } diff --git a/generate/g_appcode.go b/generate/g_appcode.go index 260472a..8489eef 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -15,15 +15,17 @@ package generate import ( + "bufio" "database/sql" "fmt" + "io" "os" "path" "path/filepath" "regexp" "strings" - beeLogger "github.com/beego/bee/logger" + "github.com/beego/bee/logger" "github.com/beego/bee/logger/colors" "github.com/beego/bee/utils" _ "github.com/go-sql-driver/mysql" @@ -946,6 +948,37 @@ func getFileName(tbName string) (filename string) { } func getPackagePath(curpath string) (packpath string) { + if os.Getenv(`GO111MODULE`) == `on` { + beeLogger.Log.Infof("GO111MODULE = on,curpath: %s", curpath) + gomodpath := filepath.Join(curpath, `go.mod`) + re, err := regexp.Compile(`^module\s+(.+)$`) + if err != nil { + beeLogger.Log.Fatalf("generate regexp error:%s", err) + return "" + } + fd, err := os.Open(gomodpath) + if err != nil { + beeLogger.Log.Fatalf("Error while reading 'go.mod',%s", gomodpath) + } + reader := bufio.NewReader(fd) + for { + byteLine, _, er := reader.ReadLine() + if er != nil && er != io.EOF { + return "" + } + if er == io.EOF { + break + } + line := string(byteLine) + s := re.FindStringSubmatch(line) + if len(s) >= 2 { + return s[1] + } + } + beeLogger.Log.Fatalf("Error while parse 'go.mod',%s", gomodpath) + return "" + } + gopath := os.Getenv("GOPATH") if gopath == "" { beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")