Skip to content

Commit

Permalink
fix: move wasn't working with folders
Browse files Browse the repository at this point in the history
  • Loading branch information
just-hms committed Mar 26, 2024
1 parent ed850c3 commit 7941c94
Showing 1 changed file with 2 additions and 27 deletions.
29 changes: 2 additions & 27 deletions osx/osx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package osx

import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand All @@ -24,35 +22,12 @@ func DownloadFolderPath() (string, error) {
}

func Move(sourcePath, destPath string) error {
inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("couldn't open source file: %v", err)
}
defer inputFile.Close()

fInfo, err := os.Stat(destPath)
if err == nil && fInfo.IsDir() {
destPath = filepath.Join(destPath, filepath.Base(inputFile.Name()))
}

outputFile, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("couldn't open dest file: %v", err)
destPath = filepath.Join(destPath, filepath.Base(sourcePath))
}
defer outputFile.Close()

_, err = io.Copy(outputFile, inputFile)
if err != nil {
return fmt.Errorf("couldn't copy to dest from source: %v", err)
}

inputFile.Close() // for Windows, close before trying to remove: https://stackoverflow.com/a/64943554/246801

err = os.Remove(sourcePath)
if err != nil {
return fmt.Errorf("couldn't remove source file: %v", err)
}
return nil
return os.Rename(sourcePath, destPath)
}

func LatestFile(files []fs.DirEntry) (os.FileInfo, error) {
Expand Down

0 comments on commit 7941c94

Please sign in to comment.