Skip to content

Commit 5005bd4

Browse files
authored
Merge pull request #691 from beego/develop
support go mod and bee pro gen
2 parents 8e91f22 + b90f934 commit 5005bd4

38 files changed

+2276
-79
lines changed

.travis.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
language: go
22
go:
3-
- 1.10.3
3+
- 1.12.17
44
install:
55
- export PATH=$PATH:$HOME/gopath/bin
66
- go get -u github.com/opennota/check/cmd/structcheck
7-
- go get -u honnef.co/go/tools/cmd/gosimple
87
- go get -u honnef.co/go/tools/cmd/staticcheck
9-
- go get -u honnef.co/go/tools/cmd/unused
108
- go get -u github.com/mdempsky/unconvert
119
- go get -u github.com/gordonklaus/ineffassign
1210
script:
11+
- pwd
12+
- cd $(dirname `dirname $(pwd)`)/beego/bee
13+
- export GO111MODULE="on"
14+
- go mod download
1315
- find . ! \( -path './vendor' -prune \) -type f -name '*.go' -print0 | xargs -0 gofmt -l -s
14-
- go vet $(go list ./... | grep -v /vendor/)
15-
- structcheck $(go list ./... | grep -v /vendor/)
16-
- gosimple -ignore "$(cat gosimple.ignore)" $(go list ./... | grep -v /vendor/)
17-
- staticcheck -ignore "$(cat staticcheck.ignore)" $(go list ./... | grep -v /vendor/)
18-
- unused $(go list ./... | grep -v /vendor/)
19-
- unconvert $(go list ./... | grep -v /vendor/)
16+
- go list ./... | grep -v /vendor/ | grep -v /pkg/mod/
17+
- go vet $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
18+
- structcheck $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
19+
- staticcheck $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
20+
- unconvert $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
2021
- ineffassign .

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Bee is a command-line tool facilitating development of Beego-based application.
99

1010
## Requirements
1111

12-
- Go version >= 1.3.
12+
- Go version >= 1.12.
1313

1414
## Installation
1515

cmd/bee.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
_ "github.com/beego/bee/cmd/commands/api"
2121
_ "github.com/beego/bee/cmd/commands/bale"
2222
_ "github.com/beego/bee/cmd/commands/beefix"
23+
_ "github.com/beego/bee/cmd/commands/beegopro"
2324
_ "github.com/beego/bee/cmd/commands/dlv"
2425
_ "github.com/beego/bee/cmd/commands/dockerize"
2526
_ "github.com/beego/bee/cmd/commands/generate"

cmd/commands/api/apiapp.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package apiapp
1616

1717
import (
1818
"fmt"
19+
"github.com/beego/bee/logger/colors"
1920
"os"
2021
path "path/filepath"
2122
"strings"
@@ -35,14 +36,15 @@ var CmdApiapp = &commands.Command{
3536
The command 'api' creates a Beego API application.
3637
3738
{{"Example:"|bold}}
38-
$ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
39+
$ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-module=true] [-beego=v1.12.1]
3940
4041
If 'conn' argument is empty, the command will generate an example API application. Otherwise the command
4142
will connect to your database and generate models based on the existing tables.
4243
4344
The command 'api' creates a folder named [appname] with the following structure:
4445
4546
├── main.go
47+
├── go.mod
4648
├── {{"conf"|foldername}}
4749
│ └── app.conf
4850
├── {{"controllers"|foldername}}
@@ -103,6 +105,14 @@ func main() {
103105
beego.Run()
104106
}
105107
108+
`
109+
var goMod = `
110+
module %s
111+
112+
go %s
113+
114+
require github.com/astaxie/beego %s
115+
require github.com/smartystreets/goconvey v1.6.4
106116
`
107117

108118
var apirouter = `// @APIVersion 1.0.0
@@ -533,11 +543,15 @@ func TestGet(t *testing.T) {
533543
}
534544
535545
`
546+
var module utils.DocValue
547+
var beegoVersion utils.DocValue
536548

537549
func init() {
538550
CmdApiapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.")
539551
CmdApiapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
540552
CmdApiapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.")
553+
CmdApiapp.Flag.Var(&module, "module", "Support go modules")
554+
CmdApiapp.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by -module=true")
541555
commands.AvailableCommands = append(commands.AvailableCommands, CmdApiapp)
542556
}
543557

@@ -548,14 +562,38 @@ func createAPI(cmd *commands.Command, args []string) int {
548562
beeLogger.Log.Fatal("Argument [appname] is missing")
549563
}
550564

551-
if len(args) > 1 {
552-
err := cmd.Flag.Parse(args[1:])
565+
if len(args) >= 2 {
566+
cmd.Flag.Parse(args[1:])
567+
} else {
568+
module = "false"
569+
}
570+
var appPath string
571+
var packPath string
572+
var err error
573+
if module != `true` {
574+
beeLogger.Log.Info("generate api project support GOPATH")
575+
version.ShowShortVersionBanner()
576+
appPath, packPath, err = utils.CheckEnv(args[0])
553577
if err != nil {
554-
beeLogger.Log.Error(err.Error())
578+
beeLogger.Log.Fatalf("%s", err)
579+
}
580+
} else {
581+
beeLogger.Log.Info("generate api project support go modules.")
582+
appPath = path.Join(utils.GetBeeWorkPath(), args[0])
583+
packPath = args[0]
584+
if beegoVersion.String() == `` {
585+
beegoVersion.Set(`v1.12.1`)
586+
}
587+
}
588+
589+
if utils.IsExist(appPath) {
590+
beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), appPath)
591+
beeLogger.Log.Warn(colors.Bold("Do you want to overwrite it? [Yes|No] "))
592+
if !utils.AskForConfirmation() {
593+
os.Exit(2)
555594
}
556595
}
557596

558-
appPath, packPath, err := utils.CheckEnv(args[0])
559597
appName := path.Base(args[0])
560598
if err != nil {
561599
beeLogger.Log.Fatalf("%s", err)
@@ -567,6 +605,10 @@ func createAPI(cmd *commands.Command, args []string) int {
567605
beeLogger.Log.Info("Creating API...")
568606

569607
os.MkdirAll(appPath, 0755)
608+
if module == `true` { //generate first for calc model name
609+
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "go.mod"), "\x1b[0m")
610+
utils.WriteToFile(path.Join(appPath, "go.mod"), fmt.Sprintf(goMod, packPath, utils.GetGoVersionSkipMinor(), beegoVersion.String()))
611+
}
570612
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath, "\x1b[0m")
571613
os.Mkdir(path.Join(appPath, "conf"), 0755)
572614
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf"), "\x1b[0m")

cmd/commands/beegopro/beegopro.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2013 bee authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"): you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
package beegopro
15+
16+
import (
17+
"github.com/beego/bee/cmd/commands"
18+
"github.com/beego/bee/internal/app/module/beegopro"
19+
"github.com/beego/bee/logger"
20+
"strings"
21+
)
22+
23+
var CmdBeegoPro = &commands.Command{
24+
UsageLine: "pro [command]",
25+
Short: "Source code generator",
26+
Long: ``,
27+
Run: BeegoPro,
28+
}
29+
30+
func init() {
31+
CmdBeegoPro.Flag.Var(&beegopro.SQL, "sql", "sql file path")
32+
CmdBeegoPro.Flag.Var(&beegopro.SQLMode, "sqlmode", "sql mode")
33+
CmdBeegoPro.Flag.Var(&beegopro.SQLModePath, "sqlpath", "sql mode path")
34+
commands.AvailableCommands = append(commands.AvailableCommands, CmdBeegoPro)
35+
}
36+
37+
func BeegoPro(cmd *commands.Command, args []string) int {
38+
if len(args) < 1 {
39+
beeLogger.Log.Fatal("Command is missing")
40+
}
41+
42+
if len(args) >= 2 {
43+
cmd.Flag.Parse(args[1:])
44+
}
45+
46+
gcmd := args[0]
47+
switch gcmd {
48+
case "gen":
49+
beegopro.DefaultBeegoPro.Run()
50+
case "config":
51+
beegopro.DefaultBeegoPro.GenConfig()
52+
case "migration":
53+
beegopro.DefaultBeegoPro.Migration(args)
54+
default:
55+
beeLogger.Log.Fatal("Command is missing")
56+
}
57+
beeLogger.Log.Successf("%s successfully generated!", strings.Title(gcmd))
58+
return 0
59+
}

cmd/commands/generate/generate.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,6 @@ func GenerateCode(cmd *commands.Command, args []string) int {
8181
beeLogger.Log.Fatal("Command is missing")
8282
}
8383

84-
gps := utils.GetGOPATHs()
85-
if len(gps) == 0 {
86-
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
87-
}
88-
89-
gopath := gps[0]
90-
91-
beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath)
92-
9384
gcmd := args[0]
9485
switch gcmd {
9586
case "scaffold":

cmd/commands/hprose/hprose.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package hprose
22

33
import (
4-
"os"
5-
64
"fmt"
5+
"github.com/beego/bee/logger/colors"
6+
"os"
77
"path"
88
"strings"
99

@@ -24,14 +24,15 @@ var CmdHproseapp = &commands.Command{
2424
2525
{{"To scaffold out your application, use:"|bold}}
2626
27-
$ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
27+
$ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-module=true] [-beego=v1.12.1]
2828
2929
If 'conn' is empty, the command will generate a sample application. Otherwise the command
3030
will connect to your database and generate models based on the existing tables.
3131
3232
The command 'hprose' creates a folder named [appname] with the following structure:
3333
3434
├── main.go
35+
├── go.mod
3536
├── {{"conf"|foldername}}
3637
│ └── app.conf
3738
└── {{"models"|foldername}}
@@ -42,34 +43,76 @@ var CmdHproseapp = &commands.Command{
4243
Run: createhprose,
4344
}
4445

46+
var goMod = `
47+
module %s
48+
49+
go %s
50+
51+
require github.com/astaxie/beego %s
52+
require github.com/smartystreets/goconvey v1.6.4
53+
`
54+
55+
var module utils.DocValue
56+
var beegoVersion utils.DocValue
57+
4558
func init() {
4659
CmdHproseapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.")
4760
CmdHproseapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
4861
CmdHproseapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.")
62+
CmdHproseapp.Flag.Var(&module, "module", "Support go modules")
63+
CmdHproseapp.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by -module=true")
4964
commands.AvailableCommands = append(commands.AvailableCommands, CmdHproseapp)
5065
}
5166

5267
func createhprose(cmd *commands.Command, args []string) int {
5368
output := cmd.Out()
54-
55-
if len(args) != 1 {
69+
if len(args) == 0 {
5670
beeLogger.Log.Fatal("Argument [appname] is missing")
5771
}
5872

5973
curpath, _ := os.Getwd()
6074
if len(args) > 1 {
6175
cmd.Flag.Parse(args[1:])
76+
} else {
77+
module = "false"
6278
}
63-
apppath, packpath, err := utils.CheckEnv(args[0])
64-
if err != nil {
65-
beeLogger.Log.Fatalf("%s", err)
79+
var apppath string
80+
var packpath string
81+
var err error
82+
if module != `true` {
83+
beeLogger.Log.Info("generate api project support GOPATH")
84+
version.ShowShortVersionBanner()
85+
apppath, packpath, err = utils.CheckEnv(args[0])
86+
if err != nil {
87+
beeLogger.Log.Fatalf("%s", err)
88+
}
89+
} else {
90+
beeLogger.Log.Info("generate api project support go modules.")
91+
apppath = path.Join(utils.GetBeeWorkPath(), args[0])
92+
packpath = args[0]
93+
if beegoVersion.String() == `` {
94+
beegoVersion.Set(`v1.12.1`)
95+
}
96+
}
97+
98+
if utils.IsExist(apppath) {
99+
beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), apppath)
100+
beeLogger.Log.Warn(colors.Bold("Do you want to overwrite it? [Yes|No] "))
101+
if !utils.AskForConfirmation() {
102+
os.Exit(2)
103+
}
66104
}
105+
67106
if generate.SQLDriver == "" {
68107
generate.SQLDriver = "mysql"
69108
}
70109
beeLogger.Log.Info("Creating Hprose application...")
71110

72111
os.MkdirAll(apppath, 0755)
112+
if module == `true` { //generate first for calc model name
113+
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "go.mod"), "\x1b[0m")
114+
utils.WriteToFile(path.Join(apppath, "go.mod"), fmt.Sprintf(goMod, packpath, utils.GetGoVersionSkipMinor(), beegoVersion.String()))
115+
}
73116
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
74117
os.Mkdir(path.Join(apppath, "conf"), 0755)
75118
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")

cmd/commands/migrate/migrate.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,6 @@ func init() {
7171
func RunMigration(cmd *commands.Command, args []string) int {
7272
currpath, _ := os.Getwd()
7373

74-
gps := utils.GetGOPATHs()
75-
if len(gps) == 0 {
76-
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
77-
}
78-
79-
gopath := gps[0]
80-
81-
beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath)
82-
8374
// Getting command line arguments
8475
if len(args) != 0 {
8576
cmd.Flag.Parse(args[1:])
@@ -207,8 +198,8 @@ func checkForSchemaUpdateTable(db *sql.DB, driver string) {
207198
beeLogger.Log.Fatalf("Column migration.name type mismatch: TYPE: %s, NULL: %s", typeStr, nullStr)
208199
}
209200
} else if fieldStr == "created_at" {
210-
if typeStr != "timestamp" || defaultStr != "CURRENT_TIMESTAMP" {
211-
beeLogger.Log.Hint("Expecting TYPE: timestamp, DEFAULT: CURRENT_TIMESTAMP")
201+
if typeStr != "timestamp" || (!strings.EqualFold(defaultStr, "CURRENT_TIMESTAMP") && !strings.EqualFold(defaultStr, "CURRENT_TIMESTAMP()")) {
202+
beeLogger.Log.Hint("Expecting TYPE: timestamp, DEFAULT: CURRENT_TIMESTAMP || CURRENT_TIMESTAMP()")
212203
beeLogger.Log.Fatalf("Column migration.timestamp type mismatch: TYPE: %s, DEFAULT: %s", typeStr, defaultStr)
213204
}
214205
}

0 commit comments

Comments
 (0)