Skip to content

Commit

Permalink
Merge pull request #691 from beego/develop
Browse files Browse the repository at this point in the history
support go mod and bee pro gen
  • Loading branch information
askuy authored Jul 19, 2020
2 parents 8e91f22 + b90f934 commit 5005bd4
Show file tree
Hide file tree
Showing 38 changed files with 2,276 additions and 79 deletions.
19 changes: 10 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
language: go
go:
- 1.10.3
- 1.12.17
install:
- export PATH=$PATH:$HOME/gopath/bin
- go get -u github.com/opennota/check/cmd/structcheck
- go get -u honnef.co/go/tools/cmd/gosimple
- go get -u honnef.co/go/tools/cmd/staticcheck
- go get -u honnef.co/go/tools/cmd/unused
- go get -u github.com/mdempsky/unconvert
- go get -u github.com/gordonklaus/ineffassign
script:
- pwd
- cd $(dirname `dirname $(pwd)`)/beego/bee
- export GO111MODULE="on"
- go mod download
- find . ! \( -path './vendor' -prune \) -type f -name '*.go' -print0 | xargs -0 gofmt -l -s
- go vet $(go list ./... | grep -v /vendor/)
- structcheck $(go list ./... | grep -v /vendor/)
- gosimple -ignore "$(cat gosimple.ignore)" $(go list ./... | grep -v /vendor/)
- staticcheck -ignore "$(cat staticcheck.ignore)" $(go list ./... | grep -v /vendor/)
- unused $(go list ./... | grep -v /vendor/)
- unconvert $(go list ./... | grep -v /vendor/)
- go list ./... | grep -v /vendor/ | grep -v /pkg/mod/
- go vet $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
- structcheck $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
- staticcheck $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
- unconvert $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
- ineffassign .
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Bee is a command-line tool facilitating development of Beego-based application.

## Requirements

- Go version >= 1.3.
- Go version >= 1.12.

## Installation

Expand Down
1 change: 1 addition & 0 deletions cmd/bee.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "github.com/beego/bee/cmd/commands/api"
_ "github.com/beego/bee/cmd/commands/bale"
_ "github.com/beego/bee/cmd/commands/beefix"
_ "github.com/beego/bee/cmd/commands/beegopro"
_ "github.com/beego/bee/cmd/commands/dlv"
_ "github.com/beego/bee/cmd/commands/dockerize"
_ "github.com/beego/bee/cmd/commands/generate"
Expand Down
52 changes: 47 additions & 5 deletions cmd/commands/api/apiapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package apiapp

import (
"fmt"
"github.com/beego/bee/logger/colors"
"os"
path "path/filepath"
"strings"
Expand All @@ -35,14 +36,15 @@ 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.
The command 'api' creates a folder named [appname] with the following structure:
├── main.go
├── go.mod
├── {{"conf"|foldername}}
│ └── app.conf
├── {{"controllers"|foldername}}
Expand Down Expand Up @@ -103,6 +105,14 @@ func main() {
beego.Run()
}
`
var goMod = `
module %s
go %s
require github.com/astaxie/beego %s
require github.com/smartystreets/goconvey v1.6.4
`

var apirouter = `// @APIVersion 1.0.0
Expand Down Expand Up @@ -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)
}

Expand All @@ -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"
}
var appPath string
var packPath string
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)
Expand All @@ -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, utils.GetGoVersionSkipMinor(), 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")
Expand Down
59 changes: 59 additions & 0 deletions cmd/commands/beegopro/beegopro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package beegopro

import (
"github.com/beego/bee/cmd/commands"
"github.com/beego/bee/internal/app/module/beegopro"
"github.com/beego/bee/logger"
"strings"
)

var CmdBeegoPro = &commands.Command{
UsageLine: "pro [command]",
Short: "Source code generator",
Long: ``,
Run: BeegoPro,
}

func init() {
CmdBeegoPro.Flag.Var(&beegopro.SQL, "sql", "sql file path")
CmdBeegoPro.Flag.Var(&beegopro.SQLMode, "sqlmode", "sql mode")
CmdBeegoPro.Flag.Var(&beegopro.SQLModePath, "sqlpath", "sql mode path")
commands.AvailableCommands = append(commands.AvailableCommands, CmdBeegoPro)
}

func BeegoPro(cmd *commands.Command, args []string) int {
if len(args) < 1 {
beeLogger.Log.Fatal("Command is missing")
}

if len(args) >= 2 {
cmd.Flag.Parse(args[1:])
}

gcmd := args[0]
switch gcmd {
case "gen":
beegopro.DefaultBeegoPro.Run()
case "config":
beegopro.DefaultBeegoPro.GenConfig()
case "migration":
beegopro.DefaultBeegoPro.Migration(args)
default:
beeLogger.Log.Fatal("Command is missing")
}
beeLogger.Log.Successf("%s successfully generated!", strings.Title(gcmd))
return 0
}
9 changes: 0 additions & 9 deletions cmd/commands/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ func GenerateCode(cmd *commands.Command, args []string) int {
beeLogger.Log.Fatal("Command is missing")
}

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)

gcmd := args[0]
switch gcmd {
case "scaffold":
Expand Down
59 changes: 51 additions & 8 deletions cmd/commands/hprose/hprose.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package hprose

import (
"os"

"fmt"
"github.com/beego/bee/logger/colors"
"os"
"path"
"strings"

Expand All @@ -24,14 +24,15 @@ 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.
The command 'hprose' creates a folder named [appname] with the following structure:
├── main.go
├── go.mod
├── {{"conf"|foldername}}
│ └── app.conf
└── {{"models"|foldername}}
Expand All @@ -42,34 +43,76 @@ var CmdHproseapp = &commands.Command{
Run: createhprose,
}

var goMod = `
module %s
go %s
require github.com/astaxie/beego %s
require github.com/smartystreets/goconvey v1.6.4
`

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)
}

func createhprose(cmd *commands.Command, args []string) int {
output := cmd.Out()

if len(args) != 1 {
if len(args) == 0 {
beeLogger.Log.Fatal("Argument [appname] is missing")
}

curpath, _ := os.Getwd()
if len(args) > 1 {
cmd.Flag.Parse(args[1:])
} else {
module = "false"
}
apppath, packpath, err := utils.CheckEnv(args[0])
if err != nil {
beeLogger.Log.Fatalf("%s", err)
var apppath string
var packpath string
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, utils.GetGoVersionSkipMinor(), 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")
Expand Down
13 changes: 2 additions & 11 deletions cmd/commands/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ 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)

// Getting command line arguments
if len(args) != 0 {
cmd.Flag.Parse(args[1:])
Expand Down Expand Up @@ -207,8 +198,8 @@ func checkForSchemaUpdateTable(db *sql.DB, driver string) {
beeLogger.Log.Fatalf("Column migration.name type mismatch: TYPE: %s, NULL: %s", typeStr, nullStr)
}
} else if fieldStr == "created_at" {
if typeStr != "timestamp" || defaultStr != "CURRENT_TIMESTAMP" {
beeLogger.Log.Hint("Expecting TYPE: timestamp, DEFAULT: CURRENT_TIMESTAMP")
if typeStr != "timestamp" || (!strings.EqualFold(defaultStr, "CURRENT_TIMESTAMP") && !strings.EqualFold(defaultStr, "CURRENT_TIMESTAMP()")) {
beeLogger.Log.Hint("Expecting TYPE: timestamp, DEFAULT: CURRENT_TIMESTAMP || CURRENT_TIMESTAMP()")
beeLogger.Log.Fatalf("Column migration.timestamp type mismatch: TYPE: %s, DEFAULT: %s", typeStr, defaultStr)
}
}
Expand Down
Loading

0 comments on commit 5005bd4

Please sign in to comment.