From f486685e99570366b0c9176d347af673545e6917 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Fri, 13 May 2022 00:16:17 +0800 Subject: [PATCH] Fix code generation (#1897) --- tools/goctl/model/sql/gen/gen.go | 2 +- tools/goctl/util/ctx/gomod.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/goctl/model/sql/gen/gen.go b/tools/goctl/model/sql/gen/gen.go index 29194225..ee55c0d7 100644 --- a/tools/goctl/model/sql/gen/gen.go +++ b/tools/goctl/model/sql/gen/gen.go @@ -65,7 +65,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul } dir = dirAbs - pkg := filepath.Base(dirAbs) + pkg := util.SafeString(filepath.Base(dirAbs)) err = pathx.MkdirIfNotExist(dir) if err != nil { return nil, err diff --git a/tools/goctl/util/ctx/gomod.go b/tools/goctl/util/ctx/gomod.go index 4c526b09..a5d67ba6 100644 --- a/tools/goctl/util/ctx/gomod.go +++ b/tools/goctl/util/ctx/gomod.go @@ -13,6 +13,10 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) +const goModuleWithoutGoFiles = "command-line-arguments" + +var errInvalidGoMod = errors.New("invalid go module") + // Module contains the relative data of go module, // which is the result of the command go list type Module struct { @@ -23,6 +27,13 @@ type Module struct { GoVersion string } +func (m *Module) validate() error { + if m.Path == goModuleWithoutGoFiles || m.Dir == "" { + return errInvalidGoMod + } + return nil +} + // projectFromGoMod is used to find the go module and project file path // the workDir flag specifies which folder we need to detect based on // only valid for go mod project @@ -43,6 +54,9 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) { if err != nil { return nil, err } + if err := m.validate(); err != nil { + return nil, err + } var ret ProjectContext ret.WorkDir = workDir