Create a symbol link file named protoc-gen-goctl from goctl (#1076)

This commit is contained in:
anqiansong
2021-09-21 23:13:31 +08:00
committed by GitHub
parent 06e114e5a3
commit e8a340c1c0
10 changed files with 135 additions and 83 deletions

View File

@@ -163,12 +163,6 @@ OPTIONS:
### 注意事项
* `google.golang.org/grpc`需要降级到 `v1.29.1`且protoc-gen-go版本不能高于v1.3.2see [https://github.com/grpc/grpc-go/issues/3347](https://github.com/grpc/grpc-go/issues/3347))即
```shell script
replace google.golang.org/grpc => google.golang.org/grpc v1.29.1
```
* proto不支持暂多文件同时生成
* proto不支持外部依赖包引入message不支持inline
* 目前main文件、shared文件、handler文件会被强制覆盖而和开发人员手动需要编写的则不会覆盖生成这一类在代码头部均有
@@ -232,40 +226,9 @@ service Greet {
## 常见问题解决(go mod工程)
* 错误一:
```text
A required privilege is not held by the client.
```
这个问题只有goctl 版本在`goctl.exe version 1.2.1` 以后的 Windows操作系统出现主要原因是goctl需要以管理员身份运行这样才能将`goctl.exe` 创建一个 `ptocot-gen-gcotl`
的符号链接。
```golang
pb/xx.pb.go:220:7: undefined: grpc.ClientConnInterface
pb/xx.pb.go:224:11: undefined: grpc.SupportPackageIsVersion6
pb/xx.pb.go:234:5: undefined: grpc.ClientConnInterface
pb/xx.pb.go:237:24: undefined: grpc.ClientConnInterface
```
解决方法:请将`protoc-gen-go`版本降至v1.3.2及一下
* 错误二:
```golang
# go.etcd.io/etcd/clientv3/balancer/picker
../../../go/pkg/mod/go.etcd.io/etcd@v0.0.0-20200402134248-51bdeb39e698/clientv3/balancer/picker/err.go:25:9: cannot use &errPicker literal (type *errPicker) as type Picker in return argument:*errPicker does not implement Picker (wrong type for Pick method)
have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
want Pick(balancer.PickInfo) (balancer.PickResult, error)
../../../go/pkg/mod/go.etcd.io/etcd@v0.0.0-20200402134248-51bdeb39e698/clientv3/balancer/picker/roundrobin_balanced.go:33:9: cannot use &rrBalanced literal (type *rrBalanced) as type Picker in return argument:
*rrBalanced does not implement Picker (wrong type for Pick method)
have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
want Pick(balancer.PickInfo) (balancer.PickResult, error)
#github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c
../../../go/pkg/mod/github.com/tal-tech/go-zero@v1.0.12/zrpc/internal/balancer/p2c/p2c.go:41:32: not enough arguments in call to base.NewBalancerBuilder
have (string, *p2cPickerBuilder)
want (string, base.PickerBuilder, base.Config)
../../../go/pkg/mod/github.com/tal-tech/go-zero@v1.0.12/zrpc/internal/balancer/p2c/p2c.go:58:9: cannot use &p2cPicker literal (type *p2cPicker) as type balancer.Picker in return argument:
*p2cPicker does not implement balancer.Picker (wrong type for Pick method)
have Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error)
want Pick(balancer.PickInfo) (balancer.PickResult, error)
```
解决方法:
```golang
replace google.golang.org/grpc => google.golang.org/grpc v1.29.1
```

View File

@@ -4,22 +4,17 @@ import (
"errors"
"fmt"
"path/filepath"
"runtime"
"github.com/urfave/cli"
"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"
)
// RPC is to generate rpc service code from a proto file by specifying a proto file using flag src,
// 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")
@@ -47,22 +42,6 @@ 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 {

View File

@@ -1,9 +1,11 @@
package generator
import (
"os/exec"
"fmt"
"runtime"
"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
@@ -25,17 +27,24 @@ 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 {
_, err := exec.LookPath("go")
if err != nil {
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
}
_, err = exec.LookPath("protoc")
if err != nil {
if _, err := env.LookUpProtoc(); err != nil {
return err
}
_, err = exec.LookPath("protoc-gen-go")
_, err := env.LookUpProtocGenGoctl()
if err == nil {
return nil
}
g.log.Warning("%+v", err)
_, err = env.LookUpProtocGenGo()
return err
}

View File

@@ -10,6 +10,7 @@ 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`
@@ -17,6 +18,12 @@ 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)
@@ -50,10 +57,15 @@ 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(" --go_out=plugins=grpc:" + ctx.GetMain().Filename)
cw.WriteString(outFlag + "=plugins=grpc:" + ctx.GetMain().Filename)
} else {
cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
cw.WriteString(outFlag + "=plugins=grpc:" + dir.Filename)
}
// Compatible with version 1.4.0github.com/golang/protobuf/protoc-gen-go@v1.4.0
@@ -66,7 +78,6 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
}
optSet.AddStr(op)
cw.WriteString(" --go_opt=" + op)
}
var currentFileOpt string
@@ -82,13 +93,13 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
currentFileOpt = " --go_opt=M" + base + "=."
}
if !optSet.Contains(currentFileOpt) {
if !optSet.Contains(currentFileOpt) && !useGoctl {
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: