Skip to content

Commit b09d818

Browse files
committed
Progress bar for deploy command
1 parent cc0594b commit b09d818

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

src/Confer/CLI/Cmd/Deploy.hs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import Data.List.NonEmpty qualified as NE
77
import Data.Text.Display
88
import Data.Text.IO qualified as Text
99
import Data.Vector (Vector)
10+
import Data.Vector qualified as Vector
1011
import Effectful
12+
import Effectful.Concurrent
13+
import Effectful.Console.ByteString
1114
import Effectful.Error.Static
1215
import Effectful.FileSystem (FileSystem)
1316
import Effectful.FileSystem qualified as FileSystem
17+
import GHC.Float
1418
import System.OsPath (OsPath)
1519
import System.OsPath qualified as OsPath
1620

1721
import Confer.CLI.Errors
22+
import Confer.CLI.UI
1823
import Confer.Config.Types
1924
import Confer.Effect.Symlink (Symlink, SymlinkError (..))
2025
import Confer.Effect.Symlink qualified as Symlink
@@ -31,32 +36,34 @@ deploy
3136
:: ( FileSystem :> es
3237
, Symlink :> es
3338
, IOE :> es
39+
, Console :> es
40+
, Concurrent :> es
3441
, Error (NonEmpty CLIError) :> es
3542
)
3643
=> Bool
3744
-> Vector Deployment
3845
-> Eff es ()
3946
deploy quiet deployments = do
40-
forM_ deployments $ \d ->
41-
forM_ d.facts $ \fact -> do
42-
linkFilepath <- liftIO $ OsPath.decodeFS fact.destination
43-
destinationPathExists <- FileSystem.doesPathExist linkFilepath
44-
if destinationPathExists
45-
then do
46-
result <- Symlink.testSymlink fact.destination fact.source
47-
case result of
48-
Left symlinkError -> do
49-
let cliError = case symlinkError of
50-
DoesNotExist path -> symlinkDoesNotExistError path
51-
IsNotSymlink path -> pathIsNotSymlinkError path
52-
AlreadyExists path -> symlinkAlreadyExistsError path
53-
WrongTarget link expected actual -> wrongTargetError link expected actual
54-
throwError (NE.singleton cliError)
55-
Right _ ->
56-
liftIO $ Text.putStrLn $ display (linkFilepath <> "")
57-
else do
58-
Symlink.createSymlink fact.source fact.destination
59-
unless quiet $ do
60-
liftIO $
61-
Text.putStrLn $
62-
"[🔗] " <> display fact
47+
let facts = deployments >>= (.facts)
48+
Vector.iforM facts $ \index fact -> do
49+
threadDelay 30_000
50+
let percentage = int2Double index / int2Double (Vector.length facts)
51+
unless quiet $ printProgress "Deploying" percentage
52+
linkFilepath <- liftIO $ OsPath.decodeFS fact.destination
53+
destinationPathExists <- FileSystem.doesPathExist linkFilepath
54+
if destinationPathExists
55+
then do
56+
result <- Symlink.testSymlink fact.destination fact.source
57+
case result of
58+
Left symlinkError -> do
59+
let cliError = case symlinkError of
60+
DoesNotExist path -> symlinkDoesNotExistError path
61+
AlreadyExists path -> symlinkAlreadyExistsError path
62+
IsNotSymlink path -> pathIsNotSymlinkError path
63+
WrongTarget link expected actual -> wrongTargetError link expected actual
64+
throwError (NE.singleton cliError)
65+
Right _ -> pure ()
66+
else do
67+
Symlink.createSymlink fact.source fact.destination
68+
69+
unless quiet $ printProgress "Deploying" 1.0

src/Confer/CLI/Errors.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ noDefaultConfigurationFileError =
6464
CLIError
6565
{ errorType = NoDefaultConfigurationFile
6666
, errorCode = ErrorCode 156
67-
, errorMessage = "Could not find configuration file at ./deployments.lua"
67+
, errorMessage = "Could not find configuration file at ./deployments.lua."
6868
}
6969

7070
noUserProvidedConfigurationFileError :: OsPath -> CLIError
7171
noUserProvidedConfigurationFileError path =
7272
CLIError
7373
{ errorType = NoUserProvidedConfigurationFile
7474
, errorCode = ErrorCode 169
75-
, errorMessage = "Could not find configuration file at" <> Text.show path
75+
, errorMessage = "Could not find configuration file at" <> Text.show path <> "."
7676
}
7777

7878
noDeploymentsAvailableError :: DeploymentOS -> DeploymentArchitecture -> Text -> CLIError
@@ -87,30 +87,31 @@ noDeploymentsAvailableError os arch hostname =
8787
<> display os
8888
<> " "
8989
<> hostname
90+
<> "."
9091
}
9192

9293
symlinkDoesNotExistError :: OsPath -> CLIError
9394
symlinkDoesNotExistError path =
9495
CLIError
9596
{ errorType = SymlinkErrorType (DoesNotExist path)
9697
, errorCode = ErrorCode 234
97-
, errorMessage = Text.show path <> " does not exist"
98+
, errorMessage = Text.show path <> " does not exist."
9899
}
99100

100101
symlinkAlreadyExistsError :: OsPath -> CLIError
101102
symlinkAlreadyExistsError path =
102103
CLIError
103104
{ errorType = SymlinkErrorType (AlreadyExists path)
104105
, errorCode = ErrorCode 205
105-
, errorMessage = Text.show path <> " already exists"
106+
, errorMessage = Text.show path <> " already exists."
106107
}
107108

108109
pathIsNotSymlinkError :: OsPath -> CLIError
109110
pathIsNotSymlinkError path =
110111
CLIError
111112
{ errorType = SymlinkErrorType (IsNotSymlink path)
112113
, errorCode = ErrorCode 142
113-
, errorMessage = Text.show path <> " is not a symbolic link"
114+
, errorMessage = Text.show path <> " already exists and is not a symbolic link."
114115
}
115116

116117
wrongTargetError

src/Confer/Effect/Symlink.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ handleAlreadyExistingDestination sourcePath destinationPath = do
160160
metadata <- Directory.getFileMetadata destination
161161
pure $ Directory.fileTypeFromMetadata metadata
162162
destinationIsSymbolic <- liftIO $ Directory.pathIsSymbolicLink destinationPath
163-
if destinationIsSymbolic
163+
if not destinationIsSymbolic
164164
then do
165165
destinationTruePath <-
166166
liftIO $

0 commit comments

Comments
 (0)