mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
Support to read go.mod recursively to get module path
This commit is contained in:
parent
d30b5fb501
commit
4130b54579
@ -126,25 +126,33 @@ func GetGOPATH() string {
|
|||||||
|
|
||||||
// GetModuleName returns the value of module in `go.mod` file.
|
// GetModuleName returns the value of module in `go.mod` file.
|
||||||
func GetModuleName(pathToProjectRoot string) (string, error) {
|
func GetModuleName(pathToProjectRoot string) (string, error) {
|
||||||
gomodPath := filepath.Join(pathToProjectRoot, "go.mod")
|
|
||||||
gomodBytes, err := ioutil.ReadFile(gomodPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
gomodContent := string(gomodBytes)
|
|
||||||
moduleIdx := strings.Index(gomodContent, "module ")
|
|
||||||
newLineIdx := strings.Index(gomodContent, "\n")
|
|
||||||
|
|
||||||
var moduleName string
|
var moduleName string
|
||||||
if moduleIdx >= 0 {
|
loopPath := pathToProjectRoot
|
||||||
if newLineIdx >= 0 {
|
for {
|
||||||
moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx])
|
if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 {
|
||||||
moduleName = strings.TrimSuffix(moduleName, "\r")
|
gomodPath := filepath.Join(loopPath, "go.mod")
|
||||||
} else {
|
gomodBytes, err := ioutil.ReadFile(gomodPath)
|
||||||
moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:])
|
if err != nil {
|
||||||
|
loopPath = loopPath[:idx]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
gomodContent := string(gomodBytes)
|
||||||
|
moduleIdx := strings.Index(gomodContent, "module ")
|
||||||
|
newLineIdx := strings.Index(gomodContent, "\n")
|
||||||
|
|
||||||
|
if moduleIdx >= 0 {
|
||||||
|
if newLineIdx >= 0 {
|
||||||
|
moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx])
|
||||||
|
moduleName = strings.TrimSuffix(moduleName, "\r")
|
||||||
|
} else {
|
||||||
|
moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:])
|
||||||
|
}
|
||||||
|
return moduleName, nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("can not get module path in `%s`", gomodPath)
|
||||||
}
|
}
|
||||||
} else {
|
break
|
||||||
return "", fmt.Errorf("can not get module path in `%s`", gomodPath)
|
|
||||||
}
|
}
|
||||||
return moduleName, nil
|
return moduleName, fmt.Errorf("no `go.mod` file in every parent directory of `%s`", pathToProjectRoot)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user