go-zero/tools/goctl/util/ctx/gomod.go
Keson e76f44a35b
reactor rpc (#179)
* reactor rpc generation

* update flag

* update command

* update command

* update unit test

* delete test file

* optimize code

* update doc

* update gen pb

* rename target dir

* update mysql data type convert rule

* add done flag

* optimize req/reply parameter

* optimize req/reply parameter

* remove waste code

* remove duplicate parameter

* format code

* format code

* optimize naming

* reactor rpcv2 to rpc

* remove new line

* format code

* rename underline to snake

* reactor getParentPackage

* remove debug log

* reactor background
2020-11-05 14:12:47 +08:00

48 lines
981 B
Go

package ctx
import (
"errors"
"os"
"path/filepath"
"github.com/tal-tech/go-zero/core/jsonx"
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
)
type Module struct {
Path string
Main bool
Dir string
GoMod string
GoVersion string
}
// projectFromGoMod is used to find the go module and project file path
// the workDir flag specifies which folder we need to detect based on
// only valid for go mod project
func projectFromGoMod(workDir string) (*ProjectContext, error) {
if len(workDir) == 0 {
return nil, errors.New("the work directory is not found")
}
if _, err := os.Stat(workDir); err != nil {
return nil, err
}
data, err := execx.Run("go list -json -m", workDir)
if err != nil {
return nil, err
}
var m Module
err = jsonx.Unmarshal([]byte(data), &m)
if err != nil {
return nil, err
}
var ret ProjectContext
ret.WorkDir = workDir
ret.Name = filepath.Base(m.Dir)
ret.Dir = m.Dir
ret.Path = m.Path
return &ret, nil
}