refactor gomod logic (#47)

* rebase upstream

* rebase

* trim no need line

* trim no need line

* trim no need line

* refactor gomod module logic

Co-authored-by: kingxt <dream4kingxt@163.com>
This commit is contained in:
kingxt
2020-09-07 18:04:59 +08:00
committed by GitHub
parent c5cd0d32d1
commit 1602f6ce81
3 changed files with 26 additions and 43 deletions

View File

@@ -4,41 +4,21 @@ import (
"fmt"
goformat "go/format"
"io"
"os"
"path"
"path/filepath"
"strings"
"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"
"github.com/tal-tech/go-zero/tools/goctl/util/project"
)
func getParentPackage(dir string) (string, error) {
absDir, err := filepath.Abs(dir)
p, err := project.Prepare(dir, false)
if err != nil {
return "", err
}
absDir = strings.ReplaceAll(absDir, `\`, `/`)
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
return p.GoMod.Module, nil
}
func writeIndent(writer io.Writer, indent int) {