mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
fix: quickstart wrong package when go.mod exists in parent dir (#2048)
* chore: fix typo * fix: quickstart in dir with go.mod * fix: runner failed * chore: refine code * chore: simplify quickstart mono
This commit is contained in:
parent
630dfa0887
commit
555c4ecd1a
@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
||||||
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
|
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/config"
|
"github.com/zeromicro/go-zero/tools/goctl/config"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
@ -85,7 +86,7 @@ func DoGenProject(apiFile, dir, style string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logx.Must(pathx.MkdirIfNotExist(dir))
|
logx.Must(pathx.MkdirIfNotExist(dir))
|
||||||
rootPkg, err := getParentPackage(dir)
|
rootPkg, err := golang.GetParentPackage(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/config"
|
"github.com/zeromicro/go-zero/tools/goctl/config"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
|
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/format"
|
"github.com/zeromicro/go-zero/tools/goctl/util/format"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
@ -42,7 +43,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
|
|||||||
handler = strings.Title(handler)
|
handler = strings.Title(handler)
|
||||||
logicName = pkgName
|
logicName = pkgName
|
||||||
}
|
}
|
||||||
parentPkg, err := getParentPackage(dir)
|
parentPkg, err := golang.GetParentPackage(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,14 @@ package gogen
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
goformat "go/format"
|
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/collection"
|
"github.com/zeromicro/go-zero/core/collection"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/api/util"
|
"github.com/zeromicro/go-zero/tools/goctl/api/util"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
|
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -54,38 +52,11 @@ func genFile(c fileGenConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
code := formatCode(buffer.String())
|
code := golang.FormatCode(buffer.String())
|
||||||
_, err = fp.WriteString(code)
|
_, err = fp.WriteString(code)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getParentPackage(dir string) (string, error) {
|
|
||||||
abs, err := filepath.Abs(dir)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
projectCtx, err := ctx.Prepare(abs)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// fix https://github.com/zeromicro/go-zero/issues/1058
|
|
||||||
wd := projectCtx.WorkDir
|
|
||||||
d := projectCtx.Dir
|
|
||||||
same, err := pathx.SameFile(wd, d)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
trim := strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir)
|
|
||||||
if same {
|
|
||||||
trim = strings.TrimPrefix(strings.ToLower(projectCtx.WorkDir), strings.ToLower(projectCtx.Dir))
|
|
||||||
}
|
|
||||||
|
|
||||||
return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
|
func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
|
||||||
util.WriteIndent(writer, indent)
|
util.WriteIndent(writer, indent)
|
||||||
var err error
|
var err error
|
||||||
@ -136,15 +107,6 @@ func getMiddleware(api *spec.ApiSpec) []string {
|
|||||||
return result.KeysStr()
|
return result.KeysStr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatCode(code string) string {
|
|
||||||
ret, err := goformat.Source([]byte(code))
|
|
||||||
if err != nil {
|
|
||||||
return code
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func responseGoTypeName(r spec.Route, pkg ...string) string {
|
func responseGoTypeName(r spec.Route, pkg ...string) string {
|
||||||
if r.ResponseType == nil {
|
if r.ResponseType == nil {
|
||||||
return ""
|
return ""
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package gogen
|
package gogen
|
||||||
|
|
||||||
const (
|
const (
|
||||||
interval = "internal/"
|
internal = "internal/"
|
||||||
typesPacket = "types"
|
typesPacket = "types"
|
||||||
configDir = interval + "config"
|
configDir = internal + "config"
|
||||||
contextDir = interval + "svc"
|
contextDir = internal + "svc"
|
||||||
handlerDir = interval + "handler"
|
handlerDir = internal + "handler"
|
||||||
logicDir = interval + "logic"
|
logicDir = internal + "logic"
|
||||||
middlewareDir = interval + "middleware"
|
middlewareDir = internal + "middleware"
|
||||||
typesDir = interval + typesPacket
|
typesDir = internal + typesPacket
|
||||||
groupProperty = "group"
|
groupProperty = "group"
|
||||||
)
|
)
|
||||||
|
12
tools/goctl/pkg/golang/format.go
Normal file
12
tools/goctl/pkg/golang/format.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package golang
|
||||||
|
|
||||||
|
import goformat "go/format"
|
||||||
|
|
||||||
|
func FormatCode(code string) string {
|
||||||
|
ret, err := goformat.Source([]byte(code))
|
||||||
|
if err != nil {
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(ret)
|
||||||
|
}
|
36
tools/goctl/pkg/golang/path.go
Normal file
36
tools/goctl/pkg/golang/path.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package golang
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetParentPackage(dir string) (string, error) {
|
||||||
|
abs, err := filepath.Abs(dir)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
projectCtx, err := ctx.Prepare(abs)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// fix https://github.com/zeromicro/go-zero/issues/1058
|
||||||
|
wd := projectCtx.WorkDir
|
||||||
|
d := projectCtx.Dir
|
||||||
|
same, err := pathx.SameFile(wd, d)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
trim := strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir)
|
||||||
|
if same {
|
||||||
|
trim = strings.TrimPrefix(strings.ToLower(projectCtx.WorkDir), strings.ToLower(projectCtx.Dir))
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil
|
||||||
|
}
|
@ -4,9 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"greet/api/internal/svc"
|
|
||||||
"greet/api/internal/types"{{if .callRPC}}
|
"{{.svcPkg}}"
|
||||||
"greet/rpc/greet"{{end}}
|
"{{.typesPkg}}"{{if .callRPC}}
|
||||||
|
"{{.rpcClientPkg}}"{{end}}
|
||||||
)
|
)
|
||||||
|
|
||||||
type PingLogic struct {
|
type PingLogic struct {
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
package svc
|
package svc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"{{.configPkg}}"{{if .callRPC}}
|
||||||
"github.com/zeromicro/go-zero/zrpc"
|
"github.com/zeromicro/go-zero/zrpc"
|
||||||
"greet/api/internal/config"
|
"{{.rpcClientPkg}}"{{end}}
|
||||||
"greet/rpc/greet"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceContext struct {
|
type ServiceContext struct {
|
||||||
Config config.Config
|
Config config.Config{{if .callRPC}}
|
||||||
GreetRpc greet.Greet
|
GreetRpc greet.Greet{{end}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServiceContext(c config.Config) *ServiceContext {
|
func NewServiceContext(c config.Config) *ServiceContext {
|
||||||
client := zrpc.MustNewClient(zrpc.RpcClientConf{
|
{{if .callRPC}}client := zrpc.MustNewClient(zrpc.RpcClientConf{
|
||||||
Target: "127.0.0.1:8080",
|
Target: "127.0.0.1:8080",
|
||||||
})
|
}){{end}}
|
||||||
return &ServiceContext{
|
return &ServiceContext{
|
||||||
Config: c,
|
Config: c,
|
||||||
GreetRpc: greet.NewGreet(client),
|
{{if .callRPC}}GreetRpc: greet.NewGreet(client),{{end}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,7 @@ var (
|
|||||||
protocContent string
|
protocContent string
|
||||||
//go:embed idl/rpc.yaml
|
//go:embed idl/rpc.yaml
|
||||||
rpcEtcContent string
|
rpcEtcContent string
|
||||||
|
zrpcWorkDir string
|
||||||
zRPCWorkDir string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type serviceImpl struct {
|
type serviceImpl struct {
|
||||||
@ -32,12 +31,12 @@ func (s serviceImpl) Start() {
|
|||||||
func (s serviceImpl) Stop() {}
|
func (s serviceImpl) Stop() {}
|
||||||
|
|
||||||
func initRPCProto() error {
|
func initRPCProto() error {
|
||||||
zRPCWorkDir = filepath.Join(projectDir, "rpc")
|
zrpcWorkDir = filepath.Join(projectDir, "rpc")
|
||||||
if err := pathx.MkdirIfNotExist(zRPCWorkDir); err != nil {
|
if err := pathx.MkdirIfNotExist(zrpcWorkDir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
protoFilename := filepath.Join(zRPCWorkDir, protoName)
|
protoFilename := filepath.Join(zrpcWorkDir, protoName)
|
||||||
rpcBytes := []byte(protocContent)
|
rpcBytes := []byte(protocContent)
|
||||||
return ioutil.WriteFile(protoFilename, rpcBytes, 0666)
|
return ioutil.WriteFile(protoFilename, rpcBytes, 0666)
|
||||||
}
|
}
|
||||||
@ -54,8 +53,8 @@ func (m micro) mustStartRPCProject() {
|
|||||||
logx.Must(initRPCProto())
|
logx.Must(initRPCProto())
|
||||||
log.Debug(">> Generating quickstart zRPC project...")
|
log.Debug(">> Generating quickstart zRPC project...")
|
||||||
arg := "goctl rpc protoc " + protoName + " --go_out=. --go-grpc_out=. --zrpc_out=. --verbose"
|
arg := "goctl rpc protoc " + protoName + " --go_out=. --go-grpc_out=. --zrpc_out=. --verbose"
|
||||||
execCommand(zRPCWorkDir, arg)
|
execCommand(zrpcWorkDir, arg)
|
||||||
etcFile := filepath.Join(zRPCWorkDir, "etc", "greet.yaml")
|
etcFile := filepath.Join(zrpcWorkDir, "etc", "greet.yaml")
|
||||||
logx.Must(ioutil.WriteFile(etcFile, []byte(rpcEtcContent), 0666))
|
logx.Must(ioutil.WriteFile(etcFile, []byte(rpcEtcContent), 0666))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +64,7 @@ func (m micro) start() {
|
|||||||
sg := service.NewServiceGroup()
|
sg := service.NewServiceGroup()
|
||||||
sg.Add(serviceImpl{func() {
|
sg.Add(serviceImpl{func() {
|
||||||
log.Debug(">> Ready to start a zRPC server...")
|
log.Debug(">> Ready to start a zRPC server...")
|
||||||
goStart(zRPCWorkDir)
|
goStart(zrpcWorkDir)
|
||||||
}})
|
}})
|
||||||
sg.Add(serviceImpl{func() {
|
sg.Add(serviceImpl{func() {
|
||||||
mono.start()
|
mono.start()
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
|
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
@ -22,9 +23,11 @@ var (
|
|||||||
apiEtcContent string
|
apiEtcContent string
|
||||||
|
|
||||||
apiWorkDir string
|
apiWorkDir string
|
||||||
|
rpcWorkDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
func initAPIFlags() error {
|
func initAPIFlags() error {
|
||||||
|
rpcWorkDir = filepath.Join(projectDir, "rpc")
|
||||||
apiWorkDir = filepath.Join(projectDir, "api")
|
apiWorkDir = filepath.Join(projectDir, "api")
|
||||||
if err := pathx.MkdirIfNotExist(apiWorkDir); err != nil {
|
if err := pathx.MkdirIfNotExist(apiWorkDir); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -58,15 +61,36 @@ func (m mono) createAPIProject() {
|
|||||||
etcFile := filepath.Join(apiWorkDir, "etc", "greet.yaml")
|
etcFile := filepath.Join(apiWorkDir, "etc", "greet.yaml")
|
||||||
logx.Must(ioutil.WriteFile(etcFile, []byte(apiEtcContent), 0666))
|
logx.Must(ioutil.WriteFile(etcFile, []byte(apiEtcContent), 0666))
|
||||||
logicFile := filepath.Join(apiWorkDir, "internal", "logic", "pinglogic.go")
|
logicFile := filepath.Join(apiWorkDir, "internal", "logic", "pinglogic.go")
|
||||||
|
svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go")
|
||||||
|
configPath := filepath.Join(apiWorkDir, "internal", "config")
|
||||||
|
svcPath := filepath.Join(apiWorkDir, "internal", "svc")
|
||||||
|
typesPath := filepath.Join(apiWorkDir, "internal", "types")
|
||||||
|
svcPkg, err := golang.GetParentPackage(svcPath)
|
||||||
|
logx.Must(err)
|
||||||
|
typesPkg, err := golang.GetParentPackage(typesPath)
|
||||||
|
logx.Must(err)
|
||||||
|
configPkg, err := golang.GetParentPackage(configPath)
|
||||||
|
logx.Must(err)
|
||||||
|
|
||||||
logx.Must(util.With("logic").Parse(apiLogicContent).SaveTo(map[string]bool{
|
var rpcClientPkg string
|
||||||
|
if m.callRPC {
|
||||||
|
rpcClientPath := filepath.Join(rpcWorkDir, "greet")
|
||||||
|
rpcClientPkg, err = golang.GetParentPackage(rpcClientPath)
|
||||||
|
logx.Must(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logx.Must(util.With("logic").Parse(apiLogicContent).SaveTo(map[string]interface{}{
|
||||||
|
"svcPkg": svcPkg,
|
||||||
|
"typesPkg": typesPkg,
|
||||||
|
"rpcClientPkg": rpcClientPkg,
|
||||||
"callRPC": m.callRPC,
|
"callRPC": m.callRPC,
|
||||||
}, logicFile, true))
|
}, logicFile, true))
|
||||||
|
|
||||||
if m.callRPC {
|
logx.Must(util.With("svc").Parse(svcContent).SaveTo(map[string]interface{}{
|
||||||
svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go")
|
"rpcClientPkg": rpcClientPkg,
|
||||||
logx.Must(ioutil.WriteFile(svcFile, []byte(svcContent), 0666))
|
"configPkg": configPkg,
|
||||||
}
|
"callRPC": m.callRPC,
|
||||||
|
}, svcFile, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m mono) start() {
|
func (m mono) start() {
|
||||||
|
@ -51,8 +51,8 @@ func PathFromGoSrc() (string, error) {
|
|||||||
return dir[len(parent)+1:], nil
|
return dir[len(parent)+1:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindGoModPath returns the path in project where has file go.mod, it maybe return empty string if
|
// FindGoModPath returns the path in project where has file go.mod,
|
||||||
// there is no go.mod file in project
|
// it returns empty string if there is no go.mod file in project.
|
||||||
func FindGoModPath(dir string) (string, bool) {
|
func FindGoModPath(dir string) (string, bool) {
|
||||||
absDir, err := filepath.Abs(dir)
|
absDir, err := filepath.Abs(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user