fix(config): catch isDoesNotExistError for config file

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2023-10-26 14:35:10 +02:00
parent 27b8b686a7
commit 92852733b5
Signed by: cafkafk
GPG key ID: 26C542FD97F965CE

View file

@ -4,6 +4,8 @@ module Main where
-- import MonadRandom -- import MonadRandom
import Control.Monad.Random import Control.Monad.Random
import Control.Exception (catch)
import System.IO.Error (isDoesNotExistError)
import GHC.IO.Exception import GHC.IO.Exception
import Lib import Lib
import System.IO import System.IO
@ -92,12 +94,19 @@ performCleanup = do
_ <- system "rm wget-log 2>/dev/null" _ <- system "rm wget-log 2>/dev/null"
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 :: IO ()
main = do main = do
content <- BS.readFile "config.yaml" -- (4) content <- safeReadFile "config.yaml" -- (4)
let parsedContent = Y.decode content :: Maybe Cred -- (5) let parsedContent = Y.decode content :: Maybe Cred -- (5)
case parsedContent of case parsedContent of
Nothing -> putStrLn "[-] no config found" Nothing -> putStrLn "[-] no config values found"
(Just (Cred u p)) -> putStrLn ("[+] example: " ++ u ++ ", other: " ++ p) (Just (Cred u p)) -> putStrLn ("[+] example: " ++ u ++ ", other: " ++ p)
maid <- getRandomMaid maid <- getRandomMaid
putStrLn maid putStrLn maid