1
0
mirror of https://github.com/beego/bee.git synced 2025-07-08 03:00:19 +00:00

fix controllers parsing issue when go module is enabled.

This commit is contained in:
zav8
2019-07-12 22:51:38 +08:00
parent 6a86284cec
commit b080da776a
2 changed files with 131 additions and 21 deletions

View File

@ -438,3 +438,94 @@ func defaultGOPATH() string {
}
return ""
}
// IsModuleEnabled checks whether go module is enabled according to
// https://github.com/golang/go/wiki/Modules#when-do-i-get-old-behavior-vs-new-module-based-behavior
func IsModuleEnabled(gopaths []string, cwd string) bool {
gm := os.Getenv("GO111MODULE")
switch gm {
case "on":
return true
case "off":
return false
default:
for _, gopath := range gopaths {
if gopath == "" {
continue
}
if InDir(cwd, filepath.Join(gopath, "src")) != "" {
return false
}
}
return true
}
}
// InDir checks whether path is in the file tree rooted at dir.
// If so, InDir returns an equivalent path relative to dir.
// If not, InDir returns an empty string.
// InDir makes some effort to succeed even in the presence of symbolic links.
func InDir(path, dir string) string {
if rel := inDirLex(path, dir); rel != "" {
return rel
}
xpath, err := filepath.EvalSymlinks(path)
if err != nil || xpath == path {
xpath = ""
} else {
if rel := inDirLex(xpath, dir); rel != "" {
return rel
}
}
xdir, err := filepath.EvalSymlinks(dir)
if err == nil && xdir != dir {
if rel := inDirLex(path, xdir); rel != "" {
return rel
}
if xpath != "" {
if rel := inDirLex(xpath, xdir); rel != "" {
return rel
}
}
}
return ""
}
// inDirLex is like inDir but only checks the lexical form of the file names.
// It does not consider symbolic links.
// return the suffix. Most uses of str.HasFilePathPrefix should probably
// be calling InDir instead.
func inDirLex(path, dir string) string {
pv := strings.ToUpper(filepath.VolumeName(path))
dv := strings.ToUpper(filepath.VolumeName(dir))
path = path[len(pv):]
dir = dir[len(dv):]
switch {
default:
return ""
case pv != dv:
return ""
case len(path) == len(dir):
if path == dir {
return "."
}
return ""
case dir == "":
return path
case len(path) > len(dir):
if dir[len(dir)-1] == filepath.Separator {
if path[:len(dir)] == dir {
return path[len(dir):]
}
return ""
}
if path[len(dir)] == filepath.Separator && path[:len(dir)] == dir {
if len(path) == len(dir)+1 {
return "."
}
return path[len(dir)+1:]
}
return ""
}
}