Skip to content

Commit 0c87dbd

Browse files
committed
complete the CLI for managing mocks
1 parent 2e55636 commit 0c87dbd

File tree

4 files changed

+178
-81
lines changed

4 files changed

+178
-81
lines changed

cmd/fauxrpc/cmd_stub.go

+90-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
package main
22

33
import (
4+
"cmp"
45
"context"
56
"crypto/tls"
7+
"encoding/json"
68
"fmt"
9+
"log/slog"
710
"net"
811
"net/http"
912
"slices"
13+
"strings"
1014

1115
"connectrpc.com/connect"
1216
"github.com/brianvoe/gofakeit/v7"
1317
stubsv1 "github.com/sudorandom/fauxrpc/private/proto/gen/stubs/v1"
1418
"github.com/sudorandom/fauxrpc/private/proto/gen/stubs/v1/stubsv1connect"
1519
"golang.org/x/net/http2"
16-
"google.golang.org/protobuf/encoding/protojson"
1720
)
1821

1922
type StubCmd struct {
20-
Add StubAddCmd `cmd:"" help:"Adds a new stub response by method or type"`
21-
List StubListCmd `cmd:"" help:"List all registered mocks"`
23+
Add StubAddCmd `cmd:"" help:"Adds a new stub response by method or type"`
24+
List StubListCmd `cmd:"" help:"List all registered stubs"`
25+
Get StubGetCmd `cmd:"" help:"Get a registered stub"`
26+
Remove StubRemoveCmd `cmd:"" help:"Remove a registered stub"`
27+
RemoveAll StubRemoveAllCmd `cmd:"" help:"Remove all stubs"`
2228
}
2329

2430
type StubAddCmd struct {
@@ -32,7 +38,7 @@ func (c *StubAddCmd) Run(globals *Globals) error {
3238
client := newStubClient(c.Addr)
3339
stubs := []*stubsv1.Stub{}
3440
if c.ID == "" {
35-
c.ID = gofakeit.LetterN(5)
41+
c.ID = gofakeit.AdjectiveDescriptive() + "-" + strings.ReplaceAll(gofakeit.Animal(), " ", "-") + gofakeit.DigitN(3)
3642
}
3743
stubs = append(stubs, &stubsv1.Stub{
3844
Ref: &stubsv1.StubRef{
@@ -42,8 +48,12 @@ func (c *StubAddCmd) Run(globals *Globals) error {
4248
Content: &stubsv1.Stub_Json{Json: c.JSON},
4349
},
4450
)
45-
_, err := client.AddStubs(context.Background(), connect.NewRequest(&stubsv1.AddStubsRequest{Stubs: stubs}))
46-
return err
51+
resp, err := client.AddStubs(context.Background(), connect.NewRequest(&stubsv1.AddStubsRequest{Stubs: stubs}))
52+
if err != nil {
53+
return err
54+
}
55+
outputStubs(resp.Msg.GetStubs())
56+
return nil
4757
}
4858

4959
type StubListCmd struct {
@@ -64,31 +74,98 @@ func (c *StubListCmd) Run(globals *Globals) error {
6474
}
6575

6676
for name, ids := range groupedStubs {
67-
fmt.Printf("%s (%d)\n", name, len(ids))
77+
fmt.Printf("target=%s\n", name)
6878
slices.Sort(ids)
79+
6980
for _, id := range ids {
70-
fmt.Printf(" - %s\n", id)
81+
fmt.Printf(" id=%s\n", id)
7182
}
7283
}
7384
return err
7485
}
7586

7687
type StubGetCmd struct {
77-
Addr string `short:"a" help:"Address to bind to." default:"http://127.0.0.1:6660"`
88+
Addr string `short:"a" help:"Address to bind to." default:"http://127.0.0.1:6660"`
89+
Target string `arg:"" help:"Protobuf method or type" example:"'connectrpc.eliza.v1/Say', 'connectrpc.eliza.v1.IntroduceResponse'"`
90+
ID string `arg:"" help:"ID to give this particular mock response, will be a random string if one isn't given" example:"bad-response"`
7891
}
7992

8093
func (c *StubGetCmd) Run(globals *Globals) error {
8194
client := newStubClient(c.Addr)
82-
resp, err := client.ListStubs(context.Background(), connect.NewRequest(&stubsv1.ListStubsRequest{}))
95+
resp, err := client.ListStubs(context.Background(), connect.NewRequest(&stubsv1.ListStubsRequest{
96+
StubRef: &stubsv1.StubRef{
97+
Id: c.ID,
98+
Target: c.Target,
99+
},
100+
}))
83101
if err != nil {
84102
return err
85103
}
86-
jsonBody, err := protojson.MarshalOptions{Indent: " "}.Marshal(resp.Msg)
104+
outputStubs(resp.Msg.GetStubs())
105+
return nil
106+
}
107+
108+
type StubRemoveCmd struct {
109+
Addr string `short:"a" help:"Address to bind to." default:"http://127.0.0.1:6660"`
110+
Target string `arg:"" help:"Protobuf method or type" example:"'connectrpc.eliza.v1/Say', 'connectrpc.eliza.v1.IntroduceResponse'"`
111+
ID string `arg:"" help:"ID to give this particular mock response, will be a random string if one isn't given" example:"bad-response"`
112+
}
113+
114+
func (c *StubRemoveCmd) Run(globals *Globals) error {
115+
client := newStubClient(c.Addr)
116+
_, err := client.RemoveStubs(context.Background(), connect.NewRequest(&stubsv1.RemoveStubsRequest{
117+
StubRefs: []*stubsv1.StubRef{
118+
{
119+
Id: c.ID,
120+
Target: c.Target,
121+
},
122+
},
123+
}))
87124
if err != nil {
88125
return err
89126
}
90-
fmt.Println(string(jsonBody))
91-
return err
127+
return nil
128+
}
129+
130+
type StubRemoveAllCmd struct {
131+
Addr string `short:"a" help:"Address to bind to." default:"http://127.0.0.1:6660"`
132+
}
133+
134+
func (c *StubRemoveAllCmd) Run(globals *Globals) error {
135+
client := newStubClient(c.Addr)
136+
_, err := client.RemoveAllStubs(context.Background(), connect.NewRequest(&stubsv1.RemoveAllStubsRequest{}))
137+
if err != nil {
138+
return err
139+
}
140+
return nil
141+
}
142+
143+
type StubForOutput struct {
144+
Ref *stubsv1.StubRef `json:"ref,omitempty"`
145+
Content any `json:"content,omitempty"`
146+
}
147+
148+
func outputStubs(stubs []*stubsv1.Stub) {
149+
slices.SortFunc(stubs, func(a *stubsv1.Stub, b *stubsv1.Stub) int {
150+
return cmp.Compare(a.GetRef().GetId(), b.GetRef().GetId())
151+
})
152+
for _, stub := range stubs {
153+
var v any
154+
if err := json.Unmarshal([]byte(stub.GetJson()), &v); err != nil {
155+
slog.Error("error marshalling for output", slog.Any("error", err))
156+
continue
157+
}
158+
outputStub := StubForOutput{
159+
Ref: stub.Ref,
160+
Content: v,
161+
}
162+
b, err := json.MarshalIndent(outputStub, "", " ")
163+
if err != nil {
164+
slog.Error("error marshalling for output", slog.Any("error", err))
165+
continue
166+
}
167+
fmt.Println(string(b))
168+
}
92169
}
93170

94171
func newStubClient(addr string) stubsv1connect.StubsServiceClient {

private/proto/gen/stubs/v1/stubs_service.pb.go

+77-65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

private/proto/stubs/v1/stubs_service.proto

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ message AddStubsRequest {
1515
repeated stubs.v1.Stub stubs = 1;
1616
}
1717

18-
message AddStubsResponse {}
18+
message AddStubsResponse {
19+
repeated stubs.v1.Stub stubs = 1;
20+
}
1921

2022
message RemoveStubsRequest {
2123
repeated StubRef stub_refs = 1;

0 commit comments

Comments
 (0)