63 lines
2.4 KiB
Haskell
63 lines
2.4 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE NoFieldSelectors #-}
|
|
|
|
module Laskutin.Options (Options(..), SendOptions(..), Command(..), parseOptions) where
|
|
|
|
import Data.Text (Text)
|
|
import Data.Time (Day)
|
|
import Data.MIME (Address, address, defaultCharsets, parse)
|
|
|
|
import qualified Data.Text as T
|
|
import qualified Data.Text.Encoding as T
|
|
|
|
import Options.Applicative
|
|
|
|
import Laskutin.Types
|
|
|
|
data Options = Options
|
|
{ csv :: FilePath
|
|
, command :: Command
|
|
}
|
|
|
|
data Command = Send SendOptions
|
|
| UpdateTable FilePath
|
|
| Status
|
|
|
|
data SendOptions = SendOptions
|
|
{ account :: IBAN
|
|
, recipient :: Text
|
|
, subject :: Text
|
|
, message :: Text
|
|
, due :: Maybe Day
|
|
, email :: Address
|
|
, sendmail :: FilePath
|
|
, reminders :: Bool
|
|
}
|
|
|
|
parseOptions :: IO Options
|
|
parseOptions = execParser $ info (options <**> helper)
|
|
(fullDesc <> progDesc "Send email invoices from CSV" <> header "laskutin - email invoice sender")
|
|
|
|
options :: Parser Options
|
|
options = Options
|
|
<$> strOption (long "file" <> help "Path to the csv file" <> metavar "FILE")
|
|
<*> subparser
|
|
(command "send" (info (sendOptions <**> helper) (progDesc "Send email invoices from CSV"))
|
|
<> command "update" (info (updateTableOptions <**> helper) (progDesc "Update invoice CSV from a bank CSV"))
|
|
<> command "status" (info (pure Status) (progDesc "Print the status of the invoices")))
|
|
|
|
updateTableOptions :: Parser Command
|
|
updateTableOptions = UpdateTable <$> strOption (long "bank-csv" <> metavar "FILE")
|
|
|
|
sendOptions :: Parser Command
|
|
sendOptions = fmap Send $ SendOptions
|
|
<$> option (maybeReader $ readIBAN . T.pack) (long "iban" <> help "IBAN account" <> metavar "IBAN")
|
|
<*> strOption (long "recipient" <> help "The recipient of the payments" <> metavar "TEXT")
|
|
<*> strOption (long "subject" <> help "Subject of the invoice" <> metavar "TEXT")
|
|
<*> strOption (long "message" <> help "Additional message" <> value "" <> metavar "TEXT")
|
|
<*> optional (option auto (long "due" <> help "Due date in YYYY-MM-DD format" <> metavar "DATE"))
|
|
<*> option (eitherReader $ parse (address defaultCharsets) . T.encodeUtf8 . T.pack)
|
|
(long "email" <> help "Invoice sender email address" <> metavar "EMAIL")
|
|
<*> strOption (long "sendmail" <> short 'm' <> metavar "FILE" <> help "The sendmail program to use")
|
|
<*> switch (long "reminders" <> short 'r' <> help "Send reminder emails")
|