reset link goctl (#1232)
This commit is contained in:
@@ -4,9 +4,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -14,6 +16,10 @@ import (
|
||||
// you can specify a target folder for code generation, when the proto file has import, you can specify
|
||||
// the import search directory through the proto_path command, for specific usage, please refer to protoc -h
|
||||
func RPC(c *cli.Context) error {
|
||||
if err := prepare(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
src := c.String("src")
|
||||
out := c.String("dir")
|
||||
style := c.String("style")
|
||||
@@ -41,6 +47,22 @@ func RPC(c *cli.Context) error {
|
||||
return g.Generate(src, out, protoImportPath, goOptions...)
|
||||
}
|
||||
|
||||
func prepare() error {
|
||||
if !env.CanExec() {
|
||||
return fmt.Errorf("%s: can not start new processes using os.StartProcess or exec.Command", runtime.GOOS)
|
||||
}
|
||||
if _, err := env.LookUpGo(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := env.LookUpProtoc(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := env.LookUpProtocGenGo(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RPCNew is to generate rpc greet service, this greet service can speed
|
||||
// up your understanding of the zrpc service structure
|
||||
func RPCNew(c *cli.Context) error {
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"os/exec"
|
||||
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/console"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
||||
)
|
||||
|
||||
// DefaultGenerator defines the environment needs of rpc service generation
|
||||
@@ -27,24 +25,17 @@ func NewDefaultGenerator() Generator {
|
||||
// Prepare provides environment detection generated by rpc service,
|
||||
// including go environment, protoc, whether protoc-gen-go is installed or not
|
||||
func (g *DefaultGenerator) Prepare() error {
|
||||
if !env.CanExec() {
|
||||
return fmt.Errorf("%s: can not start new processes using os.StartProcess or exec.Command", runtime.GOOS)
|
||||
}
|
||||
|
||||
if _, err := env.LookUpGo(); err != nil {
|
||||
_, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := env.LookUpProtoc(); err != nil {
|
||||
_, err = exec.LookPath("protoc")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := env.LookUpProtocGenGoctl()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
_, err = exec.LookPath("protoc-gen-go")
|
||||
|
||||
g.log.Warning("%+v", err)
|
||||
_, err = env.LookUpProtocGenGo()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||||
"github.com/tal-tech/go-zero/tools/goctl/util/env"
|
||||
)
|
||||
|
||||
const googleProtocGenGoErr = `--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC`
|
||||
@@ -18,12 +17,6 @@ const googleProtocGenGoErr = `--go_out: protoc-gen-go: plugins are not supported
|
||||
// GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
|
||||
// but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
|
||||
func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config, goOptions ...string) error {
|
||||
var useGoctl bool
|
||||
_, err := env.LookUpProtocGenGoctl()
|
||||
if err == nil {
|
||||
useGoctl = true
|
||||
}
|
||||
|
||||
dir := ctx.GetPb()
|
||||
cw := new(bytes.Buffer)
|
||||
directory, base := filepath.Split(proto.Src)
|
||||
@@ -57,15 +50,10 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
|
||||
}
|
||||
|
||||
cw.WriteString(" " + proto.Name)
|
||||
outFlag := " --go_out"
|
||||
if useGoctl {
|
||||
outFlag = " --goctl_out"
|
||||
}
|
||||
|
||||
if strings.Contains(proto.GoPackage, "/") {
|
||||
cw.WriteString(outFlag + "=plugins=grpc:" + ctx.GetMain().Filename)
|
||||
cw.WriteString(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
|
||||
} else {
|
||||
cw.WriteString(outFlag + "=plugins=grpc:" + dir.Filename)
|
||||
cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
|
||||
}
|
||||
|
||||
// Compatible with version 1.4.0,github.com/golang/protobuf/protoc-gen-go@v1.4.0
|
||||
@@ -78,6 +66,7 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
|
||||
}
|
||||
|
||||
optSet.AddStr(op)
|
||||
cw.WriteString(" --go_opt=" + op)
|
||||
}
|
||||
|
||||
var currentFileOpt string
|
||||
@@ -93,13 +82,13 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
|
||||
currentFileOpt = " --go_opt=M" + base + "=."
|
||||
}
|
||||
|
||||
if !optSet.Contains(currentFileOpt) && !useGoctl {
|
||||
if !optSet.Contains(currentFileOpt) {
|
||||
cw.WriteString(currentFileOpt)
|
||||
}
|
||||
|
||||
command := cw.String()
|
||||
g.log.Debug(command)
|
||||
_, err = execx.Run(command, "")
|
||||
_, err := execx.Run(command, "")
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), googleProtocGenGoErr) {
|
||||
return errors.New(`unsupported plugin protoc-gen-go which installed from the following source:
|
||||
|
||||
Reference in New Issue
Block a user