on write conflict during vim, save as filename.uuid..fileExtension

This commit is contained in:
Bel LaPointe
2023-07-02 10:22:04 -06:00
parent d105df15e9
commit 6bad1e1365
3 changed files with 16 additions and 2 deletions

View File

@@ -15,6 +15,8 @@ import (
"syscall"
"time"
"github.com/google/uuid"
"gogs.inhome.blapointe.com/bel/pttodo/pttodo"
"gopkg.in/yaml.v2"
@@ -160,6 +162,7 @@ func edit(dry bool, filepaths []string) error {
return err
}
originals := map[string]pttodo.Root{}
lastModified := map[string]time.Time{}
for _, target := range filepaths {
var original pttodo.Root
if r, err := filePathReader(target); err != nil {
@@ -171,7 +174,9 @@ func edit(dry bool, filepaths []string) error {
} else if err := ioutil.WriteFile(path.Join(tempDir, path.Base(target)), c, os.ModePerm); err != nil {
return err
}
info, _ := os.Stat(target)
originals[target] = original
lastModified[target] = info.ModTime()
}
if err := vimd(tempDir); err != nil {
return err
@@ -236,7 +241,11 @@ func edit(dry bool, filepaths []string) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(target, c, os.ModePerm); err != nil {
outputPath := target
if info, _ := os.Stat(target); info.ModTime() != lastModified[target] {
outputPath = fmt.Sprintf("%s.%s.%s", outputPath, uuid.New().String(), path.Ext(outputPath))
}
if err := ioutil.WriteFile(outputPath, c, os.ModePerm); err != nil {
return err
}
}

View File

@@ -7,6 +7,9 @@ require (
gopkg.in/yaml.v2 v2.4.0
)
require github.com/robfig/cron/v3 v3.0.1 // indirect
require (
github.com/google/uuid v1.3.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
)
replace gogs.inhome.blapointe.com/bel/pttodo => ../..

View File

@@ -1,3 +1,5 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=