diff --git a/cmd/pttodo-cli/cli.go b/cmd/pttodo-cli/cli.go index b740889..29c3ecd 100644 --- a/cmd/pttodo-cli/cli.go +++ b/cmd/pttodo-cli/cli.go @@ -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 } } diff --git a/cmd/pttodo-cli/go.mod b/cmd/pttodo-cli/go.mod index 4a0aeae..77fbfb7 100644 --- a/cmd/pttodo-cli/go.mod +++ b/cmd/pttodo-cli/go.mod @@ -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 => ../.. diff --git a/cmd/pttodo-cli/go.sum b/cmd/pttodo-cli/go.sum index 441e8f1..200a035 100644 --- a/cmd/pttodo-cli/go.sum +++ b/cmd/pttodo-cli/go.sum @@ -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=