pilu/src/Main.hs

36 lines
1.0 KiB
Haskell

{-# LANGUAGE NamedFieldPuns #-}
module Main where
import CLI (Invocation(..), Mode(..), invoked)
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)
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
<*> readCSV "timeline.csv"
main :: IO ()
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