pilu/src/Main.hs

36 lines
1.0 KiB
Haskell
Raw Permalink Normal View History

{-# LANGUAGE NamedFieldPuns #-}
module Main where
import CLI (Invocation(..), Mode(..), invoked)
2019-04-07 18:08:22 +02:00
import CSV (Row, parse)
import Data.Time (Day, UTCTime(..), getCurrentTime)
import Medicine (Pharmacy, pharmacy)
import System.Exit (exitFailure)
import Schedule (schedule)
import Timeline (State(..), stateAt)
import YAML (encode)
2019-04-07 18:08:22 +02:00
readCSV :: Row a => FilePath -> IO [a]
readCSV filePath = do
parsed <- parse filePath '\t' <$> readFile filePath
case parsed of
Left e -> (putStrLn $ show e) >> exitFailure
Right rows -> return rows
getState :: Pharmacy -> Maybe Day -> IO State
getState aPharmacy atDate =
stateAt
<$> maybe (utctDay <$> getCurrentTime) return atDate
<*> return aPharmacy
2019-04-07 18:08:22 +02:00
<*> readCSV "timeline.csv"
main :: IO ()
2019-04-07 18:08:22 +02:00
main = do
invocation <- invoked
thePharmacy <- pharmacy <$> readCSV "medicine.csv"
theState <- getState thePharmacy $ date invocation
let display = case mode invocation of
Inventory -> encode
Schedule -> encode . schedule thePharmacy
putStrLn $ display theState