From 474f98de53ac4814d544c98bccb511ef3c642a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Sat, 1 Jun 2024 16:38:57 +0200 Subject: [PATCH] settings: Use new `SafeWrite()` --- internal/config/settings.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/internal/config/settings.go b/internal/config/settings.go index a4be8931c8..db5a039860 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -149,6 +149,26 @@ var ( VolatileSettings map[string]bool ) +type SettingsWriter struct { + txt []byte +} + +func (w SettingsWriter) Overwrite(name string, isBackup bool) error { + return os.WriteFile(name, w.txt, util.FileMode) +} + +func (w SettingsWriter) BackupDir() string { + backupdir, err := util.ReplaceHome(GlobalSettings["backupdir"].(string)) + if backupdir == "" || err != nil { + backupdir = filepath.Join(ConfigDir, "backups") + } + return backupdir +} + +func (w SettingsWriter) KeepBackup() bool { + return GlobalSettings["permbackup"].(bool) +} + func init() { ModifiedSettings = make(map[string]bool) VolatileSettings = make(map[string]bool) @@ -289,8 +309,10 @@ func WriteSettings(filename string) error { } } - txt, _ := json.MarshalIndent(parsedSettings, "", " ") - err = os.WriteFile(filename, append(txt, '\n'), util.FileMode) + var w SettingsWriter + w.txt, _ = json.MarshalIndent(parsedSettings, "", " ") + w.txt = append(w.txt, '\n') + err = util.SafeWrite(filename, w) } return err } @@ -311,8 +333,10 @@ func OverwriteSettings(filename string) error { } } - txt, _ := json.MarshalIndent(settings, "", " ") - err = os.WriteFile(filename, append(txt, '\n'), util.FileMode) + var w SettingsWriter + w.txt, _ = json.MarshalIndent(parsedSettings, "", " ") + w.txt = append(w.txt, '\n') + err = util.SafeWrite(filename, w) } return err }