diff options
| author | Paul Buetow <paul@buetow.org> | 2025-05-28 22:35:13 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-05-28 22:35:13 +0300 |
| commit | 326a1faf9e2aad681c5886c18ab93ee1c99ae24d (patch) | |
| tree | f0c9f1ece8070600ab4c114480eb8ddf93440e32 | |
| parent | 00d8b2eec47830423cc39c0b2dc403ecd1b1273b (diff) | |
persisting report to a file as well
| -rw-r--r-- | internal/run.go | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/internal/run.go b/internal/run.go index 01f5bae..701d200 100644 --- a/internal/run.go +++ b/internal/run.go @@ -2,7 +2,9 @@ package internal import ( "context" + "fmt" "log" + "os" ) func Run(ctx context.Context, configFile string, renotify, force bool) { @@ -26,9 +28,30 @@ func Run(ctx context.Context, configFile string, renotify, force bool) { notifyError(conf, err) } - if subject, body, doNotify := state.report(renotify, force); doNotify { + subject, body, doNotify := state.report(renotify, force) + if doNotify { if err := notify(conf, subject, body); err != nil { log.Println("error:", err) + return } } + if err := persistReport(body, conf); err != nil { + notifyError(conf, err) + } +} + +func persistReport(body string, conf config) error { + reportFile := fmt.Sprintf("%s/report.txt", conf.StateDir) + tmpFile := fmt.Sprintf("%s.tmp", reportFile) + + f, err := os.Create(tmpFile) + if err != nil { + return err + } + defer f.Close() + + if _, err = f.WriteString(body); err != nil { + return err + } + return os.Rename(tmpFile, reportFile) } |
