-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: Modify initConfig to pass a custom getLine
- Loading branch information
1 parent
e8ec435
commit b344cd3
Showing
5 changed files
with
124 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,41 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module System.Hapistrano.InitSpec (spec) where | ||
|
||
import Data.List (isInfixOf) | ||
|
||
import Test.Hspec | ||
|
||
import System.Directory (doesFileExist, getCurrentDirectory, withCurrentDirectory) | ||
import System.FilePath ((</>)) | ||
import System.Hapistrano (initConfig) | ||
import System.Hapistrano.Internal (initConfig') | ||
import System.IO.Temp (withSystemTempDirectory) | ||
|
||
getLine' :: String -> IO String | ||
getLine' = return . go | ||
where | ||
go s | ||
| "repo" `isInfixOf` s = show "git@github.com:stackbuilders/hapistrano.git" | ||
| "revision" `isInfixOf` s = show "origin/master" | ||
| "host" `isInfixOf` s = show "user@localhost.com" | ||
| "port" `isInfixOf` s = "22" | ||
| "restart Command" `isInfixOf` s = show "n" | ||
| otherwise = show s | ||
|
||
spec :: Spec | ||
spec = do | ||
spec = | ||
describe "initConfig" $ do | ||
it "should create a file when missing" $ | ||
withSystemTempDirectory "hapistrano-spec-initConfig-missing" $ \tempDir -> | ||
withCurrentDirectory tempDir $ do | ||
configFilePath <- (</> "hap.yml") <$> getCurrentDirectory | ||
initConfig $ return "" | ||
initConfig' $ const . return $ "" | ||
doesFileExist configFilePath `shouldReturn` True | ||
|
||
it "should create a file when missing" $ | ||
withSystemTempDirectory "hapistrano-spec-initConfig-missing" $ \tempDir -> | ||
withCurrentDirectory tempDir $ do | ||
configFilePath <- (</> "hap.yml") <$> getCurrentDirectory | ||
initConfig' getLine' | ||
content <- readFile configFilePath | ||
content `shouldContain` "user@localhost.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
module System.Hapistrano.Internal (initConfig') where | ||
|
||
import Control.Monad (when) | ||
import Control.Monad.IO.Class (MonadIO(..)) | ||
|
||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as T | ||
|
||
import qualified Data.Yaml as Yaml | ||
|
||
import System.IO (stderr) | ||
import System.Exit (exitFailure) | ||
import qualified System.FilePath as FilePath | ||
import qualified System.Directory as Directory | ||
|
||
import System.Hapistrano.Types (InitTemplateConfig(..), defaultInitTemplateConfig) | ||
|
||
|
||
-- | Create a file with an initial config file by getting information from the | ||
-- user. | ||
initConfig' :: forall m. MonadIO m => (String -> m String) -> m () | ||
initConfig' getLine' = do | ||
configFilePath <- (FilePath.</> "hap.yml") <$> liftIO Directory.getCurrentDirectory | ||
alreadyExisting <- liftIO $ Directory.doesFileExist configFilePath | ||
when alreadyExisting $ liftIO $ do | ||
T.hPutStrLn stderr "'hap.yml' already exists" | ||
exitFailure | ||
liftIO $ putStrLn "Creating 'hap.yml'" | ||
defaults <- liftIO defaultInitTemplateConfig | ||
let prompt :: Read a => String -> a -> m a | ||
prompt title d = do | ||
liftIO $ T.putStrLn $ T.pack $ title <> "?: " | ||
x <- getLine' title | ||
liftIO $ putStrLn $ "Value of X: " <> x | ||
return $ | ||
if null x | ||
then d | ||
else read x | ||
prompt' :: Read a => String -> (InitTemplateConfig -> T.Text) -> (InitTemplateConfig -> a) -> m a | ||
prompt' title f fd = prompt title (fd defaults) | ||
|
||
let yesNo :: a -> a -> T.Text -> a | ||
yesNo t f x = if x == "y" then t else f | ||
|
||
config <- | ||
InitTemplateConfig | ||
<$> prompt' "repo" repo repo | ||
<*> prompt' "revision" revision revision | ||
<*> prompt' "host" host host | ||
<*> prompt' "port" (T.pack . show . port) port | ||
<*> return (buildScript defaults) | ||
<*> fmap (yesNo (restartCommand defaults) Nothing) (prompt' "Include restart command" (const "Y/n") (const "y")) | ||
|
||
liftIO $ do | ||
Yaml.encodeFile configFilePath config | ||
putStrLn $ "Configuration written at " <> configFilePath |