diff --git a/backend/app.go b/backend/app.go index 84551b3..8aa1d22 100644 --- a/backend/app.go +++ b/backend/app.go @@ -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 } diff --git a/backend/util/util.go b/backend/util/util.go index dc49ff7..43fe28b 100644 --- a/backend/util/util.go +++ b/backend/util/util.go @@ -1,7 +1,9 @@ package util import ( + "io" "math/rand" + "os" "time" ) @@ -18,3 +20,20 @@ func ShuffleSlice(a []int) { rand.Seed(time.Now().UnixNano()) rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] }) } + +func CopyFile(srcPath, dstPath string) error { + fin, err := os.Open(srcPath) + if err != nil { + return err + } + defer fin.Close() + + fout, err := os.Create(dstPath) + if err != nil { + return err + } + defer fout.Close() + + _, err = io.Copy(fout, fin) + return err +}