aboutsummaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 9debfe1dd56ba8bce5b59bfb2d94fe11c2754a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{-# LANGUAGE OverloadedStrings #-}

module Main
  ( main
  ) where

import System.IO (stderr)

import qualified Data.Text as T
import qualified Data.Text.IO as T

import Date (getCurrentDate)
import Birthdate (readBirthdates, filterBirthday)
import Mail (mailSubject, mailBody)
import SendMail (sendMail)
import Config

birthdatePath :: FilePath
birthdatePath = "birthdates.csv"

configPath :: FilePath
configPath = "config.txt"

main :: IO ()
main = do
  eitherBirthdates <- readBirthdates birthdatePath
  eitherConfig <- getConfig configPath
  case (eitherBirthdates, eitherConfig) of
    (Left error, _) ->
      T.hPutStr stderr $
        T.concat
          [ "Error while parsing file "
          , T.pack birthdatePath
          , ":\n"
          , error
          ]
    (_, Left error) ->
      T.hPutStr stderr $
        T.concat
          [ "Error while parsing config file "
          , T.pack birthdatePath
          , ":\n"
          , error
          ]
    (Right birthdates, Right config) -> do
      currentDate <- getCurrentDate
      let birthdays = filterBirthday currentDate birthdates
      sendMail
        (mailTo config)
        (mailFrom config)
        (mailSubject birthdays)
        (mailBody currentDate birthdays)