Skip to content

Commit

Permalink
Merge pull request #1 from ernesto27/grpc
Browse files Browse the repository at this point in the history
add grpc option
  • Loading branch information
ernesto27 authored Feb 11, 2024
2 parents 45b212e + a0e7047 commit 8809f26
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

dist/
protobuf
protobuf
data.txt
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/bufbuild/protocompile v0.6.0 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fullstorydev/grpcurl v1.8.9 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/jhump/protoreflect v1.15.3 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand All @@ -28,6 +31,7 @@ require (
github.com/yuin/goldmark v1.5.2 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/bufbuild/protocompile v0.6.0 h1:Uu7WiSQ6Yj9DbkdnOe7U4mNKp58y9WDMKDn28/ZlunY=
github.com/bufbuild/protocompile v0.6.0/go.mod h1:YNP35qEYoYGme7QMtz5SBCoN4kL4g12jTtjuzRNdjpE=
github.com/charmbracelet/glamour v0.6.0 h1:wi8fse3Y7nfcabbbDuwolqTqMQPMnVPeZhDM273bISc=
github.com/charmbracelet/glamour v0.6.0/go.mod h1:taqWV4swIMMbWALc0m7AfE9JkPSU8om2538k9ITBxOc=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
Expand All @@ -14,6 +16,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/fullstorydev/grpcurl v1.8.9 h1:JMvZXK8lHDGyLmTQ0ZdGDnVVGuwjbpaumf8p42z0d+c=
github.com/fullstorydev/grpcurl v1.8.9/go.mod h1:PNNKevV5VNAV2loscyLISrEnWQI61eqR0F8l3bVadAA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
Expand All @@ -24,6 +28,8 @@ github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down Expand Up @@ -59,6 +65,8 @@ github.com/yuin/goldmark-emoji v1.0.1/go.mod h1:2w1E6FEWLcDQkoTE+7HU6QF1F6SLlNGj
golang.org/x/net v0.0.0-20221002022538-bcab6841153b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
47 changes: 31 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import (
)

type Params struct {
typeP string
url string
query string
header string
message string
method string
file string
typeP string
url string
query string
header string
message string
method string
file string
importPath string
proto string
methodName string
verbose bool
}

func getProtocol(p Params) Protocol {
Expand All @@ -29,6 +33,8 @@ func getProtocol(p Params) Protocol {
return NewGrapQL(p)
case "http":
return NewHTTP(p)
case "grpc":
return NewGRPC(p)
}
return nil
}
Expand All @@ -49,7 +55,7 @@ var styleMessage = lipgloss.NewStyle().
Background(lipgloss.Color("#606683")).Padding(1).MarginBottom(1).MarginTop(1).MarginLeft(1)

func main() {
typeParam := flag.String("t", "http", "type connection (ws, gq, http)")
typeParam := flag.String("t", "http", "type connection (ws, gq, http, grpc)")
urlParam := flag.String("u", "", "url to connect")
messageParam := flag.String("p", "", "data send to server")
queryParam := flag.String("q", "", "query params")
Expand All @@ -58,14 +64,19 @@ func main() {
method := flag.String("m", "GET", "method request")
file := flag.String("f", "", "file path")

// GRPC exclusive flags
importPath := flag.String("import-path", "", "The path to a directory from which proto sources can be imported, for use with -proto flags")
proto := flag.String("proto", "", "The proto file")
methodName := flag.String("method", "", "The service method to call")

flag.Parse()

if *urlParam == "" {
flag.PrintDefaults()
return
}

validProtocos := []string{"ws", "gq", "http"}
validProtocos := []string{"ws", "gq", "http", "grpc"}

valid := false
for _, v := range validProtocos {
Expand All @@ -81,13 +92,17 @@ func main() {
}

params := Params{
typeP: *typeParam,
url: *urlParam,
query: *queryParam,
header: *headerP,
message: *messageParam,
method: *method,
file: *file,
typeP: *typeParam,
url: *urlParam,
query: *queryParam,
header: *headerP,
message: *messageParam,
method: *method,
file: *file,
importPath: *importPath,
proto: *proto,
methodName: *methodName,
verbose: *verboseParam,
}

p := getProtocol(params)
Expand Down
116 changes: 116 additions & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"

"encoding/json"
"fmt"
Expand All @@ -17,7 +18,12 @@ import (
"time"

"github.com/charmbracelet/lipgloss"
"github.com/fullstorydev/grpcurl"
"github.com/gorilla/websocket"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"
)

type Protocol interface {
Expand Down Expand Up @@ -317,6 +323,116 @@ func printHttpResponse(r *http.Response) {
fmt.Println()
}

type GRPC struct {
cc *grpc.ClientConn
url string
importPath string
proto string
message string
methodName string
}

func NewGRPC(params Params) *GRPC {
return &GRPC{
url: params.url,
importPath: params.importPath,
proto: params.proto,
message: params.message,
methodName: params.methodName,
}
}

func (g *GRPC) RequestResponse() (string, error) {
ctx := context.Background()
dial := func() (*grpc.ClientConn, error) {
dialTime := 10 * time.Second
ctx, cancel := context.WithTimeout(ctx, dialTime)
defer cancel()
var opts []grpc.DialOption

network := "tcp"
var creds credentials.TransportCredentials

grpcurlUA := "grpcurl/" + "dev build <no version set>"

opts = append(opts, grpc.WithUserAgent(grpcurlUA))
target := g.url
cc, err := grpcurl.BlockingDial(ctx, network, target, creds, opts...)
if err != nil {
return nil, err
}
return cc, nil
}

var descSource grpcurl.DescriptorSource
var fileSource grpcurl.DescriptorSource

importPaths := []string{g.importPath}
protoFiles := []string{g.proto}

var err error
fileSource, err = grpcurl.DescriptorSourceFromProtoFiles(importPaths, protoFiles...)
if err != nil {
return "", err
}

descSource = fileSource

if g.cc == nil {
g.cc, err = dial()
if err != nil {
return "", err
}
}

in := strings.NewReader(g.message)

options := grpcurl.FormatOptions{
EmitJSONDefaultFields: true,
IncludeTextSeparator: true,
AllowUnknownFields: true,
}
rf, formatter, err := grpcurl.RequestParserAndFormatter(grpcurl.Format("json"), descSource, in, options)
if err != nil {
panic(err)
}

var buf bytes.Buffer

h := &grpcurl.DefaultEventHandler{
Out: &buf,
Formatter: formatter,
VerbosityLevel: 1,
}

symbol := g.methodName
err = grpcurl.InvokeRPC(ctx, descSource, g.cc, symbol, append([]string{}, []string{}...), h, rf.Next)

if err != nil {
if errStatus, ok := status.FromError(err); ok && false {
h.Status = errStatus
} else {
return "", err
}
}

if h.Status.Code() != codes.OK {
grpcurl.PrintStatus(os.Stderr, h.Status, formatter)
}

return buf.String(), nil
}

func (g *GRPC) OnMessageReceived() {}
func (g *GRPC) PrintHeaderResponse() {}

func (g *GRPC) Close() {
if g.cc != nil {
g.cc.Close()
g.cc = nil
}
}

type HeaderP struct {
Key string
Value string
Expand Down

0 comments on commit 8809f26

Please sign in to comment.