Refactor: Separate file existence check from CopyMusic#4
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
musicutils.FileExists(filePath string) boolis created to specifically check if a file (not a directory) exists at the given path.movemusic.CopyMusicfunction 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.cmd/copy.gologic has been updated to first callmovemusic.BuildDestinationFileNameto determine the target path, then usemusicutils.FileExiststo 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.movemusic.ErrFileExistserror variable has been removed as it's no longer needed.musicutils.FileExistsandmovemusic.CopyMusicto verify the new behavior, including overwrite scenarios. Existing tests for related functions likeBuildDestinationFileNamewere 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.