Skip to content

Commit

Permalink
settings: Use new SafeWrite()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKar committed Jun 1, 2024
1 parent 25fb13b commit 474f98d
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 474f98d

Please sign in to comment.