Skip to content

Commit

Permalink
feat: add --dry-run flag (#226) (#246)
Browse files Browse the repository at this point in the history
Co-authored-by: Cristhian Motoche <[email protected]>
  • Loading branch information
blackheaven and CristhianMotoche authored Oct 18, 2024
1 parent 159e7a1 commit 745b439
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
7 changes: 6 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ optionParser = Opts
<> metavar "PATH"
<> showDefault
<> help "Configuration file to use" )
<*> flag
False
True
( long "dry-run"
<> help "Display command (without running them)" )

deployParser :: Parser Command
deployParser = Deploy
Expand Down Expand Up @@ -153,7 +158,7 @@ runHapCmd Opts{..} hapCmd = do
let printFnc dest str = atomically $
writeTChan chan (PrintMsg dest str)
hap shell sshOpts executionMode = do
r <- Hap.runHapistrano sshOpts shell printFnc $ hapCmd hapConfig executionMode
r <- Hap.runHapistrano optsDryRun sshOpts shell printFnc $ hapCmd hapConfig executionMode
atomically (writeTChan chan FinishMsg)
return r
printer :: Int -> IO ()
Expand Down
2 changes: 1 addition & 1 deletion spec/System/HapistranoSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ runHapWithShell shell m = do
case dest of
StdoutDest -> putStr str
StderrDest -> hPutStr stderr str
r <- Hap.runHapistrano Nothing shell printFnc m
r <- Hap.runHapistrano False Nothing shell printFnc m
case r of
Left n -> do
expectationFailure ("Failed with status code: " ++ show n)
Expand Down
6 changes: 4 additions & 2 deletions src/System/Hapistrano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,21 @@ import qualified Text.Megaparsec.Char as M
-- | Run the 'Hapistrano' monad. The monad hosts 'exec' actions.
runHapistrano ::
MonadIO m
=> Maybe SshOptions -- ^ SSH options to use or 'Nothing' if we run locally
=> Bool -- ^ Is running in dry run
-> Maybe SshOptions -- ^ SSH options to use or 'Nothing' if we run locally
-> Shell -- ^ Shell to run commands
-> (OutputDest -> String -> IO ()) -- ^ How to print messages
-> Hapistrano a -- ^ The computation to run
-> m (Either Int a) -- ^ Status code in 'Left' on failure, result in
-- 'Right' on success
runHapistrano sshOptions shell' printFnc m =
runHapistrano isDryRun sshOptions shell' printFnc m =
liftIO $ do
let config =
Config
{ configSshOptions = sshOptions
, configShellOptions = shell'
, configPrint = printFnc
, configDryRun = isDryRun
}
r <- try @HapistranoException $ unHapistrano m config
case r of
Expand Down
39 changes: 22 additions & 17 deletions src/System/Hapistrano/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,28 @@ exec' ::
-> Hapistrano String -- ^ Raw stdout output of that program
exec' cmd readProcessOutput maybeRelease = do
Config {..} <- ask
time <- liftIO getZonedTime
let timeStampFormat = "%T, %F (%Z)"
printableTime = formatTime defaultTimeLocale timeStampFormat time
hostLabel =
case configSshOptions of
Nothing -> "localhost"
Just SshOptions {..} -> sshHost ++ ":" ++ show sshPort
hostInfo = colorizeString Blue $ putLine hostLabel
timestampInfo = colorizeString Cyan ("[" ++ printableTime ++ "] INFO -- : $ ")
cmdInfo = colorizeString Green (cmd ++ "\n")
liftIO $ configPrint StdoutDest (hostInfo ++ timestampInfo ++ cmdInfo)
(exitCode', stdout', stderr') <- liftIO readProcessOutput
unless (null stdout') . liftIO $ configPrint StdoutDest stdout'
unless (null stderr') . liftIO $ configPrint StderrDest stderr'
case exitCode' of
ExitSuccess -> return stdout'
ExitFailure n -> failWith n Nothing maybeRelease
if configDryRun
then do
liftIO $ configPrint StderrDest $ "[Dry run] " <> cmd
return ""
else do
time <- liftIO getZonedTime
let timeStampFormat = "%T, %F (%Z)"
printableTime = formatTime defaultTimeLocale timeStampFormat time
hostLabel =
case configSshOptions of
Nothing -> "localhost"
Just SshOptions {..} -> sshHost ++ ":" ++ show sshPort
hostInfo = colorizeString Blue $ putLine hostLabel
timestampInfo = colorizeString Cyan ("[" ++ printableTime ++ "] INFO -- : $ ")
cmdInfo = colorizeString Green (cmd ++ "\n")
liftIO $ configPrint StdoutDest (hostInfo ++ timestampInfo ++ cmdInfo)
(exitCode', stdout', stderr') <- liftIO readProcessOutput
unless (null stdout') . liftIO $ configPrint StdoutDest stdout'
unless (null stderr') . liftIO $ configPrint StderrDest stderr'
case exitCode' of
ExitSuccess -> return stdout'
ExitFailure n -> failWith n Nothing maybeRelease

-- | Put something “inside” a line, sort-of beautifully.
putLine :: String -> String
Expand Down
2 changes: 2 additions & 0 deletions src/System/Hapistrano/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ data Config =
-- ^ One of the supported 'Shell's
, configPrint :: !(OutputDest -> String -> IO ())
-- ^ How to print messages
, configDryRun :: !Bool
}

-- | The source of the repository. It can be from a version control provider
Expand Down Expand Up @@ -189,6 +190,7 @@ data MaintenanceOptions = Enable | Disable
data Opts = Opts
{ optsCommand :: Command
, optsConfigFile :: FilePath
, optsDryRun :: Bool
}

-- | Command to execute and command-specific options.
Expand Down

0 comments on commit 745b439

Please sign in to comment.