Skip to content

Commit

Permalink
Merge pull request #13 from mpl-interhyp/rewrite_recording_files_on_p…
Browse files Browse the repository at this point in the history
…layback

Added new environment variable to rewrite all accessed recording file…
  • Loading branch information
StephanHCB committed Nov 2, 2022
2 parents 8ca20bb + 6f8dfc6 commit 448117a
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions implementation/playback/playback.go
Expand Up @@ -7,12 +7,15 @@ import (
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
aurestrecorder "github.com/StephanHCB/go-autumn-restclient/implementation/recorder"
"os"
"strings"
"path/filepath"
"time"
)

const PlaybackRewritePathEnvVariable = "GO_AUTUMN_RESTCLIENT_PLAYBACK_REWRITE_PATH"

type PlaybackImpl struct {
RecorderPath string
RecorderPath string
RecorderRewritePath string
// Now is exposed so tests can fixate the time by overwriting this field
Now func() time.Time
}
Expand All @@ -21,14 +24,10 @@ type PlaybackImpl struct {
//
// Use this in your tests.
func New(recorderPath string) aurestclientapi.Client {
if recorderPath != "" {
if !strings.HasSuffix(recorderPath, "/") {
recorderPath += "/"
}
}
return &PlaybackImpl{
RecorderPath: recorderPath,
Now: time.Now,
RecorderPath: recorderPath,
RecorderRewritePath: os.Getenv(PlaybackRewritePathEnvVariable),
Now: time.Now,
}
}

Expand All @@ -38,30 +37,38 @@ func (c *PlaybackImpl) Perform(ctx context.Context, method string, requestUrl st
return err
}

jsonBytes, err := os.ReadFile(c.RecorderPath + filename)
jsonBytes, err := os.ReadFile(filepath.Join(c.RecorderPath, filename))
if err != nil {
// try old filename for compatibility (cannot fail if ConstructFilenameV2 didn't)
filenameOldV1, _ := aurestrecorder.ConstructFilename(method, requestUrl)

jsonBytesOldV1, errWithOldFilenameV1 := os.ReadFile(c.RecorderPath + filenameOldV1)
jsonBytesOldV1, errWithOldFilenameV1 := os.ReadFile(filepath.Join(c.RecorderPath, filenameOldV1))
if errWithOldFilenameV1 != nil {
// try old filename for compatibility (cannot fail if ConstructFilenameV2 didn't)
filenameOldV2, _ := aurestrecorder.ConstructFilenameV2(method, requestUrl)

jsonBytesOldV2, errWithOldFilenameV2 := os.ReadFile(c.RecorderPath + filenameOldV2)
jsonBytesOldV2, errWithOldFilenameV2 := os.ReadFile(filepath.Join(c.RecorderPath, filenameOldV2))
if errWithOldFilenameV2 != nil {
// but return original error if that also fails
return err
} else {
aulogging.Logger.Ctx(ctx).Info().Printf("use of deprecated recorder filename (v2) '%s', please move to '%s'", filenameOldV2, filename)

_ = c.rewriteFileIfConfigured(ctx, filenameOldV2, filename)

filename = filenameOldV2
jsonBytes = jsonBytesOldV2
}
} else {
aulogging.Logger.Ctx(ctx).Info().Printf("use of deprecated recorder filename (v1) '%s', please move to '%s'", filenameOldV1, filename)

_ = c.rewriteFileIfConfigured(ctx, filenameOldV1, filename)

filename = filenameOldV1
jsonBytes = jsonBytesOldV1
}
} else {
_ = c.rewriteFileIfConfigured(ctx, filename, filename)
}

recording := aurestrecorder.RecorderData{}
Expand All @@ -86,3 +93,27 @@ func (c *PlaybackImpl) Perform(ctx context.Context, method string, requestUrl st

return recording.Error
}

func (c *PlaybackImpl) rewriteFileIfConfigured(ctx context.Context, fileNameFrom string, fileNameTo string) error {
if c.RecorderRewritePath != "" {
fileBase := filepath.Base(c.RecorderPath)

bytes, err := os.ReadFile(filepath.Join(c.RecorderPath, fileNameFrom))
if err != nil {
aulogging.Logger.Ctx(ctx).Error().WithErr(err).Printf("Can't read file: '%s'", fileNameFrom)
return err
}

targetPath := filepath.Join(c.RecorderRewritePath, fileBase)
if _, err := os.Stat(targetPath); os.IsNotExist(err) {
_ = os.MkdirAll(targetPath, 0700)
}

err = os.WriteFile(filepath.Join(targetPath, fileNameTo), bytes, 0644)
if err != nil {
aulogging.Logger.Ctx(ctx).Error().WithErr(err).Printf("Can't write file: '%s'", fileNameTo)
return err
}
}
return nil
}

0 comments on commit 448117a

Please sign in to comment.