Implement entry command
authorCameron Ball <cameron@moodle.com>
Wed, 12 Jun 2019 03:31:53 +0000 (11:31 +0800)
committerCameron Ball <cameron@moodle.com>
Wed, 12 Jun 2019 03:31:53 +0000 (11:31 +0800)
app/Main.hs

index 742b739..0146ae9 100644 (file)
@@ -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 <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