|
|
|
@ -12,20 +12,46 @@ import (
|
|
|
|
|
"github.com/tal-tech/go-zero/core/collection"
|
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
|
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/util"
|
|
|
|
|
goctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const goModeIdentifier = "go.mod"
|
|
|
|
|
|
|
|
|
|
func getParentPackage(dir string) (string, error) {
|
|
|
|
|
absDir, err := filepath.Abs(dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
|
parent := path.Join(gopath, "src")
|
|
|
|
|
pos := strings.Index(absDir, parent)
|
|
|
|
|
if pos < 0 {
|
|
|
|
|
return "", fmt.Errorf("%s not in GOPATH %s directory", absDir, gopath)
|
|
|
|
|
|
|
|
|
|
var rootPath string
|
|
|
|
|
var tempPath = absDir
|
|
|
|
|
var hasGoMod = false
|
|
|
|
|
for {
|
|
|
|
|
tempPath = filepath.Dir(tempPath)
|
|
|
|
|
if goctlutil.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
|
|
|
|
|
tempPath = filepath.Dir(tempPath)
|
|
|
|
|
rootPath = absDir[len(tempPath)+1:]
|
|
|
|
|
hasGoMod = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if tempPath == string(filepath.Separator) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !hasGoMod {
|
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
|
parent := path.Join(gopath, "src")
|
|
|
|
|
pos := strings.Index(absDir, parent)
|
|
|
|
|
if pos < 0 {
|
|
|
|
|
message := fmt.Sprintf("%s not in gomod project path, or not in GOPATH of %s directory", absDir, gopath)
|
|
|
|
|
println(message)
|
|
|
|
|
tempPath = filepath.Dir(absDir)
|
|
|
|
|
rootPath = absDir[len(tempPath)+1:]
|
|
|
|
|
} else {
|
|
|
|
|
rootPath = absDir[len(parent)+1:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return absDir[len(parent)+1:], nil
|
|
|
|
|
return rootPath, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeIndent(writer io.Writer, indent int) {
|
|
|
|
|