Move code to Internal module and add tests
This commit is contained in:
parent
2ac76cb650
commit
ace38dcebc
6 changed files with 89 additions and 15 deletions
17
src/HCat.hs
17
src/HCat.hs
|
|
@ -9,27 +9,22 @@ import System.IO (hPutStrLn, stderr)
|
|||
import Control.Exception qualified as Exception
|
||||
import System.IO.Error qualified as IOError
|
||||
|
||||
import Data.Text qualified as Text
|
||||
import Data.Text.IO qualified as TextIO
|
||||
|
||||
parseArgs :: [String] -> Either String FilePath
|
||||
parseArgs args = case args of
|
||||
[] -> Left "No filename given!"
|
||||
[arg] -> Right arg
|
||||
_ -> Left "Only a single file is supported"
|
||||
import HCat.Internal (parseArgs)
|
||||
|
||||
runHCat :: IO ()
|
||||
runHCat = do
|
||||
fileNameOrError <- parseArgs <$> Env.getArgs
|
||||
fileName <- eitherToError fileNameOrError
|
||||
TextIO.readFile fileName >>= TextIO.putStr
|
||||
fileNameOrError <- parseArgs <$> Env.getArgs
|
||||
fileName <- eitherToError fileNameOrError
|
||||
TextIO.readFile fileName >>= TextIO.putStr
|
||||
|
||||
eitherToError :: Show a => Either a b -> IO b
|
||||
eitherToError = either (Exception.throwIO . IOError.userError . show) return
|
||||
|
||||
handleError :: IOError -> IO ()
|
||||
handleError e = do
|
||||
hPutStrLn stderr $ "I ran into an issue: " <> show e
|
||||
|
||||
hPutStrLn stderr $ "I ran into an issue: " <> show e
|
||||
|
||||
defaultMain :: IO ()
|
||||
defaultMain = Exception.catch runHCat handleError
|
||||
|
|
|
|||
8
src/HCat/Internal.hs
Normal file
8
src/HCat/Internal.hs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
-- | Internal module in order to facilitate testability.
|
||||
module HCat.Internal where
|
||||
|
||||
parseArgs :: [String] -> Either String FilePath
|
||||
parseArgs args = case args of
|
||||
[] -> Left "No filename given!"
|
||||
[arg] -> Right arg
|
||||
_ -> Left "Only a single file is supported"
|
||||
Loading…
Add table
Add a link
Reference in a new issue