Skip to content

Commit

Permalink
Merge pull request #15 from oliver-hohn/prevent-overwrite
Browse files Browse the repository at this point in the history
Prevent file overwrites with errors/logs
  • Loading branch information
oliver-hohn authored Dec 26, 2020
2 parents d3a4a4b + af3b24c commit e390542
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/oliverhohn/woodhouse

go 1.15

require github.com/djherbis/times v1.2.0
require (
github.com/djherbis/times v1.2.0
github.com/fatih/color v1.10.0
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
github.com/djherbis/times v1.2.0 h1:xANXjsC/iBqbO00vkWlYwPWgBgEVU6m6AFYg0Pic+Mc=
github.com/djherbis/times v1.2.0/go.mod h1:CGMZlo255K5r4Yw0b9RRfFQpM2y7uOmxg4jm9HsaVf8=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
43 changes: 39 additions & 4 deletions organizemyfiles/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/djherbis/times"
"github.com/fatih/color"
)

var inDir = flag.String("in", "", "Path to directory with files to organize")
Expand Down Expand Up @@ -84,6 +85,18 @@ func (f *File) GetExt() string {
return filepath.Ext(f.Path)
}

func (f *File) GetOutputDir() string {
return filepath.Join(*outDir, f.GetYear(), f.GetQuarter())
}

func (f *File) GetOutputFilename(suffix string) string {
if suffix != "" {
return fmt.Sprintf("%s_%s%s", f.GetName(), suffix, f.GetExt())
}

return fmt.Sprintf("%s%s", f.GetName(), f.GetExt())
}

func main() {
flag.Parse()

Expand Down Expand Up @@ -148,11 +161,20 @@ func shouldIgnoreFile(f *File) bool {
}

func copy(f *File, fileIndex uint64) error {
outPrefix := filepath.Join(*outDir, f.GetYear(), f.GetQuarter())
outPrefix := f.GetOutputDir()
// Suffix filename with an index to avoid clashes for similarly named files
outFilename := fmt.Sprintf("%s_%d%s", f.GetName(), fileIndex, f.GetExt())
outFilename := f.GetOutputFilename(strconv.FormatUint(fileIndex, 10))
out := filepath.Join(outPrefix, outFilename)

// Prevent overwrites by raising an error if the output file already exists
if _, err := os.Stat(out); !os.IsNotExist(err) {
if *dryrun {
logError("(dryrun) unable to copy %s to: %s, as the output file already exists\n", f.Path, out)
return nil
}
return fmt.Errorf("unable to copy %s to: %s, as the output file already exists", f.Path, out)
}

if *dryrun {
fmt.Printf("(dryrun) Copy %s to %s\n", f.Path, out)
return nil
Expand Down Expand Up @@ -186,11 +208,20 @@ func copy(f *File, fileIndex uint64) error {
}

func move(f *File, fileIndex uint64) error {
outPrefix := filepath.Join(*outDir, f.GetYear(), f.GetQuarter())
outPrefix := f.GetOutputDir()
// Suffix filename with an index to avoid clashes for similarly named files
outFilename := fmt.Sprintf("%s_%d%s", f.GetName(), fileIndex, f.GetExt())
outFilename := f.GetOutputFilename(strconv.FormatUint(fileIndex, 10))
out := filepath.Join(outPrefix, outFilename)

// Prevent overwrites by raising an error if the output file already exists
if _, err := os.Stat(out); !os.IsNotExist(err) {
if *dryrun {
logError("(dryrun) unable to move %s to: %s, as the output file already exists\n", f.Path, out)
return nil
}
return fmt.Errorf("unable to move %s to: %s, as the output file already exists", f.Path, out)
}

if *dryrun {
fmt.Printf("(dryrun) Move %s to %s\n", f.Path, out)
return nil
Expand All @@ -208,3 +239,7 @@ func move(f *File, fileIndex uint64) error {

return nil
}

func logError(msg string, args ...interface{}) {
color.Red(fmt.Sprintf(msg, args...))
}

0 comments on commit e390542

Please sign in to comment.