From 92852733b5410b5d7588bb5134afec721c9d9ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Thu, 26 Oct 2023 14:35:10 +0200 Subject: [PATCH] fix(config): catch `isDoesNotExistError` for config file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christina Sørensen --- app/Main.hs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 67b5ab6..48e8376 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -4,6 +4,8 @@ module Main where -- import MonadRandom import Control.Monad.Random +import Control.Exception (catch) +import System.IO.Error (isDoesNotExistError) import GHC.IO.Exception import Lib import System.IO @@ -92,12 +94,19 @@ performCleanup = do _ <- system "rm wget-log 2>/dev/null" system "rm wget-log.* 2>/dev/null" +safeReadFile :: FilePath -> IO BS.ByteString +safeReadFile path = (BS.readFile path) `catch` handleDoesNotExist + where + handleDoesNotExist e + | isDoesNotExistError e = return BS.empty + | otherwise = ioError e + main :: IO () main = do - content <- BS.readFile "config.yaml" -- (4) + content <- safeReadFile "config.yaml" -- (4) let parsedContent = Y.decode content :: Maybe Cred -- (5) case parsedContent of - Nothing -> putStrLn "[-] no config found" + Nothing -> putStrLn "[-] no config values found" (Just (Cred u p)) -> putStrLn ("[+] example: " ++ u ++ ", other: " ++ p) maid <- getRandomMaid putStrLn maid