Skip to content

Commit

Permalink
Fixes recorders
Browse files Browse the repository at this point in the history
  • Loading branch information
kalverra committed Jan 23, 2025
1 parent 9598eda commit 93ad4a1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
19 changes: 14 additions & 5 deletions parrot/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (

func main() {
var (
port int
debug bool
trace bool
silent bool
json bool
port int
debug bool
trace bool
silent bool
json bool
recorders []string
)

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -49,6 +50,13 @@ func main() {
return err
}

for _, r := range recorders {
err = p.Record(r)
if err != nil {
return err
}
}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
Expand All @@ -65,6 +73,7 @@ func main() {
rootCmd.Flags().BoolVarP(&trace, "trace", "t", false, "Enable trace and debug output")
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Disable all output")
rootCmd.Flags().BoolVarP(&json, "json", "j", false, "Output logs in JSON format")
rootCmd.Flags().StringSliceVarP(&recorders, "recorders", "r", nil, "Existing recorders to use")

if err := rootCmd.Execute(); err != nil {
log.Error().Err(err).Msg("error executing command")
Expand Down
6 changes: 3 additions & 3 deletions parrot/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var (
ErrResponseMarshal = errors.New("unable to marshal response body to JSON")
ErrRouteNotFound = errors.New("route not found")

ErrNoRecorderURL = errors.New("no recorder URL specified")
ErrNilRecorder = errors.New("recorder is nil")
ErrRecorderNotFound = errors.New("recorder not found")
ErrNoRecorderURL = errors.New("no recorder URL specified")
ErrInvalidRecorderURL = errors.New("invalid recorder URL")
ErrRecorderNotFound = errors.New("recorder not found")
)

// Custom error type to help add more detail to base errors
Expand Down
15 changes: 6 additions & 9 deletions parrot/parrot.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,20 +362,17 @@ func (p *Server) registerRouteHandler(w http.ResponseWriter, r *http.Request) {
}

// Record registers a new recorder with the parrot. All incoming requests to the parrot will be sent to the recorder.
func (p *Server) Record(recorder *Recorder) error {
func (p *Server) Record(recorderURL string) error {
p.recordersMu.Lock()
defer p.recordersMu.Unlock()
if recorder == nil {
return ErrNilRecorder
}
if recorder.URL == "" {
if recorderURL == "" {
return ErrNoRecorderURL
}
_, err := url.Parse(recorder.URL)
_, err := url.Parse(recorderURL)
if err != nil {
return fmt.Errorf("failed to parse recorder URL: %w", err)
return ErrInvalidRecorderURL
}
p.recorderHooks = append(p.recorderHooks, recorder.URL)
p.recorderHooks = append(p.recorderHooks, recorderURL)
return nil
}

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

err := p.Record(recorder)
err := p.Record(recorder.URL)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
recordLogger.Debug().Err(err).Msg("Failed to add recorder")
Expand Down
2 changes: 1 addition & 1 deletion parrot/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestRecorder(t *testing.T) {
recorder, err := NewRecorder()
require.NoError(t, err, "error creating recorder")

err = p.Record(recorder)
err = p.Record(recorder.URL)
require.NoError(t, err, "error recording parrot")
t.Cleanup(func() {
require.NoError(t, recorder.Close())
Expand Down

0 comments on commit 93ad4a1

Please sign in to comment.