Skip to content

Commit

Permalink
feat: add --dry-run flag (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackheaven committed Oct 1, 2024
1 parent ccb8d7c commit b6f6b99
Show file tree
Hide file tree
Showing 5 changed files with 36 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 @@ -61,6 +61,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 @@ -126,7 +131,7 @@ main = do
let printFnc dest str = atomically $
writeTChan chan (PrintMsg dest str)
hap shell sshOpts executionMode = do
r <- Hap.runHapistrano sshOpts shell printFnc $
r <- Hap.runHapistrano optsDryRun sshOpts shell printFnc $
case optsCommand of
Deploy cliReleaseFormat cliKeepReleases cliKeepOneFailed ->
Hap.deploy
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
7 changes: 5 additions & 2 deletions src/System/Hapistrano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Control.Exception (try)
import Control.Monad
import Control.Monad.Catch (catch, throwM)
import Control.Monad.Except
import Control.Monad.IO.Class
import Control.Monad.Reader (local)
import Data.List (dropWhileEnd, genericDrop, sortOn)
import Data.Maybe (fromMaybe, mapMaybe)
Expand All @@ -62,19 +63,21 @@ import Text.Read (readMaybe)
-- | 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 @@ -138,23 +138,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
case configDryRun of
True -> do
liftIO $ configPrint StderrDest $ "[Dry run] " <> cmd
return ""
False -> 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 @@ -81,6 +81,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 @@ -182,6 +183,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 b6f6b99

Please sign in to comment.