refactor: extract rename func
This commit is contained in:
25
main.go
25
main.go
@ -87,11 +87,9 @@ func removeSuffix(suffix string) filepath.WalkFunc {
|
||||
if filenameNoSuff != filenameNoExt {
|
||||
newFilename := filenameNoSuff + ext
|
||||
|
||||
err := os.Rename(filepath.Join(dir, filename), filepath.Join(dir, newFilename))
|
||||
err := rename(filepath.Join(dir, filename), filepath.Join(dir, newFilename), dryrun)
|
||||
if err != nil {
|
||||
fmt.Printf("Could not rename %s: %v\n", filename, err)
|
||||
} else {
|
||||
fmt.Println("Renamed " + filename + " to " + newFilename)
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,19 +108,20 @@ func normalize(p string, f os.FileInfo, err error) error {
|
||||
|
||||
element := newMediaElement(p)
|
||||
newPath := element.Path()
|
||||
if newPath != p {
|
||||
if dryrun {
|
||||
fmt.Printf("Would move %s to %s\n", p, newPath)
|
||||
} else {
|
||||
err = os.Rename(p, newPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("%s => %s\n", p, newPath)
|
||||
}
|
||||
if newPath == p {
|
||||
return nil
|
||||
}
|
||||
|
||||
return rename(p, newPath, dryrun)
|
||||
}
|
||||
|
||||
func rename(oldPath, newPath string, dryrun bool) error {
|
||||
fmt.Printf("%s => %s\n", oldPath, newPath)
|
||||
if dryrun {
|
||||
return nil
|
||||
}
|
||||
|
||||
return os.Rename(oldPath, newPath)
|
||||
}
|
||||
|
||||
func unpack(p string, f os.FileInfo, err error) error {
|
||||
|
Reference in New Issue
Block a user