fix: quickstart wrong package when go.mod exists in parent dir (#2048)

* chore: fix typo

* fix: quickstart in dir with go.mod

* fix: runner failed

* chore: refine code

* chore: simplify quickstart mono
This commit is contained in:
Kevin Wan
2022-06-26 22:37:15 +08:00
committed by GitHub
parent 630dfa0887
commit 555c4ecd1a
11 changed files with 111 additions and 75 deletions

View File

@@ -0,0 +1,12 @@
package golang
import goformat "go/format"
func FormatCode(code string) string {
ret, err := goformat.Source([]byte(code))
if err != nil {
return code
}
return string(ret)
}

View File

@@ -0,0 +1,36 @@
package golang
import (
"path/filepath"
"strings"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
func GetParentPackage(dir string) (string, error) {
abs, err := filepath.Abs(dir)
if err != nil {
return "", err
}
projectCtx, err := ctx.Prepare(abs)
if err != nil {
return "", err
}
// fix https://github.com/zeromicro/go-zero/issues/1058
wd := projectCtx.WorkDir
d := projectCtx.Dir
same, err := pathx.SameFile(wd, d)
if err != nil {
return "", err
}
trim := strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir)
if same {
trim = strings.TrimPrefix(strings.ToLower(projectCtx.WorkDir), strings.ToLower(projectCtx.Dir))
}
return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil
}