diff --git a/go.mod b/go.mod index fe974c21..26a06621 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,6 @@ require ( github.com/urfave/cli v1.22.5 github.com/zeromicro/antlr v0.0.1 github.com/zeromicro/ddl-parser v0.0.0-20210712021150-63520aca7348 - github.com/zeromicro/protobuf v0.0.0-20210921042113-636cd51f0c35 go.etcd.io/etcd/api/v3 v3.5.1 go.etcd.io/etcd/client/v3 v3.5.1 go.opentelemetry.io/otel v1.1.0 diff --git a/go.sum b/go.sum index fb681b02..517dc8e9 100644 --- a/go.sum +++ b/go.sum @@ -405,8 +405,6 @@ github.com/zeromicro/antlr v0.0.1 h1:CQpIn/dc0pUjgGQ81y98s/NGOm2Hfru2NNio2I9mQgk github.com/zeromicro/antlr v0.0.1/go.mod h1:nfpjEwFR6Q4xGDJMcZnCL9tEfQRgszMwu3rDz2Z+p5M= github.com/zeromicro/ddl-parser v0.0.0-20210712021150-63520aca7348 h1:OhxL9tn28gDeJVzreIUiE5oVxZCjL3tBJ0XBNw8p5R8= github.com/zeromicro/ddl-parser v0.0.0-20210712021150-63520aca7348/go.mod h1:ISU/8NuPyEpl9pa17Py9TBPetMjtsiHrb9f5XGiYbo8= -github.com/zeromicro/protobuf v0.0.0-20210921042113-636cd51f0c35 h1:LKLiozf78evAX4+82AHw/rG7TvefD7TJII7XB7Oip7o= -github.com/zeromicro/protobuf v0.0.0-20210921042113-636cd51f0c35/go.mod h1:CJvyESptK/6uU5003fPJQ9Hmubl3vRuGqaLgzUU0R7Y= go.etcd.io/etcd/api/v3 v3.5.1 h1:v28cktvBq+7vGyJXF8G+rWJmj+1XUmMtqcLnH8hDocM= go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.1 h1:XIQcHCFSG53bJETYeRJtIxdLv2EWRGxcfzR8lSnTH4E= diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 9f528f35..85d8faca 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -3,7 +3,6 @@ package main import ( "fmt" "os" - "path/filepath" "runtime" "github.com/logrusorgru/aurora" @@ -29,16 +28,10 @@ import ( rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli" "github.com/tal-tech/go-zero/tools/goctl/tpl" "github.com/tal-tech/go-zero/tools/goctl/upgrade" - "github.com/tal-tech/go-zero/tools/goctl/util/console" - "github.com/tal-tech/go-zero/tools/goctl/util/env" "github.com/urfave/cli" - pluginCtl "github.com/zeromicro/protobuf/protoc-gen-go" ) -const ( - protocGenGoctl = "protoc-gen-goctl" - codeFailure = 1 -) +const codeFailure = 1 var commands = []cli.Command{ { @@ -650,46 +643,10 @@ var commands = []cli.Command{ }, } -func init() { - err := linkProtocGenGoctl() - if err != nil { - console.Error("%+v", err) - } -} - -func linkProtocGenGoctl() error { - path, err := env.LookPath("goctl") - if err != nil { - return err - } - - dir := filepath.Dir(path) - ext := filepath.Ext(path) - target := filepath.Join(dir, protocGenGoctl) - if len(ext) > 0 { - target = target + ext - } - _, err = os.Lstat(target) - if err != nil && !os.IsNotExist(err) { - return err - } - if os.IsNotExist(err) { - return os.Symlink(path, target) - } - return nil -} - func main() { logx.Disable() load.Disable() - args := os.Args - pluginName := filepath.Base(args[0]) - if pluginName == protocGenGoctl { - pluginCtl.Generate() - return - } - app := cli.NewApp() app.Usage = "a cli tool to generate code" app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH) diff --git a/tools/goctl/rpc/cli/cli.go b/tools/goctl/rpc/cli/cli.go index ad576b49..46ec562b 100644 --- a/tools/goctl/rpc/cli/cli.go +++ b/tools/goctl/rpc/cli/cli.go @@ -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 { diff --git a/tools/goctl/rpc/generator/defaultgenerator.go b/tools/goctl/rpc/generator/defaultgenerator.go index 68288d4d..61725161 100644 --- a/tools/goctl/rpc/generator/defaultgenerator.go +++ b/tools/goctl/rpc/generator/defaultgenerator.go @@ -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 } diff --git a/tools/goctl/rpc/generator/genpb.go b/tools/goctl/rpc/generator/genpb.go index f7c34354..189d74b2 100644 --- a/tools/goctl/rpc/generator/genpb.go +++ b/tools/goctl/rpc/generator/genpb.go @@ -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: diff --git a/tools/goctl/util/env/env.go b/tools/goctl/util/env/env.go index 6bcef662..9e9e0a77 100644 --- a/tools/goctl/util/env/env.go +++ b/tools/goctl/util/env/env.go @@ -11,11 +11,10 @@ import ( ) const ( - bin = "bin" - binGo = "go" - binProtoc = "protoc" - binProtocGenGo = "protoc-gen-go" - binProtocGenGoctl = "protoc-gen-goctl" + bin = "bin" + binGo = "go" + binProtoc = "protoc" + binProtocGenGo = "protoc-gen-go" ) // LookUpGo searches an executable go in the directories @@ -39,22 +38,14 @@ func LookUpProtoc() (string, error) { return LookPath(xProtoc) } -// Deprecated: LookUpProtocGenGo searches an executable protoc-gen-go in the directories -// named by the PATH environment variable, use LookUpProtocGenGoctl instead. +// LookUpProtocGenGo searches an executable protoc-gen-go in the directories +// named by the PATH environment variable. func LookUpProtocGenGo() (string, error) { suffix := getExeSuffix() xProtocGenGo := binProtocGenGo + suffix return LookPath(xProtocGenGo) } -// LookUpProtocGenGoctl searches an executable protoc-gen-go in the directories -// named by the PATH environment variable. -func LookUpProtocGenGoctl() (string, error) { - suffix := getExeSuffix() - xProtocGenGo := binProtocGenGoctl + suffix - return LookPath(xProtocGenGo) -} - // LookPath searches for an executable named file in the // directories named by the PATH environment variable, // for the os windows, the named file will be spliced with the