From 2715aaf5286197983536caaae4584b9822d6e184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D?= Date: Sun, 30 Jun 2019 06:00:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.=20=E5=88=9B=E5=BB=BA=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E5=A6=82=E6=9E=9C=E5=B8=A6=E6=9C=89proto=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=9C=A8api=E7=9B=AE=E5=BD=95=E6=94=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=80=E4=B8=AAgo=20generate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/kratos/main.go | 5 +++++ tool/kratos/new.go | 6 ++++++ tool/kratos/project.go | 14 +++++++++----- tool/kratos/template.go | 36 ++++++++++++++++++++---------------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/tool/kratos/main.go b/tool/kratos/main.go index 778e02496..ec71239cc 100644 --- a/tool/kratos/main.go +++ b/tool/kratos/main.go @@ -35,6 +35,11 @@ func main() { Usage: "whether to use protobuf for create project", Destination: &p.WithGRPC, }, + cli.StringFlag{ + Name: "m", + Usage: "project module name for create project, for `go mod init`", + Destination: &p.ModuleName, + }, }, Action: runNew, }, diff --git a/tool/kratos/new.go b/tool/kratos/new.go index 49d5fb47b..65378a10f 100644 --- a/tool/kratos/new.go +++ b/tool/kratos/new.go @@ -14,6 +14,11 @@ func runNew(ctx *cli.Context) error { return errors.New("required project name") } p.Name = ctx.Args()[0] + + if p.ModuleName == "" { + p.ModuleName = path.Join(p.Owner, p.Name) + } + if p.Path != "" { p.Path = path.Join(p.Path, p.Name) } else { @@ -26,6 +31,7 @@ func runNew(ctx *cli.Context) error { } fmt.Printf("Project: %s\n", p.Name) fmt.Printf("Owner: %s\n", p.Owner) + fmt.Printf("Module Name: %s\n", p.ModuleName) fmt.Printf("WithGRPC: %t\n", p.WithGRPC) fmt.Printf("Directory: %s\n\n", p.Path) fmt.Println("The application has been created.") diff --git a/tool/kratos/project.go b/tool/kratos/project.go index 4a97b7e1a..670b70400 100644 --- a/tool/kratos/project.go +++ b/tool/kratos/project.go @@ -11,11 +11,12 @@ import ( // project project config type project struct { - Name string - Owner string - Path string - WithGRPC bool - Here bool + Name string + Owner string + Path string + WithGRPC bool + Here bool + ModuleName string // 支持项目的自定义module名 (go.mod init) } const ( @@ -36,6 +37,7 @@ const ( _tplTypeModel _tplTypeGRPCServer _tplTypeGomod + _tplTypeAPIGogen ) var ( @@ -78,6 +80,7 @@ var ( _tplTypeHTTPToml: _tplHTTPToml, _tplTypeModel: _tplModel, _tplTypeGomod: _tplGoMod, + _tplTypeAPIGogen: _tplGogen, } ) @@ -85,6 +88,7 @@ func create() (err error) { if p.WithGRPC { files[_tplTypeGRPCServer] = "/internal/server/grpc/server.go" files[_tplTypeAPIProto] = "/api/api.proto" + files[_tplTypeAPIGogen] = "/api/generate.go" tpls[_tplTypeHTTPServer] = _tplPBHTTPServer tpls[_tplTypeGRPCServer] = _tplGRPCServer tpls[_tplTypeGRPCToml] = _tplGRPCToml diff --git a/tool/kratos/template.go b/tool/kratos/template.go index 0207da211..14305397c 100644 --- a/tool/kratos/template.go +++ b/tool/kratos/template.go @@ -72,8 +72,8 @@ import ( "syscall" "time" - "{{.Name}}/internal/server/http" - "{{.Name}}/internal/service" + "{{.ModuleName}}/internal/server/http" + "{{.ModuleName}}/internal/service" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/log" ) @@ -122,9 +122,9 @@ import ( "syscall" "time" - "{{.Name}}/internal/server/grpc" - "{{.Name}}/internal/server/http" - "{{.Name}}/internal/service" + "{{.ModuleName}}/internal/server/grpc" + "{{.ModuleName}}/internal/server/http" + "{{.ModuleName}}/internal/service" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/log" ) @@ -284,7 +284,7 @@ func (d *dao) pingRedis(ctx context.Context) (err error) { import ( "context" - "{{.Name}}/internal/dao" + "{{.ModuleName}}/internal/dao" "github.com/bilibili/kratos/pkg/conf/paladin" ) @@ -324,8 +324,8 @@ import ( "context" "fmt" - pb "{{.Name}}/api" - "{{.Name}}/internal/dao" + pb "{{.ModuleName}}/api" + "{{.ModuleName}}/internal/dao" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/golang/protobuf/ptypes/empty" @@ -381,8 +381,8 @@ func (s *Service) Close() { import ( "net/http" - "{{.Name}}/internal/model" - "{{.Name}}/internal/service" + "{{.ModuleName}}/internal/model" + "{{.ModuleName}}/internal/service" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/log" @@ -442,9 +442,9 @@ func howToStart(c *bm.Context) { import ( "net/http" - pb "{{.Name}}/api" - "{{.Name}}/internal/model" - "{{.Name}}/internal/service" + pb "{{.ModuleName}}/api" + "{{.ModuleName}}/internal/model" + "{{.ModuleName}}/internal/service" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/log" @@ -542,7 +542,7 @@ message HelloResp { type Kratos struct { Hello string }` - _tplGoMod = `module {{.Name}} + _tplGoMod = `module {{.ModuleName}} go 1.12 @@ -573,8 +573,8 @@ replace ( _tplGRPCServer = `package grpc import ( - pb "{{.Name}}/api" - "{{.Name}}/internal/service" + pb "{{.ModuleName}}/api" + "{{.ModuleName}}/internal/service" "github.com/bilibili/kratos/pkg/conf/paladin" "github.com/bilibili/kratos/pkg/net/rpc/warden" ) @@ -597,5 +597,9 @@ func New(svc *service.Service) *warden.Server { } return ws } +` + _tplGogen = `package api + +//go:generate kratos tool protoc --swagger --grpc --bm api.proto ` )