• This repository has been archived on 11/Feb/2023
  • Stars
    star
    189
  • Rank 204,649 (Top 5 %)
  • Language
    Go
  • License
    Other
  • Created about 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cobra command line tool generator for gRPC clients

â›” DEPRECATED

This repository is no longer actively maintained. All future work has moved here.

protoc-gen-cobra

Cobra command line tool generator for Go gRPC.

GoDoc

What's this?

A plugin for the protobuf compiler protoc, that generates Go code using cobra. It is capable of generating client code for command line tools consistent with your protobuf description.

This:

service Bank {
	rpc Deposit(DepositRequest) returns (DepositReply)
}

message DepositRequest {
	string account = 1;
	double amount = 2;
}

message DepositReply {
	string account = 1;
	double balance = 2;
}

produces a client that can do:

echo '{"account":"foobar","amount":10}' | command bank deposit

It generates one cobra.Command per gRPC service (e.g. bank). The service's rpc methods are sub-commands, and share the same command line semantics. They take a request file for input, or stdin, and prints the response to the terminal, in the specified format. The client currently supports basic connectivity settings such as tls on/off, tls client authentication and so on.

$ ./example bank deposit -h
Deposit client

You can use environment variables with the same name of the command flags.
All caps and s/-/_, e.g. SERVER_ADDR.

Usage:
  example bank deposit [flags]

Examples:

Save a sample request to a file (or refer to your protobuf descriptor to create one):
	deposit -p > req.json

Submit request using file:
	deposit -f req.json

Authenticate using the Authorization header (requires transport security):
	export AUTH_TOKEN=your_access_token
	export SERVER_ADDR=api.example.com:443
	echo '{json}' | deposit --tls

Flags:
      --auth-token string          authorization token
      --auth-token-type string     authorization token type (default "Bearer")
      --jwt-key string             jwt key
      --jwt-key-file string        jwt key file
  -p, --print-sample-request       print sample request file and exit
  -f, --request-file string        client request file (must be json, yaml, or xml); use "-" for stdin + json
  -o, --response-format string     response format (json, prettyjson, yaml, or xml) (default "json")
  -s, --server-addr string         server address in form of host:port (default "localhost:8080")
      --timeout duration           client connection timeout (default 10s)
      --tls                        enable tls
      --tls-ca-cert-file string    ca certificate file
      --tls-cert-file string       client certificate file
      --tls-insecure-skip-verify   INSECURE: skip tls checks
      --tls-key-file string        client key file
      --tls-server-name string     tls server name override

This is an experiment. Was bored of writing the same boilerplate code to interact with gRPC servers, wanted something like kubectl. At some point I might want to generate server code too, similar to what go-swagger does. Perhaps look at using go-openapi too. Tests are lacking.

Streams

gRPC client and server streams are supported, you can do pipes from the command line. On server streams, each response is printed out using the specified response format. Client streams input must be formatted as json, one document per line, from a file or stdin.

Example client stream:

$ cat req.json
{"key":"hello","value":"world"}
{"key":"foo","value":"bar"}

$ ./example cache multiset -f req.json

Example client and server streams:

$ echo -ne '{"key":"hello"}\n{"key":"foo"}\n' | ./example cache multiget
{"value":"world"}
{"value":"bar"}

Idle server streams hang until the server closes the stream, or a timeout occurs.

More Repositories

1

freegeoip

IP geolocation web server
Go
4,931
star
2

wsdl2go

Command line tool to generate Go code from WSDL for SOAP XML services
Go
449
star
3

go-diameter

Diameter stack and Base Protocol (RFC 6733) for the Go programming language
Go
244
star
4

go-smpp

SMPP 3.4 Protocol for the Go programming language
Go
218
star
5

go-daemon

Utility to "daemonize" Go programs
C
174
star
6

go-eventsocket

FreeSWITCH Event Socket library for the Go programming language.
Go
125
star
7

cat-o-licious

Cat game written in Go
Go
104
star
8

eventsocket

Twisted protocol for the FreeSWITCH's Event Socket
Python
54
star
9

go-web

Utilities for http servers written in Go
Go
52
star
10

sms-api-server

HTTP API to send SMS via SMPP
Go
47
star
11

go-redis

Redis client library for Go
Go
45
star
12

txtraceroute

An asynchronous, pure python, traceroute with geolocation information
Python
44
star
13

twisted-twitter-stream

Twitter Stream API for Twisted
Python
43
star
14

jsonschema2go

Code generator for JSON schema
Go
38
star
15

ffmpeg-arm

Docker image with ffmpeg for ARM (Raspberry Pi)
37
star
16

nitgen-bsp

Python extension for Nitgen fingerprint recognition devices
C
22
star
17

mustash

recognize people's face and put a mustache on it
Python
15
star
18

defacer

meow
Go
12
star
19

go-swagger-ui

Go HTTP handler for serving swagger-ui
Go
10
star
20

niosted

Experimental reactor on top of Java NIO
Python
8
star
21

gocp

Go Concurrency Primitives
Go
7
star
22

dotfiles

my set of configuration files
Shell
5
star
23

go-talks

A collection of my Go talks
Go
5
star
24

txdbapi

Experimental ORM for Twisted based on Twistar
Python
4
star
25

pyqrcode

QRcode encoder and decoder for python
Java
4
star
26

go-listener

TCP listener on roids
Go
3
star
27

crosstool-ng-arm

Docker image with crosstool-ng for ARM (Raspberry Pi)
Shell
3
star
28

talkinator

a talking akinator
Python
3
star
29

pyascii85

Ascii85 encoder and decoder for python
C
3
star
30

linvpn

Secure socket layer for pppd (legacy)
C
2
star
31

b3-geoip-plugin

IP Geolocation plugin for the Big Brother Bot
Python
2
star
32

fiorix.github.com

HTML
2
star
33

pbserver

pbcopy and pbpaste over the web
Python
1
star