From 4408767981ccc81012d21d98c4d6a68f2fff9abe Mon Sep 17 00:00:00 2001 From: kingxt Date: Tue, 8 Sep 2020 12:19:34 +0800 Subject: [PATCH] fix command run path bug (#52) * rebase upstream * rebase * trim no need line * trim no need line * trim no need line * optimized go path logic Co-authored-by: kingxt --- tools/goctl/api/gogen/util.go | 3 ++- tools/goctl/rpc/execx/execx.go | 6 ++++-- tools/goctl/rpc/gen/gencall.go | 2 +- tools/goctl/rpc/gen/genpb.go | 2 +- tools/goctl/util/project/project.go | 27 ++++++++++++++++----------- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index f1edd409..0e0e3ef5 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -19,6 +19,7 @@ func getParentPackage(dir string) (string, error) { if err != nil { return "", err } + if len(p.GoMod.Path) > 0 { goModePath := filepath.Clean(filepath.Dir(p.GoMod.Path)) absPath, err := filepath.Abs(dir) @@ -30,7 +31,7 @@ func getParentPackage(dir string) (string, error) { return parent, nil } - return p.GoMod.Module, nil + return p.Package, nil } func writeIndent(writer io.Writer, indent int) { diff --git a/tools/goctl/rpc/execx/execx.go b/tools/goctl/rpc/execx/execx.go index 14108f09..327c5a35 100644 --- a/tools/goctl/rpc/execx/execx.go +++ b/tools/goctl/rpc/execx/execx.go @@ -8,7 +8,7 @@ import ( "runtime" ) -func Run(arg string) (string, error) { +func Run(arg string, dir string) (string, error) { goos := runtime.GOOS var cmd *exec.Cmd switch goos { @@ -19,7 +19,9 @@ func Run(arg string) (string, error) { default: return "", fmt.Errorf("unexpected os: %v", goos) } - + if len(dir) > 0 { + cmd.Dir = dir + } dtsout := new(bytes.Buffer) stderr := new(bytes.Buffer) cmd.Stdout = dtsout diff --git a/tools/goctl/rpc/gen/gencall.go b/tools/goctl/rpc/gen/gencall.go index c92d133c..3e47be88 100644 --- a/tools/goctl/rpc/gen/gencall.go +++ b/tools/goctl/rpc/gen/gencall.go @@ -169,7 +169,7 @@ func (g *defaultRpcGenerator) genCall() error { // if mockgen is already installed, it will generate code of gomock for shared files _, err = exec.LookPath("mockgen") if mockGenInstalled { - execx.Run(fmt.Sprintf("go generate %s", filename)) + execx.Run(fmt.Sprintf("go generate %s", filename), "") } return nil diff --git a/tools/goctl/rpc/gen/genpb.go b/tools/goctl/rpc/gen/genpb.go index 872abb4b..67296804 100644 --- a/tools/goctl/rpc/gen/genpb.go +++ b/tools/goctl/rpc/gen/genpb.go @@ -74,7 +74,7 @@ func (g *defaultRpcGenerator) genPb() error { func (g *defaultRpcGenerator) protocGenGo(target string) error { src := filepath.Dir(g.Ctx.ProtoFileSrc) sh := fmt.Sprintf(`protoc -I=%s --go_out=plugins=grpc:%s %s`, src, target, g.Ctx.ProtoFileSrc) - stdout, err := execx.Run(sh) + stdout, err := execx.Run(sh, "") if err != nil { return err } diff --git a/tools/goctl/util/project/project.go b/tools/goctl/util/project/project.go index 8e6da6d0..ff1fcc8d 100644 --- a/tools/goctl/util/project/project.go +++ b/tools/goctl/util/project/project.go @@ -2,7 +2,6 @@ package project import ( "io/ioutil" - "os" "os/exec" "path/filepath" "regexp" @@ -21,14 +20,15 @@ const ( type ( Project struct { - Path string - Name string - GoMod GoMod + Path string // Project path name + Name string // Project name + Package string // The service related package + GoMod GoMod } GoMod struct { - Module string - Path string + Module string // The gomod module name + Path string // The gomod related path } ) @@ -54,15 +54,16 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) { goMod, module string goPath string name, path string + pkg string ) - ret, err := execx.Run(constGoMod) + ret, err := execx.Run(constGoMod, projectDir) if err != nil { return nil, err } goMod = strings.TrimSpace(ret) - ret, err = execx.Run(constGoPath) + ret, err = execx.Run(constGoPath, "") if err != nil { return nil, err } @@ -82,10 +83,11 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) { return nil, err } } else { - pwd, err := os.Getwd() + pwd, err := execx.Run("pwd", projectDir) if err != nil { return nil, err } + pwd = filepath.Clean(strings.TrimSpace(pwd)) if !strings.HasPrefix(pwd, src) { absPath, err := filepath.Abs(projectDir) @@ -95,6 +97,7 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) { name = filepath.Clean(filepath.Base(absPath)) path = projectDir + pkg = name } else { r := strings.TrimPrefix(pwd, src+string(filepath.Separator)) name = filepath.Dir(r) @@ -102,13 +105,15 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) { name = r } path = filepath.Join(src, name) + pkg = r } module = name } return &Project{ - Name: name, - Path: path, + Name: name, + Path: path, + Package: pkg, GoMod: GoMod{ Module: module, Path: goMod,