Skip to content

Commit

Permalink
bindings: Use new SafeWrite()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKar committed Jun 1, 2024
1 parent 12e277b commit 25fb13b
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions internal/action/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,29 @@ var Binder = map[string]func(e Event, action string){
"terminal": TermMapEvent,
}

type BindingsWriter struct {
txt []byte
}

func (w BindingsWriter) Overwrite(name string, isBackup bool) error {
return os.WriteFile(name, w.txt, util.FileMode)
}

func (w BindingsWriter) BackupDir() string {
backupdir, err := util.ReplaceHome(config.GlobalSettings["backupdir"].(string))
if backupdir == "" || err != nil {
backupdir = filepath.Join(config.ConfigDir, "backups")
}
return backupdir
}

func (w BindingsWriter) KeepBackup() bool {
return config.GlobalSettings["permbackup"].(bool)
}

func createBindingsIfNotExist(fname string) {
if _, e := os.Stat(fname); errors.Is(e, fs.ErrNotExist) {
os.WriteFile(fname, []byte("{}"), util.FileMode)
util.SafeWrite(fname, BindingsWriter{[]byte("{}")})
}
}

Expand Down Expand Up @@ -317,8 +337,10 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {

BindKey(k, v, Binder["buffer"])

txt, _ := json.MarshalIndent(parsed, "", " ")
return true, os.WriteFile(filename, append(txt, '\n'), util.FileMode)
var w BindingsWriter
w.txt, _ = json.MarshalIndent(parsed, "", " ")
w.txt = append(w.txt, '\n')
return true, util.SafeWrite(filename, w)
}
return false, e
}
Expand Down Expand Up @@ -367,8 +389,10 @@ func UnbindKey(k string) error {
delete(config.Bindings["buffer"], k)
}

txt, _ := json.MarshalIndent(parsed, "", " ")
return os.WriteFile(filename, append(txt, '\n'), util.FileMode)
var w BindingsWriter
w.txt, _ = json.MarshalIndent(parsed, "", " ")
w.txt = append(w.txt, '\n')
return util.SafeWrite(filename, w)
}
return e
}
Expand Down

0 comments on commit 25fb13b

Please sign in to comment.