copy config file to backup if unmarshal error occurs
This commit is contained in:
+9
-1
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user