From 9a1e75719481e68259e79b7bed6c6f500f0da86e Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 21 Mar 2015 18:57:23 +0900 Subject: tidy up use of IO in sariulccron main --- src/Control/Monad/SariulClocks.hs | 8 ++++++-- src/sariulccron.hs | 17 +++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Control/Monad/SariulClocks.hs b/src/Control/Monad/SariulClocks.hs index e25add2..d84bec3 100644 --- a/src/Control/Monad/SariulClocks.hs +++ b/src/Control/Monad/SariulClocks.hs @@ -10,11 +10,12 @@ module Control.Monad.SariulClocks ( SariulScoresMonad , getSession , putScores , getScores - , modifyScores) where + , modifyScores + , printLn) where import Control.Monad (liftM) import Control.Monad.State (MonadState, StateT, evalStateT, get, put) -import Control.Monad.Trans (MonadIO, lift) +import Control.Monad.Trans (MonadIO, lift, liftIO) import Data.Classes import Network.CGI (CGIResult, handleErrors, runCGI) import Network.CGI.Monad (CGIT, MonadCGI, cgiAddHeader, cgiGet) @@ -70,3 +71,6 @@ runSariulClocksCGI k = runSariulClocksIO :: SariulClocksIO () -> IO () runSariulClocksIO k = evalStateT (getSCI k) zeroScores + +printLn :: String -> SariulClocksIO () +printLn x = liftIO $ putStrLn $ x diff --git a/src/sariulccron.hs b/src/sariulccron.hs index d36258e..3adaf5a 100644 --- a/src/sariulccron.hs +++ b/src/sariulccron.hs @@ -17,15 +17,17 @@ weeklyCron scores = undefined main :: IO () main = runSariulClocksIO $ do scores <- readScoresFile + -- Proceed only if we actually read some scores. when (isJust scores) $ do modifyScores weeklyCron shouldModify <- liftM (((/=) scores) . Just) getScores - liftIO $ putStrLn "Scores before:\n" - liftIO $ putStrLn . ppScores $ fromJust scores - liftIO $ putStrLn "Scores after:\n" - scores' <- getScores - liftIO $ putStrLn . ppScores $ scores' - when shouldModify writeScoresFile + + when shouldModify $ do + writeScoresFile + + -- Output what we did to be e-mailed from crond. + liftM (scoresBeforeAfter (fromJust scores)) getScores + >>= printLn --- utility functions @@ -45,6 +47,9 @@ classBox (c, (Score x y)) = hsep 3 left , alignHoriz center1 7 $ (text . show) x , alignHoriz center1 12 $ (text . show) y] +scoresBeforeAfter :: ScoresList -> ScoresList -> String +scoresBeforeAfter x y = "Scores before:\n\n" ++ ppScores x ++ "\nScores after:\n\n" ++ ppScores y + isJust :: Maybe a -> Bool isJust (Just _) = True isJust _ = False -- cgit v1.2.3