aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2015-03-21 18:57:23 +0900
committerSean Whitton <spwhitton@spwhitton.name>2015-03-21 18:57:23 +0900
commit9a1e75719481e68259e79b7bed6c6f500f0da86e (patch)
treeefa18725dede445ea59a6caa8c6653eb7d085fcb
parent61e7a904975518c6ab61589010dbf7b25fc21e48 (diff)
downloadsariulclocks-9a1e75719481e68259e79b7bed6c6f500f0da86e.tar.gz
tidy up use of IO in sariulccron main
-rw-r--r--src/Control/Monad/SariulClocks.hs8
-rw-r--r--src/sariulccron.hs17
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