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 monomaster
parent
630dfa0887
commit
555c4ecd1a
@ -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"
|
||||||
)
|
)
|
||||||
|
@ -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
|
||||||
|
}
|
@ -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}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue