module Main where import Control.Monad ((<=<)) import Data.ItCli import ItCli import System.Exit import Options import Control.Monad.Except (throwError, liftIO) import Control.Monad.Writer import System.FilePath (()) import qualified Data.UUID as UUID runCommands :: ItCliCommand -> IO () runCommands Init = initItCliDir runCommands Version = putStrLn "0.1.8.3" runCommands (ListIssues filters) = runProgram $ listIssues filters runCommands (Open title) = runProgram $ openIssue title >> return () runCommands (Close iid) = runProgram $ closeIssue iid runCommands (CommentCommand iid cmd) = runProgram $ addComment iid cmd runCommands (ShowIssue iid) = runProgram $ showIssue iid runCommands (Branch iid) = runProgram $ makeBranchName iid >>= tell . (:[]) runCommands (CommitMessgae iid) = runProgram $ makeCommitMessage iid >>= tell . (:[]) runCommands (IssuePath showRelative iid) = runProgram $ getIssueIdFromArg iid >>= mkPath showRelative >>= tell . (:[]) where mkPath False iid = getIssueBaseDir iid mkPath True iid = return $ ".itcli" (UUID.toString iid) runProgram :: ItCliM () -> IO () runProgram prg = do (r, logs) <- runItCliM prg printLogs logs either handleError return r handleError :: String -> IO () handleError msg = putStrLn msg >> exitWith (ExitFailure 1) printLogs :: Logs -> IO () printLogs = mapM_ putStrLn runTests :: IO () runTests = runProgram $ do iid <- display <$> (openIssue "a test issue") sequence_ [ addComment iid (CommentMessage "a new message") , addComment iid (CommentMessage "a new new message") , showIssue iid ] bd <- getIssueBaseDir =<< getIssueIdFromArg iid liftIO $ putStrLn =<< readFile (bd ++ "/meta") liftIO $ putStrLn $ "\n" liftIO $ putStrLn =<< readFile (bd ++ "/comments") main :: IO () main = parseProgArgs >>= runCommands