Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new environment variable to rewrite all accessed recording file… #13

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}