Skip to content

Refactor: Separate file existence check from CopyMusic#4

Merged
punkscience merged 1 commit into
mainfrom
refactor-copymusic-fileexists
May 23, 2025
Merged

Refactor: Separate file existence check from CopyMusic#4
punkscience merged 1 commit into
mainfrom
refactor-copymusic-fileexists

Conversation

@punkscience

Copy link
Copy Markdown
Owner

The CopyMusic function in movemusic/movemusic.go previously handled both checking for the destination file's existence and performing the copy operation.

This change introduces the following:

  • A new public function musicutils.FileExists(filePath string) bool is created to specifically check if a file (not a directory) exists at the given path.
  • The movemusic.CopyMusic function has been modified to remove the file existence check. It now focuses solely on copying the file. If a destination file exists, it will be overwritten.
  • The cmd/copy.go logic has been updated to first call movemusic.BuildDestinationFileName to determine the target path, then use musicutils.FileExists to check if the file already exists. If it does, the copy operation is skipped. This check is implemented for both normal and dry-run modes.
  • The movemusic.ErrFileExists error variable has been removed as it's no longer needed.
  • Unit tests have been added for musicutils.FileExists and movemusic.CopyMusic to verify the new behavior, including overwrite scenarios. Existing tests for related functions like BuildDestinationFileName were also reviewed and updated.

This refactoring allows for better separation of concerns, enabling calling code to manage file existence checks before deciding to invoke the copy operation.

The CopyMusic function in movemusic/movemusic.go previously handled
both checking for the destination file's existence and performing
the copy operation.

This change introduces the following:
- A new public function `musicutils.FileExists(filePath string) bool`
  is created to specifically check if a file (not a directory)
  exists at the given path.
- The `movemusic.CopyMusic` function has been modified to remove
  the file existence check. It now focuses solely on copying
  the file. If a destination file exists, it will be overwritten.
- The `cmd/copy.go` logic has been updated to first call
  `movemusic.BuildDestinationFileName` to determine the target path,
  then use `musicutils.FileExists` to check if the file already
  exists. If it does, the copy operation is skipped. This check
  is implemented for both normal and dry-run modes.
- The `movemusic.ErrFileExists` error variable has been removed as
  it's no longer needed.
- Unit tests have been added for `musicutils.FileExists` and
  `movemusic.CopyMusic` to verify the new behavior, including
  overwrite scenarios. Existing tests for related functions like
  `BuildDestinationFileName` were also reviewed and updated.

This refactoring allows for better separation of concerns, enabling
calling code to manage file existence checks before deciding to
invoke the copy operation.
@punkscience
punkscience merged commit 1c31c34 into main May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant