copy config file to backup if unmarshal error occurs
This commit is contained in:
+9
-1
@@ -5,7 +5,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"supersonic/backend/util"
|
||||||
"supersonic/player"
|
"supersonic/player"
|
||||||
|
|
||||||
"github.com/20after4/configdir"
|
"github.com/20after4/configdir"
|
||||||
@@ -62,10 +64,16 @@ func StartupApp() (*App, error) {
|
|||||||
|
|
||||||
func (a *App) readConfig() {
|
func (a *App) readConfig() {
|
||||||
configdir.MakePath(configdir.LocalConfig(AppName))
|
configdir.MakePath(configdir.LocalConfig(AppName))
|
||||||
cfg, err := ReadConfigFile(configPath())
|
cfgPath := configPath()
|
||||||
|
cfg, err := ReadConfigFile(cfgPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading app config file: %v", err)
|
log.Printf("Error reading app config file: %v", err)
|
||||||
cfg = DefaultConfig()
|
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
|
a.Config = cfg
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,3 +20,20 @@ func ShuffleSlice(a []int) {
|
|||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
|
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