From 4158ab284eb1aaf605d8bafa18946fef8c9bacec Mon Sep 17 00:00:00 2001 From: qiantao Date: Sat, 11 Jul 2020 13:02:25 +0800 Subject: [PATCH] use `go env` check project is `go mod` or not. --- generate/swaggergen/g_docs.go | 2 +- utils/utils.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index a3e71dc..73ba71b 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -435,7 +435,7 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) { pkgRealpath := "" - if os.Getenv(`GO111MODULE`) == `on` { + if bu.IsGOMODULE() { pkgRealpath = filepath.Join(bu.GetBeeWorkPath(), "..", pkgpath) } else { gopaths := bu.GetGOPATHs() diff --git a/utils/utils.go b/utils/utils.go index f53d7de..f4516e8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -452,3 +452,14 @@ func GetGoVersionSkipMinor() string { strArray := strings.Split(runtime.Version()[2:], `.`) return strArray[0] + `.` + strArray[1] } + +func IsGOMODULE() bool { + if combinedOutput, e := exec.Command(`go`, `env`).CombinedOutput(); e != nil { + beeLogger.Log.Errorf("i cann't find go.") + } else { + regex := regexp.MustCompile(`GOMOD="?(.+go.mod)"?`) + stringSubmatch := regex.FindStringSubmatch(string(combinedOutput)) + return len(stringSubmatch) == 2 + } + return false +}