diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index fa1093ec18fc..6be110a106d5 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -8,6 +8,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/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" @@ -19,6 +20,7 @@ const defaultLogicPackage = "logic" var handlerTemplate string type handlerInfo struct { + PackageName string PkgName string ImportPackages string ImportHttpxPackage string @@ -40,8 +42,13 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route handler = strings.Title(handler) logicName = pkgName } + packageName, err := golang.GetPackageName(dir) + if err != nil { + return err + } return doGenToFile(dir, handler, cfg, group, route, handlerInfo{ + PackageName: packageName, PkgName: pkgName, ImportPackages: genHandlerImports(group, route, rootPkg), HandlerName: handler, diff --git a/tools/goctl/api/gogen/genlogic.go b/tools/goctl/api/gogen/genlogic.go index 287804956fa5..35dc8fcc858e 100644 --- a/tools/goctl/api/gogen/genlogic.go +++ b/tools/goctl/api/gogen/genlogic.go @@ -10,6 +10,7 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api" "github.com/zeromicro/go-zero/tools/goctl/api/spec" "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/format" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/vars" @@ -52,6 +53,10 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group, if len(route.RequestTypeName()) > 0 { requestString = "req *" + requestGoTypeName(route, typesPacket) } + packageName, err := golang.GetPackageName(dir) + if err != nil { + return err + } subDir := getLogicFolderPath(group, route) return genFile(fileGenConfig{ @@ -63,6 +68,7 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group, templateFile: logicTemplateFile, builtinTemplate: logicTemplate, data: map[string]string{ + "packageName": packageName, "pkgName": subDir[strings.LastIndex(subDir, "/")+1:], "imports": imports, "logic": strings.Title(logic), diff --git a/tools/goctl/pkg/golang/path.go b/tools/goctl/pkg/golang/path.go index 1f0a2bb1d019..2d50899f0d09 100644 --- a/tools/goctl/pkg/golang/path.go +++ b/tools/goctl/pkg/golang/path.go @@ -34,3 +34,17 @@ func GetParentPackage(dir string) (string, error) { return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil } + +func GetPackageName(dir string) (string, error) { + abs, err := filepath.Abs(dir) + if err != nil { + return "", err + } + + projectCtx, err := ctx.Prepare(abs) + if err != nil { + return "", err + } + + return projectCtx.Path, nil +}