Skip to content

`gorpc` cmdline for quickly generate project based on rpc framework `go-rpc`

License

Notifications You must be signed in to change notification settings

hitzhangjie/gorpc-cli

Repository files navigation

gorpc-cli

gorpc-cli is an efficient tool to help developers :

  • gorpc create, generate service template or rpc stub
  • gorpc update, update service template or rpc stub
  • gorpc bug, create a bug/issue at issue page
  • gorpc version, show the version
  • gorpc rpc, launch rpc test to your work

gorpc is built upon cobra, it is easy to extend new abilities.

Using "Google Protobuf" as IDL

Google Protobuf is developed by Google, it's a self-descriptive message format.

Using protobuf as IDL (Interface Descriptor Language) is very common, Google also provides a protobuf compiler called protoc.

Before, I wrote an article to introduce how protoc and proto-gen-go coordinate to work, and some internals of protocol buffer. If you're interested, please read my it: Protoc及其插件工作原理分析(精华版)

How does protoc and protoc plugins coordinate to work ?

We usually execute command like protoc --cpp_out/--java_out to generate cpp header/source, java files.

While for many other languages, protoc doesn't implement it, such as, go programming language. If we want to generate *.pb.go like *.pb.h/*.pb.cc or *.pb.java, we should implement a plugin for go, we already have a tool, that is protoc-gen-go.

Take protoc and protoc-gen-go as an example, let's see how they coordinate to work.

Just now, we know protobuf is a self-descriptive message format, when file *.proto parsed by protoc, a FileDesciptorProto object will be built, it contains nearly everything about the *.proto we written. If you know little about internals of protoc or protobuf itself, please refer to my article metioned above.

when run command protoc --go_out *.proto, protoc will read your protofile and parse it, after that, it build a FileDescriptorProto object, then it will serialize it and search executable named protoc-gen-go in your PATH shell env variable. If found, it will fork a childprocess to run protoc-gen-go, and parentprocess protoc will create a pipe btw itself and childprocess to communicate. protoc will send a CodeGenerateRequest to the childprocess protoc-gen-go via pipe. This CodeGenerateRequest contains serialized FileDescriptorProto, then protoc-gen-go read from pipe and extract it. protoc-gen-go will be responsible for generate source code by g.P(".."). This generated source code info will be responded to protoc, protoc process will create file and write file content (source code).

This is the way protoc and protoc-gen-go works.

Why we choose go templates to generate code ?

Usually, we can use protoc-gen-go as an starting point, we can add some files to generate besiding *.pb.go, for example, some default configuration files, or other go code.

But, writing a new protoc plugin like protoc-gen-go is really not a good idea for generating source code, especially you want to generate language for many more languages, or you want to add some flags, etc.

This manner, writing a new protoc plugin, increases the difficulty in maintenance and extensibility. If you have written some before, you'll know what I am saying.

  • use g.P(), g.In(), g.Out() to generate or format code;

Though we could use go tempalates instead of this, like hitzhangjie/protoc-gen-gorpc, passing flags is another big problem, which seriously limit the funtionality, and make it hard to use.

We could parse the *.proto file once, then using template technology to generate files. If we want to support new project template, we just change or add project template, needless to change the code. And, we could add command, subcommand, flags easily by cobra to extend the tool's functionalities.

How to use gorpc ?

run gorpc or gorpc help to show the help message, you can run gorpc help create to see more details relevant to gorpc create.

gorpc is an efficient too to speedup development.

for example:
- quickly generate project or rpcstub, based on pb
- send rpc request to test the target server
- update template to the newest version
- quickly open issue page to report an issue
- more ...

Usage:
  gorpc [command]

Available Commands:
  create      quickly create project or rpcstub, based on pb
  help        Help about any command
  issue       report an issue
  update      quickly update gorpc template or rpcstub, based on pb
  version     show gorpc version (commit hash)

Flags:
  -h, --help   help for gorpc

Contribution

Welcome the contribution from you !

Thanks

jetbrains.svg

Special thanks to the Jetbrains Team for their support.

About

`gorpc` cmdline for quickly generate project based on rpc framework `go-rpc`

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published