Skip to content

Commit

Permalink
Allow user to restore the last config backup in case of corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
qrasmont committed Feb 27, 2022
1 parent 9f39b6c commit 4697dc6
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,43 @@ func hasConfigFile(file string) (bool, error) {
return false, nil
}

func userWantsToRestoreConfig() bool {
resp := ""
for {
fmt.Println("Could not parse your config.")
fmt.Println("Do you want to restore the last backup?")
fmt.Println("NOTE: This will override your current config file [yY/nN]: ")
fmt.Scanln(&resp)
if resp == "y" || resp == "Y" {
return true
} else if resp == "n" || resp == "N" {
return false
} else {
fmt.Println("Wrong input!")
}
}
}

func restoreConfig(config *Config, path string) error {
jsonFile, err := os.Open(path + ".bak")
if err != nil {
return err
}

defer jsonFile.Close()

byteValue, _ := ioutil.ReadAll(jsonFile)

err = json.Unmarshal(byteValue, &config)
if err != nil {
return err
}

storeConfig(config, path)

return nil
}

func parseConfig(config *Config, path string) error {
jsonFile, err := os.Open(path)
if err != nil {
Expand All @@ -61,6 +98,10 @@ func parseConfig(config *Config, path string) error {

err = json.Unmarshal(byteValue, &config)
if err != nil {
if userWantsToRestoreConfig() {
restoreConfig(config, path)
return nil
}
return err
}

Expand Down

0 comments on commit 4697dc6

Please sign in to comment.