Skip to content

Commit

Permalink
refactor link creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tchoutri committed Jul 20, 2024
1 parent af2628e commit d123343
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/Confer/Effect/Symlink.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ runSymlinkIO = interpret $ \_ -> \case
sourceType <- liftIO $ do
metadata <- Directory.getFileMetadata source
pure $ Directory.fileTypeFromMetadata metadata
sourceFilePath <- liftIO $ OsPath.decodeFS source
sourcePath <- FileSystem.makeAbsolute sourceFilePath
sourcePath <- FileSystem.makeAbsolute =<< liftIO (OsPath.decodeFS source)
destinationPath <- liftIO $ OsPath.decodeFS destination
case sourceType of

Check warning on line 78 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on alpine-3.19

Pattern match(es) are non-exhaustive

Check warning on line 78 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

Pattern match(es) are non-exhaustive

Check warning on line 78 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

Pattern match(es) are non-exhaustive
File -> do
destinationExists <- FileSystem.doesFileExist destinationPath
if destinationExists
then Error.throwError (AlreadyExists destination)
then handleAlreadyExistingDestination sourcePath destinationPath
else createFileLink sourcePath destinationPath
Directory ->
createDirectoryLink sourcePath destinationPath
Expand All @@ -90,10 +89,10 @@ runSymlinkIO = interpret $ \_ -> \case
metadata <- Directory.getFileMetadata linkOsPath
pure $ Directory.fileTypeFromMetadata metadata
case sourceType of
File ->
FileSystem.removeFile linkFilePath
Directory ->
FileSystem.removeDirectory linkFilePath
_ ->
FileSystem.removeFile linkFilePath
TestSymlink linkOsPath expectedLinkTarget -> do
linkFilepath <- liftIO $ OsPath.decodeFS linkOsPath
liftIO $
Expand Down Expand Up @@ -145,3 +144,27 @@ runSymlinkPure virtualFS = reinterpret (State.evalState virtualFS) $ \_ -> \case
actualLinkTarget
)
Nothing -> pure $ Left (DoesNotExist linkPath)

handleAlreadyExistingDestination
:: (FileSystem :> es, Error SymlinkError :> es, IOE :> es)
=> FilePath
-> FilePath
-> Eff es ()
handleAlreadyExistingDestination sourcePath destinationPath = do
source <- liftIO $ OsPath.encodeFS sourcePath
destination <- liftIO $ OsPath.encodeFS destinationPath
destinationType <- liftIO $ do
metadata <- Directory.getFileMetadata destination
pure $ Directory.fileTypeFromMetadata metadata
if Directory.fileTypeIsLink destinationType
then do
destinationTruePath <-
liftIO $
Directory.readSymbolicLink destination
if destinationTruePath == source
then do
liftIO $
putStrLn $
destinationPath <> " already points to " <> sourcePath <> "."
else Error.throwError (AlreadyExists destination)
else createFileLink sourcePath destinationPath

0 comments on commit d123343

Please sign in to comment.