Fix code generation (#1897)

master
anqiansong 3 years ago committed by GitHub
parent 3ae874d75d
commit f486685e99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -65,7 +65,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
} }
dir = dirAbs dir = dirAbs
pkg := filepath.Base(dirAbs) pkg := util.SafeString(filepath.Base(dirAbs))
err = pathx.MkdirIfNotExist(dir) err = pathx.MkdirIfNotExist(dir)
if err != nil { if err != nil {
return nil, err return nil, err

@ -13,6 +13,10 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util/pathx" "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, // Module contains the relative data of go module,
// which is the result of the command go list // which is the result of the command go list
type Module struct { type Module struct {
@ -23,6 +27,13 @@ type Module struct {
GoVersion string 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 // projectFromGoMod is used to find the go module and project file path
// the workDir flag specifies which folder we need to detect based on // the workDir flag specifies which folder we need to detect based on
// only valid for go mod project // only valid for go mod project
@ -43,6 +54,9 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := m.validate(); err != nil {
return nil, err
}
var ret ProjectContext var ret ProjectContext
ret.WorkDir = workDir ret.WorkDir = workDir

Loading…
Cancel
Save