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
master
Kevin Wan 2 years ago committed by GitHub
parent 630dfa0887
commit 555c4ecd1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,6 +18,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
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/pkg/golang"
"github.com/zeromicro/go-zero/tools/goctl/util"
"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))
rootPkg, err := getParentPackage(dir)
rootPkg, err := golang.GetParentPackage(dir)
if err != nil {
return err
}

@ -9,6 +9,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"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/format"
"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)
logicName = pkgName
}
parentPkg, err := getParentPackage(dir)
parentPkg, err := golang.GetParentPackage(dir)
if err != nil {
return err
}

@ -3,16 +3,14 @@ package gogen
import (
"bytes"
"fmt"
goformat "go/format"
"io"
"path/filepath"
"strings"
"text/template"
"github.com/zeromicro/go-zero/core/collection"
"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/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@ -54,38 +52,11 @@ func genFile(c fileGenConfig) error {
return err
}
code := formatCode(buffer.String())
code := golang.FormatCode(buffer.String())
_, err = fp.WriteString(code)
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 {
util.WriteIndent(writer, indent)
var err error
@ -136,15 +107,6 @@ func getMiddleware(api *spec.ApiSpec) []string {
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 {
if r.ResponseType == nil {
return ""

@ -1,13 +1,13 @@
package gogen
const (
interval = "internal/"
internal = "internal/"
typesPacket = "types"
configDir = interval + "config"
contextDir = interval + "svc"
handlerDir = interval + "handler"
logicDir = interval + "logic"
middlewareDir = interval + "middleware"
typesDir = interval + typesPacket
configDir = internal + "config"
contextDir = internal + "svc"
handlerDir = internal + "handler"
logicDir = internal + "logic"
middlewareDir = internal + "middleware"
typesDir = internal + typesPacket
groupProperty = "group"
)

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

@ -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"
"github.com/zeromicro/go-zero/core/logx"
"greet/api/internal/svc"
"greet/api/internal/types"{{if .callRPC}}
"greet/rpc/greet"{{end}}
"{{.svcPkg}}"
"{{.typesPkg}}"{{if .callRPC}}
"{{.rpcClientPkg}}"{{end}}
)
type PingLogic struct {

@ -1,22 +1,22 @@
package svc
import (
"{{.configPkg}}"{{if .callRPC}}
"github.com/zeromicro/go-zero/zrpc"
"greet/api/internal/config"
"greet/rpc/greet"
"{{.rpcClientPkg}}"{{end}}
)
type ServiceContext struct {
Config config.Config
GreetRpc greet.Greet
Config config.Config{{if .callRPC}}
GreetRpc greet.Greet{{end}}
}
func NewServiceContext(c config.Config) *ServiceContext {
client := zrpc.MustNewClient(zrpc.RpcClientConf{
{{if .callRPC}}client := zrpc.MustNewClient(zrpc.RpcClientConf{
Target: "127.0.0.1:8080",
})
}){{end}}
return &ServiceContext{
Config: c,
GreetRpc: greet.NewGreet(client),
{{if .callRPC}}GreetRpc: greet.NewGreet(client),{{end}}
}
}

@ -17,8 +17,7 @@ var (
protocContent string
//go:embed idl/rpc.yaml
rpcEtcContent string
zRPCWorkDir string
zrpcWorkDir string
)
type serviceImpl struct {
@ -32,12 +31,12 @@ func (s serviceImpl) Start() {
func (s serviceImpl) Stop() {}
func initRPCProto() error {
zRPCWorkDir = filepath.Join(projectDir, "rpc")
if err := pathx.MkdirIfNotExist(zRPCWorkDir); err != nil {
zrpcWorkDir = filepath.Join(projectDir, "rpc")
if err := pathx.MkdirIfNotExist(zrpcWorkDir); err != nil {
return err
}
protoFilename := filepath.Join(zRPCWorkDir, protoName)
protoFilename := filepath.Join(zrpcWorkDir, protoName)
rpcBytes := []byte(protocContent)
return ioutil.WriteFile(protoFilename, rpcBytes, 0666)
}
@ -54,8 +53,8 @@ func (m micro) mustStartRPCProject() {
logx.Must(initRPCProto())
log.Debug(">> Generating quickstart zRPC project...")
arg := "goctl rpc protoc " + protoName + " --go_out=. --go-grpc_out=. --zrpc_out=. --verbose"
execCommand(zRPCWorkDir, arg)
etcFile := filepath.Join(zRPCWorkDir, "etc", "greet.yaml")
execCommand(zrpcWorkDir, arg)
etcFile := filepath.Join(zrpcWorkDir, "etc", "greet.yaml")
logx.Must(ioutil.WriteFile(etcFile, []byte(rpcEtcContent), 0666))
}
@ -65,7 +64,7 @@ func (m micro) start() {
sg := service.NewServiceGroup()
sg.Add(serviceImpl{func() {
log.Debug(">> Ready to start a zRPC server...")
goStart(zRPCWorkDir)
goStart(zrpcWorkDir)
}})
sg.Add(serviceImpl{func() {
mono.start()

@ -7,6 +7,7 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"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/pathx"
)
@ -22,9 +23,11 @@ var (
apiEtcContent string
apiWorkDir string
rpcWorkDir string
)
func initAPIFlags() error {
rpcWorkDir = filepath.Join(projectDir, "rpc")
apiWorkDir = filepath.Join(projectDir, "api")
if err := pathx.MkdirIfNotExist(apiWorkDir); err != nil {
return err
@ -58,15 +61,36 @@ func (m mono) createAPIProject() {
etcFile := filepath.Join(apiWorkDir, "etc", "greet.yaml")
logx.Must(ioutil.WriteFile(etcFile, []byte(apiEtcContent), 0666))
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)
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]bool{
logx.Must(util.With("logic").Parse(apiLogicContent).SaveTo(map[string]interface{}{
"svcPkg": svcPkg,
"typesPkg": typesPkg,
"rpcClientPkg": rpcClientPkg,
"callRPC": m.callRPC,
}, logicFile, true))
if m.callRPC {
svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go")
logx.Must(ioutil.WriteFile(svcFile, []byte(svcContent), 0666))
}
logx.Must(util.With("svc").Parse(svcContent).SaveTo(map[string]interface{}{
"rpcClientPkg": rpcClientPkg,
"configPkg": configPkg,
"callRPC": m.callRPC,
}, svcFile, true))
}
func (m mono) start() {

@ -51,8 +51,8 @@ func PathFromGoSrc() (string, error) {
return dir[len(parent)+1:], nil
}
// FindGoModPath returns the path in project where has file go.mod, it maybe return empty string if
// there is no go.mod file in project
// FindGoModPath returns the path in project where has file go.mod,
// it returns empty string if there is no go.mod file in project.
func FindGoModPath(dir string) (string, bool) {
absDir, err := filepath.Abs(dir)
if err != nil {

Loading…
Cancel
Save