Home

Awesome

English | 中文

trpc-cmdline

Go Reference Go Report Card LICENSE Releases Tests Coverage

trpc-cmdline is the command line tool for trpc-cpp and trpc-go.

It supports the latest three major releases of Go.

Installation

Install trpc-cmdline

Install using go command

First, add the following into your ~/.gitconfig:

[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

Then run the following to install trpc-cmdline:

go install trpc.group/trpc-go/trpc-cmdline/trpc@latest
<!-- #### Install from release <details><summary>Click to show the bash script</summary><br><pre> $ TAG="v0.0.1" # Choose tag. $ OS=linux # Choose from "linux", "darwin" or "windows". $ wget -O trpc https://github.com/trpc-group/trpc-cmdline/releases/download/${TAG}/trpc_${OS} $ mkdir -p ~/go/bin && chmod +x trpc && mv trpc ~/go/bin $ export PATH=~/go/bin:$PATH # Add this to your `~/.bashrc`. </pre></details> -->

Dependencies

Use one of the following methods to download:

Using trpc setup

After installation of trpc-cmdline, simply running trpc setup will automatically install all the dependencies.

Install separately

<details><summary>Install protoc </summary><br><pre> $ # Reference: https://grpc.io/docs/protoc-installation/ $ PB_REL="https://github.com/protocolbuffers/protobuf/releases" $ curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip $ unzip -o protoc-3.15.8-linux-x86_64.zip -d $HOME/.local $ export PATH=~/.local/bin:$PATH # Add this to your `~/.bashrc`. $ protoc --version libprotoc 3.15.8 </pre></details> <details><summary>Install flatc </summary><br><pre> $ # Reference: https://github.com/google/flatbuffers/releases $ wget https://github.com/google/flatbuffers/releases/download/v23.5.26/Linux.flatc.binary.g++-10.zip $ unzip -o Linux.flatc.binary.g++-10.zip -d $HOME/.bin $ export PATH=~/.bin:$PATH # Add this to your `~/.bashrc`. $ flatc --version flatc version 23.5.26 </pre></details> <details><summary>Install protoc-gen-go</summary><br><pre> $ # Reference: https://grpc.io/docs/languages/go/quickstart/ $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest </pre></details> <details><summary>Install goimports</summary><br><pre> $ go install golang.org/x/tools/cmd/goimports@latest </pre></details> <details><summary>Install mockgen</summary><br><pre> $ # Reference: https://github.com/uber-go/mock $ go install go.uber.org/mock/mockgen@latest </pre></details> <details><summary>Install protoc-gen-validate and protoc-gen-validate-go</summary><br><pre> $ # Please download the binaries in https://github.com/bufbuild/protoc-gen-validate/releases $ # Or: $ go install github.com/envoyproxy/protoc-gen-validate@latest $ go install github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-go@latest </pre></details>

Quick Start

Generation of Full Project

syntax = "proto3";
package helloworld;

option go_package = "github.com/some-repo/examples/helloworld";

// HelloRequest is hello request.
message HelloRequest {
  string msg = 1;
}

// HelloResponse is hello response.
message HelloResponse {
  string msg = 1;
}

// HelloWorldService handles hello request and echo message.
service HelloWorldService {
  // Hello says hello.
  rpc Hello(HelloRequest) returns(HelloResponse);
}
$ trpc create -p helloworld.proto -o out

Note: -p specifies proto file, -o specifies the output directory, for more information please run trpc -h and trpc create -h

$ cd out
$ go run .
...
... trpc service:helloworld.HelloWorldService launch success, tcp:127.0.0.1:8000, serving ...
...
$ go run cmd/client/main.go 
... simple  rpc   receive: 

Note: Since the implementation of server service is an empty operation and the client sends empty data, therefore the log shows that the simple rpc receives an empty string.

$ tree
.
|-- cmd
|   `-- client
|       `-- main.go  # Generated client code.
|-- go.mod
|-- go.sum
|-- hello_world_service.go  # Generated server service implementation.
|-- hello_world_service_test.go
|-- main.go  # Server entrypoint.
|-- stub  # Stub code.
|   `-- github.com
|       `-- some-repo
|           `-- examples
|               `-- helloworld
|                   |-- go.mod
|                   |-- helloworld.pb.go
|                   |-- helloworld.proto
|                   |-- helloworld.trpc.go
|                   `-- helloworld_mock.go
`-- trpc_go.yaml  # Configuration file for trpc-go.

Generation of RPC Stub

$ trpc create -p helloworld.proto -o out --rpconly
$ tree out
out
|-- go.mod
|-- go.sum
|-- helloworld.pb.go
|-- helloworld.trpc.go
`-- helloworld_mock.go

Frequently Used Flags

The following lists some frequently used flags.

Note: The proto import paths for options like alias/gotag/validate/swagger usually vary:

For detailed usage, please refer to /docs/examples/example-2/README.zh_CN.md

For additional flags please run trpc -h and trpc [subcmd] -h.

Functionalities

Please check Documentation

Contributing

This project is open-source and accepts contributions. See the contribution guide for more information.