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 cc0f624
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
11 changes: 6 additions & 5 deletions src/Confer/CLI/Cmd/Deploy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ deploy verbose deployments = do
destinationPathExists <- FileSystem.doesPathExist filepath
if destinationPathExists
then do
liftIO $
Text.putStrLn $
"[🔗] " <> display fact
createSymlink fact.source fact.destination
else when verbose $ do
destination <- liftIO $ OsPath.decodeFS fact.destination
liftIO $ Text.putStrLn $ display destination <> " already exists."
else do
createSymlink fact.source fact.destination
when verbose $ do
liftIO $
Text.putStrLn $
"[🔗] " <> display fact
43 changes: 36 additions & 7 deletions src/Confer/Effect/Symlink.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,29 @@ 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
Directory -> do
destinationExists <- FileSystem.doesDirectoryExist destinationPath
if destinationExists
then handleAlreadyExistingDestination sourcePath destinationPath
else createDirectoryLink sourcePath destinationPath
DeleteSymlink linkOsPath -> do
linkFilePath <- liftIO $ OsPath.decodeFS linkOsPath
sourceType <- liftIO $ do
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 +147,30 @@ runSymlinkPure virtualFS = reinterpret (State.evalState virtualFS) $ \_ -> \case
actualLinkTarget
)
Nothing -> pure $ Left (DoesNotExist linkPath)

handleAlreadyExistingDestination
:: (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
destinationIsSymbolic <- liftIO $ Directory.pathIsSymbolicLink destinationPath
if destinationIsSymbolic
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 do
liftIO $ putStrLn $ destinationPath <> " is not a symbolic link but a " <> show destinationType
Error.throwError (AlreadyExists destination)

0 comments on commit cc0f624

Please sign in to comment.