@ -4,41 +4,21 @@ import (
"fmt"
"fmt"
goformat "go/format"
goformat "go/format"
"io"
"io"
"os"
"path"
"path/filepath"
"strings"
"strings"
"github.com/tal-tech/go-zero/core/collection"
"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/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
goctlutil "github.com/tal-tech/go-zero/tools/goctl/util "
"github.com/tal-tech/go-zero/tools/goctl/util /project "
)
)
func getParentPackage ( dir string ) ( string , error ) {
func getParentPackage ( dir string ) ( string , error ) {
absDir, err := filepath . Abs ( dir )
p, err := project . Prepare ( dir , false )
if err != nil {
if err != nil {
return "" , err
return "" , err
}
}
absDir = strings . ReplaceAll ( absDir , ` \ ` , ` / ` )
return p . GoMod . Module , nil
rootPath , hasGoMod := goctlutil . FindGoModPath ( dir )
if hasGoMod {
return rootPath , nil
}
gopath := os . Getenv ( "GOPATH" )
parent := path . Join ( gopath , "src" )
pos := strings . Index ( absDir , parent )
if pos < 0 {
fmt . Printf ( "%s not in go.mod project path, or not in GOPATH of %s directory\n" , absDir , gopath )
tempPath := filepath . Dir ( absDir )
rootPath = absDir [ len ( tempPath ) + 1 : ]
} else {
rootPath = absDir [ len ( parent ) + 1 : ]
}
return rootPath , nil
}
}
func writeIndent ( writer io . Writer , indent int ) {
func writeIndent ( writer io . Writer , indent int ) {