83 lines
2.4 KiB
Haskell
83 lines
2.4 KiB
Haskell
{-# LANGUAGE NamedFieldPuns #-}
|
|
module Arguments (
|
|
Arguments(..)
|
|
, get
|
|
) where
|
|
|
|
import Data.Monoid ((<>))
|
|
import Options.Applicative
|
|
import System.Directory (doesDirectoryExist, doesFileExist, makeAbsolute)
|
|
import System.Exit (die)
|
|
import System.FilePath.Posix (dropTrailingPathSeparator, isValid)
|
|
|
|
data Arguments = Arguments {
|
|
sourceDir :: FilePath
|
|
, blogName :: Maybe String
|
|
, previewCountArg :: Int
|
|
, bannerPath :: Maybe FilePath
|
|
, headPath :: Maybe FilePath
|
|
}
|
|
|
|
parseArguments :: Parser Arguments
|
|
parseArguments = Arguments
|
|
<$> argument filePath (metavar "INPUT_DIR")
|
|
<*> option (optional str) (
|
|
metavar "BLOG_NAME"
|
|
<> value Nothing
|
|
<> short 'n'
|
|
<> long "name"
|
|
<> help "name of the blog"
|
|
)
|
|
<*> option auto (
|
|
metavar "PREVIEW_COUNT"
|
|
<> value 3
|
|
<> short 'p'
|
|
<> long "preview-count"
|
|
<> help "number of articles listed on the page of each category"
|
|
)
|
|
<*> option (optional filePath) (
|
|
metavar "BANNER_PATH"
|
|
<> value Nothing
|
|
<> short 'b'
|
|
<> long "banner"
|
|
<> help "path to the file to use for the blog's banner"
|
|
)
|
|
<*> option (optional filePath) (
|
|
metavar "HEAD_PATH"
|
|
<> value Nothing
|
|
<> short 'H'
|
|
<> long "head"
|
|
<> help "path to the file to add in the blog's head"
|
|
)
|
|
|
|
filePath :: ReadM FilePath
|
|
filePath = eitherReader $ \path ->
|
|
if isValid path
|
|
then Right $ dropTrailingPathSeparator path
|
|
else Left "This string doesn't represent a valid path"
|
|
|
|
ifNotDie :: (a -> IO Bool) -> (a -> String) -> a -> IO ()
|
|
ifNotDie check messageBuilder input = do
|
|
bool <- check input
|
|
if bool
|
|
then return ()
|
|
else die $ messageBuilder input
|
|
|
|
get :: IO Arguments
|
|
get = do
|
|
arguments <- execParser $ info (parseArguments <**> helper) fullDesc
|
|
doesDirectoryExist `ifNotDie` noSuchDirectory $ sourceDir arguments
|
|
absoluteSourceDir <- makeAbsolute $ sourceDir arguments
|
|
mapM_ (doesFileExist `ifNotDie` noSuchFile) $ bannerPath arguments
|
|
absoluteBannerPath <- mapM makeAbsolute $ bannerPath arguments
|
|
mapM_ (doesFileExist `ifNotDie` noSuchFile) $ headPath arguments
|
|
absoluteHeadPath <- mapM makeAbsolute $ headPath arguments
|
|
return $ arguments {
|
|
sourceDir = absoluteSourceDir
|
|
, bannerPath = absoluteBannerPath
|
|
, headPath = absoluteHeadPath
|
|
}
|
|
where
|
|
noSuchDirectory = (++ ": no such directory")
|
|
noSuchFile = (++ ": no such file")
|