diff --git a/gosmee/client.go b/gosmee/client.go index cb6cf4d..a1ae4d2 100644 --- a/gosmee/client.go +++ b/gosmee/client.go @@ -163,10 +163,10 @@ func emoji(emoji, color string, decorate bool) string { return ansi.Color(emoji, color) + " " } -func (c goSmee) saveData(pm payloadMsg) error { +func saveData(rd *replayDataOpts, logger *slog.Logger, pm payloadMsg) error { // check if saveDir is created - if _, err := os.Stat(c.replayDataOpts.saveDir); os.IsNotExist(err) { - if err := os.MkdirAll(c.replayDataOpts.saveDir, 0o755); err != nil { + if _, err := os.Stat(rd.saveDir); os.IsNotExist(err) { + if err := os.MkdirAll(rd.saveDir, 0o755); err != nil { return err } } @@ -178,7 +178,7 @@ func (c goSmee) saveData(pm payloadMsg) error { fbasepath = pm.timestamp } - jsonfile := fmt.Sprintf("%s/%s.json", c.replayDataOpts.saveDir, fbasepath) + jsonfile := fmt.Sprintf("%s/%s.json", rd.saveDir, fbasepath) f, err := os.Create(jsonfile) if err != nil { return err @@ -190,9 +190,9 @@ func (c goSmee) saveData(pm payloadMsg) error { return err } - shscript := fmt.Sprintf("%s/%s.sh", c.replayDataOpts.saveDir, fbasepath) + shscript := fmt.Sprintf("%s/%s.sh", rd.saveDir, fbasepath) - c.logger.Info(fmt.Sprintf("%s%s and %s has been saved", emoji("⌁", "yellow+b", c.replayDataOpts.decorate), shscript, jsonfile)) + logger.Info(fmt.Sprintf("%s%s and %s has been saved", emoji("⌁", "yellow+b", rd.decorate), shscript, jsonfile)) s, err := os.Create(shscript) if err != nil { return err @@ -212,7 +212,7 @@ func (c goSmee) saveData(pm payloadMsg) error { FileBase string }{ Headers: headers, - TargetURL: c.replayDataOpts.targetURL, + TargetURL: rd.targetURL, ContentType: pm.contentType, FileBase: fbasepath, }); err != nil { @@ -322,7 +322,7 @@ func (c goSmee) clientSetup() error { if string(msg.Data) != "{}" { if c.replayDataOpts.saveDir != "" { - err := c.saveData(pm) + err := saveData(c.replayDataOpts, c.logger, pm) if err != nil { s := fmt.Sprintf("%s %s saving message with headers '%s' - %s", nowStr, diff --git a/gosmee/replay.go b/gosmee/replay.go index 0072d9f..5a835a5 100644 --- a/gosmee/replay.go +++ b/gosmee/replay.go @@ -103,6 +103,17 @@ func (r *replayOpts) replayHooks(ctx context.Context, hookid int64) error { r.logger.Error(s) continue } + if r.replayDataOpts.saveDir != "" { + err := saveData(r.replayDataOpts, r.logger, pm) + if err != nil { + s := fmt.Sprintf("%s saving payload to local directory %s - %s\n", + ansi.Color("ERROR", "red+b"), + r.replayDataOpts.saveDir, + err.Error()) + r.logger.Error(s) + continue + } + } } if len(deliveries) != 0 {