grpcget

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2018 License: MIT Imports: 17 Imported by: 0

README

grpcget

GoDoc

grpcget is a command-line client for gRPC servers with reflection enabled.

With this tool you can query the server for services and symbols, and invoke methods with parameters.

The parameters for a method invocation are simple name=value parameters, and the name can have "." to set values in inner messages.

The default implementation output is aimed to be user-friendly, not JSON or marchine parseable. But using output customizers it is easy to create a version that does output other formats.

example

List all services:

# grpcget -plaintext list localhost:50051
grpc.reflection.v1alpha.ServerReflection
helloworld.Greeter

List service methods:

# grpcget -plaintext list localhost:50051 helloworld.Greeter 
    SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply)

Describe symbol:

# grpcget -plaintext describe localhost:50051 helloworld.Greeter 
Service: helloworld.Greeter
    SayHello(HelloRequest) returns (HelloReply)
# grpcget -plaintext describe localhost:50051 helloworld.HelloRequest 
Message: helloworld.HelloRequest
        name: TYPE_STRING
# grpcget -plaintext describe localhost:50051 helloworld.Greeter.SayHello 
Service RPC: helloworld.Greeter.SayHello
        SayHello(HelloRequest) returns (HelloReply)
                Request: helloworld.HelloRequest
                        name: TYPE_STRING
                Response: helloworld.HelloRequest
                        message: TYPE_STRING

Invoke:

# grpcget -plaintext invoke localhost:50051 helloworld.Greeter.SayHello name="Han Solo"
message: Hello Han Solo
# grpcget -plaintext invoke -describe localhost:50051 helloworld.Greeter.SayHello name="Rangel"
Service RPC: helloworld.Greeter.SayHello
        SayHello(HelloRequest) returns (HelloReply)
                Request: helloworld.HelloRequest
                        name: TYPE_STRING
                Response: helloworld.HelloRequest
                        message: TYPE_STRING
Invoke parameters

Given this protobuf message:

message SMData {
    string data = 1;
    string data2 = 2;
    map<string, SMData> data_list = 3;
    repeated SMData data_repeat = 4;
}

To set the various fields, these are sample invoke parameters:

data="Value" 
data_list.item1.data="Value inside the map with key 'item'" 
data_list.item1.data2="Value inside the same map as the previous param" 
data_repeat.0.data="Value inside the repeat with index '0'"
data_repeat.0.data2="Value into the same index '0' as the previous"
data_repeat.1.data2="Value inside the repeat with index '1'"

Notes:

  • For repeated items, the index must be set in sequential order, starting with 0.
  • Subsequent uses of the same map/repeated index sets the value on the existing item.
library

grpcget is also a customizable library that you can use in your projects.

It supports customizing setters and getters so you can define special handling for types that your application supports. See the "dmh" directory to learn more.

For example, give an UUID value type:

message UUID {
    string value = 1;
}

To invoke a parameter with this type you would need to call:

grpcget -plaintext invoke localhost:11300 app.MyService id.value="6708164e-2a56-4312-a66c-8f4de3b7b261"

You can create a field getter that allows you to omit the ".value" part:

grpcget -plaintext invoke localhost:11300 app.MyService id="6708164e-2a56-4312-a66c-8f4de3b7b261"

Set "DynMsgHelper" for details.

TODO
  • Customizable input/output (JSON, XML)
  • Support servers without reflection (read the .proto files directly)
  • Stream support
acknowledgement

This library is heavily based on grpccurl, and the packages it uses.

author

Rangel Reale (rangelspam@gmail.com)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MetadataFromHeaders

func MetadataFromHeaders(headers []string) metadata.MD

MetadataFromHeaders converts a list of header strings (each string in "Header-Name=Header-Value" form) into metadata. If a string has a header name without a value (e.g. does not contain a colon), the value is assumed to be blank. Binary headers (those whose names end in "-bin") should be base64-encoded. But if they cannot be base64-decoded, they will be assumed to be in raw form and used as is.

func ParseArgumentParam

func ParseArgumentParam(argument string) (name string, value string, err error)

Parse a name=value argument into separate variables

Types

type ConnectionConnectionSupplier

type ConnectionConnectionSupplier struct {
	Conn *grpc.ClientConn
}

ConnectionSupplier - Connection

func NewConnectionConnectionSupplier

func NewConnectionConnectionSupplier(conn *grpc.ClientConn) *ConnectionConnectionSupplier

func (*ConnectionConnectionSupplier) GetConnection

type ConnectionSupplier

type ConnectionSupplier interface {
	GetConnection(ctx context.Context) (*grpc.ClientConn, error)
}

Interface to supply a connection to GrpcGet

type DMHOption

type DMHOption func(*dmhOptions)

func WithDMHFieldValueGetters

func WithDMHFieldValueGetters(getters ...DynMsgHelperFieldValueGetter) DMHOption

func WithDMHFieldValueParsers

func WithDMHFieldValueParsers(setters ...DynMsgHelperFieldValueParser) DMHOption

type DefaultConnectionSupplier

type DefaultConnectionSupplier struct {
	Ctx    context.Context
	Target string
	Opts   []grpc.DialOption
}

ConnectionSupplier - Default

func NewDefaultConnectionSupplier

func NewDefaultConnectionSupplier(ctx context.Context, target string, opts ...grpc.DialOption) *DefaultConnectionSupplier

func (*DefaultConnectionSupplier) GetConnection

func (d *DefaultConnectionSupplier) GetConnection(ctx context.Context) (*grpc.ClientConn, error)

type DefaultDescribeOutput

type DefaultDescribeOutput struct {
	Out io.Writer
}

DescribeOutput

func NewDefaultDescribeOutput

func NewDefaultDescribeOutput(out io.Writer) *DefaultDescribeOutput

func (*DefaultDescribeOutput) DumpEnum

func (d *DefaultDescribeOutput) DumpEnum(level int, enum *desc.EnumDescriptor) error

func (*DefaultDescribeOutput) DumpField

func (d *DefaultDescribeOutput) DumpField(level int, fld *desc.FieldDescriptor) error

func (*DefaultDescribeOutput) DumpMessage

func (d *DefaultDescribeOutput) DumpMessage(level int, msg *desc.MessageDescriptor) error

func (*DefaultDescribeOutput) DumpMethod

func (d *DefaultDescribeOutput) DumpMethod(level int, mtd *desc.MethodDescriptor, complete bool) error

func (*DefaultDescribeOutput) DumpService

func (d *DefaultDescribeOutput) DumpService(level int, svc *desc.ServiceDescriptor) error

func (*DefaultDescribeOutput) OutputDescribe

func (d *DefaultDescribeOutput) OutputDescribe(descriptor desc.Descriptor) error

type DefaultInvokeOutput

type DefaultInvokeOutput struct {
	Out io.Writer
}

InvokeOutput

func NewDefaultInvokeOutput

func NewDefaultInvokeOutput(out io.Writer) *DefaultInvokeOutput

func (*DefaultInvokeOutput) DumpMessage

func (d *DefaultInvokeOutput) DumpMessage(dmh *DynMsgHelper, level int, msg *dynamic.Message) error

func (*DefaultInvokeOutput) OutputInvoke

func (d *DefaultInvokeOutput) OutputInvoke(dmh *DynMsgHelper, value proto.Message) error

type DefaultServiceListOutput

type DefaultServiceListOutput struct {
	Out io.Writer
}

ServiceListOutput

func NewDefaultServiceListOutput

func NewDefaultServiceListOutput(out io.Writer) *DefaultServiceListOutput

func (*DefaultServiceListOutput) OutputServiceList

func (d *DefaultServiceListOutput) OutputServiceList(services []string) error

type DefaultServiceOutput

type DefaultServiceOutput struct {
	Out io.Writer
}

ServiceOutput

func NewDefaultServiceOutput

func NewDefaultServiceOutput(out io.Writer) *DefaultServiceOutput

func (*DefaultServiceOutput) OutputService

func (d *DefaultServiceOutput) OutputService(service *desc.ServiceDescriptor) error

type DescribeOutput

type DescribeOutput interface {
	OutputDescribe(descriptor desc.Descriptor) error
}

Interface that outputs descrption of a symbol

type DynMsgHelper

type DynMsgHelper struct {
	// contains filtered or unexported fields
}

DynMsgHelper is a helper for setting and getting values from a *dynamic.Message struct It supports getter and setter customizations to support custom data types.

func NewDynMsgHelper

func NewDynMsgHelper(opts ...DMHOption) *DynMsgHelper

Creates a new DynMsgHelper

func (*DynMsgHelper) GetFieldValue

func (h *DynMsgHelper) GetFieldValue(msg *dynamic.Message, fld *desc.FieldDescriptor) (ok bool, value string, err error)

Gets the value of a field using a DynMsgHelperFieldValueGetter

func (*DynMsgHelper) MustParseScalarFieldValue

func (h *DynMsgHelper) MustParseScalarFieldValue(fld *desc.FieldDescriptor, value string) (retval interface{}, err error)

func (*DynMsgHelper) ParseFieldParamValue

func (h *DynMsgHelper) ParseFieldParamValue(fld *desc.FieldDescriptor, value string) (interface{}, error)

Sets the value of a field on the message. It supports DynMsgHelperFieldSetter for types that are not scalar.

func (*DynMsgHelper) ParseScalarFieldValue

func (h *DynMsgHelper) ParseScalarFieldValue(fld *desc.FieldDescriptor, value string) (supported bool, retval interface{}, err error)

func (*DynMsgHelper) SetParamValue

func (h *DynMsgHelper) SetParamValue(msg *dynamic.Message, name, value string) error

Sets a parameter value into the message. The name can have "." to set values inside another messages, like address.street_name.

type DynMsgHelperFieldValueGetter

type DynMsgHelperFieldValueGetter interface {
	GetFieldValue(msg *dynamic.Message, fld *desc.FieldDescriptor) (ok bool, value string, err error)
}

Getter

type DynMsgHelperFieldValueParser

type DynMsgHelperFieldValueParser interface {
	ParseFieldValue(fld *desc.FieldDescriptor, value string) (ok bool, retval interface{}, err error)
}

Setter

type GetOption

type GetOption func(*getOptions)

Get options for GrpcGet

func WithConnection

func WithConnection(conn *grpc.ClientConn) GetOption

func WithConnectionSupplier

func WithConnectionSupplier(supplier ConnectionSupplier) GetOption

func WithDMHOpts

func WithDMHOpts(opts ...DMHOption) GetOption

func WithDefaultConnection

func WithDefaultConnection(ctx context.Context, target string, opts ...grpc.DialOption) GetOption

func WithDefaultOutputs

func WithDefaultOutputs(w io.Writer) GetOption

func WithOutputDescribe

func WithOutputDescribe(output DescribeOutput) GetOption

func WithOutputInvoke

func WithOutputInvoke(output InvokeOutput) GetOption

func WithOutputService

func WithOutputService(output ServiceOutput) GetOption

func WithOutputServiceList

func WithOutputServiceList(output ServiceListOutput) GetOption

type GrpcGet

type GrpcGet struct {
	// contains filtered or unexported fields
}

GrpcGet is the client that talks to the gRPC server

func NewGrpcGet

func NewGrpcGet(opts ...GetOption) *GrpcGet

Creates a new GrpcGet

func NewGrpcGet_Default

func NewGrpcGet_Default(opts ...GetOption) *GrpcGet

Creates a new GrpcGet without default outputs

func (*GrpcGet) Describe

func (g *GrpcGet) Describe(ctx context.Context, symbol string) error

Get a symbol and call DescribeOutput.OutputDescribe

func (*GrpcGet) Invoke

func (g *GrpcGet) Invoke(ctx context.Context, method string, opts ...InvokeOption) error

Invoke the method and call InvokeOutput.OutputInvoke

func (*GrpcGet) ListService

func (g *GrpcGet) ListService(ctx context.Context, service string) error

List a single service and call ServiceOutput.OutputService

func (*GrpcGet) ListServices

func (g *GrpcGet) ListServices(ctx context.Context) error

List services and call ServiceListOutput.OutputServiceList

func (*GrpcGet) SetOpts

func (g *GrpcGet) SetOpts(opts ...GetOption) *GrpcGet

Set options the same was as the "New" functions

type InvokeOption

type InvokeOption func(*invokeOptions)

Invoke option

func WithInvokeParams

func WithInvokeParams(params ...string) InvokeOption

type InvokeOutput

type InvokeOutput interface {
	OutputInvoke(dmh *DynMsgHelper, value proto.Message) error
}

Interface that outputs the response of an invoke

type InvokeParamSetter

type InvokeParamSetter interface {
	SetInvokeParam(dmh *DynMsgHelper, req *dynamic.Message) error
}

Setter for an invoke parameters

type ParameterInvokeParamSetter

type ParameterInvokeParamSetter struct {
	Params []string
}

ParameterInvokeParamSetter

Params are in the format name=value

func NewParameterInvokeParamSetter

func NewParameterInvokeParamSetter(params ...string) *ParameterInvokeParamSetter

func (*ParameterInvokeParamSetter) SetInvokeParam

func (i *ParameterInvokeParamSetter) SetInvokeParam(dmh *DynMsgHelper, req *dynamic.Message) error

type ServiceListOutput

type ServiceListOutput interface {
	OutputServiceList(services []string) error
}

Interface that outputs a service list

type ServiceOutput

type ServiceOutput interface {
	OutputService(service *desc.ServiceDescriptor) error
}

Interface that outputs a single service and its methods

Directories

Path Synopsis
cmd
dmh

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL