copy config file to backup if unmarshal error occurs

This commit is contained in:
Drew Weymouth
2023-02-19 14:41:12 -08:00
parent a83e21d7ea
commit c9cd0922ea
2 changed files with 28 additions and 1 deletions
+9 -1
View File
@@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"log"
"os"
"path"
"supersonic/backend/util"
"supersonic/player"
"github.com/20after4/configdir"
@@ -62,10 +64,16 @@ func StartupApp() (*App, error) {
func (a *App) readConfig() {
configdir.MakePath(configdir.LocalConfig(AppName))
cfg, err := ReadConfigFile(configPath())
cfgPath := configPath()
cfg, err := ReadConfigFile(cfgPath)
if err != nil {
log.Printf("Error reading app config file: %v", err)
cfg = DefaultConfig()
if _, err := os.Stat(cfgPath); err == nil {
backupCfgName := fmt.Sprintf("%s.bak", configFile)
log.Printf("Config file may be malformed: copying to %s", backupCfgName)
_ = util.CopyFile(cfgPath, path.Join(configdir.LocalConfig(AppName), backupCfgName))
}
}
a.Config = cfg
}