Skip to content

Commit c4dcef3

Browse files
committed
micro: Provide recovery of settings.json & bindings.json
1 parent e15bb88 commit c4dcef3

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

cmd/micro/micro.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"os"
99
"os/signal"
10+
"path/filepath"
1011
"regexp"
1112
"runtime"
1213
"runtime/pprof"
@@ -222,6 +223,35 @@ func LoadInput(args []string) []*buffer.Buffer {
222223
return buffers
223224
}
224225

226+
func checkBackup(name string) error {
227+
target := filepath.Join(config.ConfigDir, name)
228+
backup := util.AppendBackupSuffix(target)
229+
if info, err := os.Stat(backup); err == nil {
230+
input, err := os.ReadFile(backup)
231+
if err == nil {
232+
t := info.ModTime()
233+
msg := fmt.Sprintf(buffer.BackupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), backup)
234+
choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
235+
236+
if choice%3 == 0 {
237+
// recover
238+
err := os.WriteFile(target, input, util.FileMode)
239+
if err != nil {
240+
return err
241+
}
242+
return os.Remove(backup)
243+
} else if choice%3 == 1 {
244+
// delete
245+
return os.Remove(backup)
246+
} else if choice%3 == 2 {
247+
// abort
248+
return errors.New("Aborted")
249+
}
250+
}
251+
}
252+
return nil
253+
}
254+
225255
func exit(rc int) {
226256
for _, b := range buffer.OpenBuffers {
227257
if !b.Modified() {
@@ -269,6 +299,12 @@ func main() {
269299
config.InitRuntimeFiles(true)
270300
config.InitPlugins()
271301

302+
err = checkBackup("settings.json")
303+
if err != nil {
304+
screen.TermMessage(err)
305+
exit(1)
306+
}
307+
272308
err = config.ReadSettings()
273309
if err != nil {
274310
screen.TermMessage(err)
@@ -329,6 +365,12 @@ func main() {
329365
screen.TermMessage(err)
330366
}
331367

368+
err = checkBackup("bindings.json")
369+
if err != nil {
370+
screen.TermMessage(err)
371+
exit(1)
372+
}
373+
332374
action.InitBindings()
333375
action.InitCommands()
334376

internal/buffer/backup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/zyedidia/micro/v2/internal/util"
1515
)
1616

17-
const backupMsg = `A backup was detected for this file. This likely means that micro
17+
const BackupMsg = `A backup was detected for this file. This likely means that micro
1818
crashed while editing this file, or another instance of micro is currently
1919
editing this file.
2020
@@ -131,7 +131,7 @@ func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
131131
if err == nil {
132132
defer backup.Close()
133133
t := info.ModTime()
134-
msg := fmt.Sprintf(backupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), backupfile)
134+
msg := fmt.Sprintf(BackupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), backupfile)
135135
choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
136136

137137
if choice%3 == 0 {

0 commit comments

Comments
 (0)