diff --git a/app/hcat.hs b/app/hcat.hs index eef1e4d..c1b3966 100644 --- a/app/hcat.hs +++ b/app/hcat.hs @@ -3,4 +3,4 @@ module Main (main) where import HCat qualified main :: IO () -main = HCat.runHCat +main = HCat.defaultMain diff --git a/src/HCat.hs b/src/HCat.hs index 86d3f01..6be48ae 100644 --- a/src/HCat.hs +++ b/src/HCat.hs @@ -1,6 +1,32 @@ module HCat ( runHCat, + defaultMain, ) where +import System.Environment qualified as Env +import System.IO (hPutStrLn, stderr) + +import Control.Exception qualified as Exception +import System.IO.Error qualified as IOError + +parseArgs :: [String] -> Either String FilePath +parseArgs args = case args of + [] -> Left "No filename given!" + [arg] -> Right arg + _ -> Left "Only a single file is supported" + runHCat :: IO () -runHCat = pure () +runHCat = do + fileNameOrError <- parseArgs <$> Env.getArgs + fileName <- eitherToError fileNameOrError + readFile fileName >>= 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 + +defaultMain :: IO () +defaultMain = Exception.catch runHCat handleError