Skip to content

Commit 93ad4a1

Browse files
committed
Fixes recorders
1 parent 9598eda commit 93ad4a1

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

parrot/cmd/main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import (
1414

1515
func main() {
1616
var (
17-
port int
18-
debug bool
19-
trace bool
20-
silent bool
21-
json bool
17+
port int
18+
debug bool
19+
trace bool
20+
silent bool
21+
json bool
22+
recorders []string
2223
)
2324

2425
rootCmd := &cobra.Command{
@@ -49,6 +50,13 @@ func main() {
4950
return err
5051
}
5152

53+
for _, r := range recorders {
54+
err = p.Record(r)
55+
if err != nil {
56+
return err
57+
}
58+
}
59+
5260
c := make(chan os.Signal, 1)
5361
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
5462
<-c
@@ -65,6 +73,7 @@ func main() {
6573
rootCmd.Flags().BoolVarP(&trace, "trace", "t", false, "Enable trace and debug output")
6674
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Disable all output")
6775
rootCmd.Flags().BoolVarP(&json, "json", "j", false, "Output logs in JSON format")
76+
rootCmd.Flags().StringSliceVarP(&recorders, "recorders", "r", nil, "Existing recorders to use")
6877

6978
if err := rootCmd.Execute(); err != nil {
7079
log.Error().Err(err).Msg("error executing command")

parrot/errors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var (
1414
ErrResponseMarshal = errors.New("unable to marshal response body to JSON")
1515
ErrRouteNotFound = errors.New("route not found")
1616

17-
ErrNoRecorderURL = errors.New("no recorder URL specified")
18-
ErrNilRecorder = errors.New("recorder is nil")
19-
ErrRecorderNotFound = errors.New("recorder not found")
17+
ErrNoRecorderURL = errors.New("no recorder URL specified")
18+
ErrInvalidRecorderURL = errors.New("invalid recorder URL")
19+
ErrRecorderNotFound = errors.New("recorder not found")
2020
)
2121

2222
// Custom error type to help add more detail to base errors

parrot/parrot.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,20 +362,17 @@ func (p *Server) registerRouteHandler(w http.ResponseWriter, r *http.Request) {
362362
}
363363

364364
// Record registers a new recorder with the parrot. All incoming requests to the parrot will be sent to the recorder.
365-
func (p *Server) Record(recorder *Recorder) error {
365+
func (p *Server) Record(recorderURL string) error {
366366
p.recordersMu.Lock()
367367
defer p.recordersMu.Unlock()
368-
if recorder == nil {
369-
return ErrNilRecorder
370-
}
371-
if recorder.URL == "" {
368+
if recorderURL == "" {
372369
return ErrNoRecorderURL
373370
}
374-
_, err := url.Parse(recorder.URL)
371+
_, err := url.Parse(recorderURL)
375372
if err != nil {
376-
return fmt.Errorf("failed to parse recorder URL: %w", err)
373+
return ErrInvalidRecorderURL
377374
}
378-
p.recorderHooks = append(p.recorderHooks, recorder.URL)
375+
p.recorderHooks = append(p.recorderHooks, recorderURL)
379376
return nil
380377
}
381378

@@ -394,7 +391,7 @@ func (p *Server) recordHandler(w http.ResponseWriter, r *http.Request) {
394391
return
395392
}
396393

397-
err := p.Record(recorder)
394+
err := p.Record(recorder.URL)
398395
if err != nil {
399396
http.Error(w, err.Error(), http.StatusBadRequest)
400397
recordLogger.Debug().Err(err).Msg("Failed to add recorder")

parrot/recorder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestRecorder(t *testing.T) {
8585
recorder, err := NewRecorder()
8686
require.NoError(t, err, "error creating recorder")
8787

88-
err = p.Record(recorder)
88+
err = p.Record(recorder.URL)
8989
require.NoError(t, err, "error recording parrot")
9090
t.Cleanup(func() {
9191
require.NoError(t, recorder.Close())

0 commit comments

Comments
 (0)