Browse Source

Fix code generation (#1897)

anqiansong 3 years ago
parent
commit
f486685e99
2 changed files with 15 additions and 1 deletions
  1. 1 1
      tools/goctl/model/sql/gen/gen.go
  2. 14 0
      tools/goctl/util/ctx/gomod.go

+ 1 - 1
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

+ 14 - 0
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