From a05e5965f23b2b3c4c8a0c40d387b35bf4166e0d Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Wed, 12 Jun 2019 11:31:53 +0800 Subject: [PATCH] Implement entry command --- app/Main.hs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 742b739..0146ae9 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE OverloadedStrings #-} + module Main where import Lib @@ -8,13 +10,21 @@ import Prelude hiding (init) 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" @@ -46,17 +56,29 @@ build = do 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 ", "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 -- 2.11.0