+{-# LANGUAGE OverloadedStrings #-}
+
module Main where
import Lib
import Data.List (intercalate)
import Text.JSON
import Text.JSON.Generic
-import Data.Text hiding (intercalate, unlines, init)
+import Data.Text hiding (intercalate, unlines, init, length)
+import Data.Time.Clock.POSIX
import System.File.Tree hiding (mapM, mapM_)
import System.Directory (doesFileExist)
main :: IO ()
main = getArgs >>= parse
+continueIfValidProject :: IO() -> IO ()
+continueIfValidProject nextfn = do
+ jsonExists <- doesFileExist "xyz.json"
+ if jsonExists
+ then nextfn
+ else putStrLn "This does not appear to be a valid project directory"
+
init :: IO ()
init = do
dirExists <- doesFileExist "xyz.json"
ununlines :: [String] -> String
ununlines = intercalate "\n\n"
+entry :: IO ()
+entry = do
+ putStrLn "Enter a name for this entry:"
+ entryName <- getLine
+ entryNum <- fmap ((+1) . length) $ allFilesIn "entries"
+ stamp <- fmap round getPOSIXTime
+ let filename = "entries/" ++ (show entryNum) ++ "-" ++ (unpack . (toLower. replace " " "-") $ pack entryName) ++ ".txt"
+ writeFile filename (show stamp ++ "::" ++ entryName ++ "\n")
+ putStrLn $ "Created " ++ filename
+
usage :: IO ()
usage = putStr . ununlines $ [
"usage: xyz <command>",
"Commands:", unlines [
"\tinit\t\tInitialise a new site",
- "\tbuild\t\tBuild the site"
+ "\tbuild\t\tBuild the site",
+ "\tentry\t\tInitialise an entry"
]
]
parse ["init"] = init >> exit
-parse ["build"] = build >> exit
+parse ["build"] = continueIfValidProject build >> exit
+parse ["entry"] = continueIfValidProject entry >> exit
parse [_] = usage >> exit
parse [] = usage >> exit