From e74eadd6ba15daa73cc435e7af208fffc947ddf7 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Fri, 23 Oct 2020 15:36:49 +0200 Subject: [PATCH] Stop dying in Path validation and return an Either instead so we can handle expected errors cleanly --- src/Blog.hs | 3 ++- src/Blog/Path.hs | 15 +++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Blog.hs b/src/Blog.hs index 6de4108..246181b 100644 --- a/src/Blog.hs +++ b/src/Blog.hs @@ -41,6 +41,7 @@ import Page (Page) import qualified Page (at) import Prelude hiding (lookup) import System.Directory (doesFileExist, withCurrentDirectory) +import System.Exit (die) import System.FilePath ((), dropTrailingPathSeparator, takeExtension, takeFileName) import Text.Parsec (ParseError) @@ -115,7 +116,7 @@ build arguments = do templates <- Template.build wording root <- Files.absolute . Dir $ Arguments.sourceDir arguments withCurrentDirectory root $ do - path <- Path.build root arguments + path <- either die return =<< Path.build root arguments let name = maybe (takeFileName $ dropTrailingPathSeparator root) id $ Arguments.name arguments skin <- Skin.build name arguments diff --git a/src/Blog/Path.hs b/src/Blog/Path.hs index 3f40db5..aa60a2f 100644 --- a/src/Blog/Path.hs +++ b/src/Blog/Path.hs @@ -12,14 +12,13 @@ import Data.Aeson (ToJSON(..), (.=), pairs) import Data.Monoid ((<>)) import Files (File(..), filePath, filePathMaybe) import GHC.Generics (Generic) -import System.Exit (die) data Path = Path { articlesPath :: Maybe FilePath , pagesPath :: Maybe FilePath , remarkableConfig :: Maybe FilePath , root :: FilePath - } deriving Generic + } deriving (Eq, Generic, Show) instance ToJSON Path where toEncoding (Path {articlesPath, pagesPath}) = pairs ( @@ -31,14 +30,14 @@ getMarkdownPath :: FilePath -> Maybe FilePath -> IO (Maybe FilePath) getMarkdownPath defaultPath Nothing = filePathMaybe (Dir defaultPath) getMarkdownPath _ (Just path) = Just <$> filePath (Dir path) -build :: FilePath -> Arguments -> IO Path +build :: FilePath -> Arguments -> IO (Either String Path) build root arguments = do articlesPath <- getMarkdownPath "articles" $ Arguments.articlesPath arguments pagesPath <- getMarkdownPath "pages" $ Arguments.pagesPath arguments - checkForContent articlesPath pagesPath remarkableConfig <- mapM (filePath . File) $ Arguments.remarkableConfig arguments - return $ Path {articlesPath, pagesPath, remarkableConfig, root} + checkForContent articlesPath pagesPath remarkableConfig where - checkForContent Nothing Nothing = - die "No articles ? No pages ? Why did you wake me up ? I'm going back to sleep" - checkForContent _ _ = return () + checkForContent Nothing Nothing _ = return $ + Left "No articles ? No pages ? Why did you wake me up ? I'm going back to sleep" + checkForContent articlesPath pagesPath remarkableConfig = + return . Right $ Path {articlesPath, pagesPath, remarkableConfig, root}