1 {-# LANGUAGE LambdaCase #-}
2 module Hcompta.CLI.Command.Balance where
4 import Control.Monad.IO.Class (liftIO)
5 import Control.Monad.Trans.Except (runExceptT)
6 import qualified Data.Either
7 import qualified Data.List
8 import System.Console.GetOpt
13 import System.Environment as Env (getProgName)
14 import System.Exit (exitWith, ExitCode(..))
15 import qualified System.IO as IO
17 import qualified Hcompta.CLI.Args as Args
18 import qualified Hcompta.CLI.Context as Context
19 import qualified Hcompta.CLI.Write as Write
20 import qualified Hcompta.Calc.Balance
21 import qualified Hcompta.Format.Ledger.Journal
22 import qualified Hcompta.Format.Ledger.Read
23 -- import qualified Hcompta.Format.Ledger.Write
27 { ctx_input :: [FilePath]
38 bin <- Env.getProgName
41 , " "++bin++" balance [option..]"
43 , usageInfo "OPTIONS" options
46 options :: Args.Options Ctx
50 usage >>= IO.hPutStr IO.stderr
51 exitWith ExitSuccess))
53 , Option "i" ["input"]
55 return $ ctx{ctx_input=s:ctx_input ctx}) "FILE")
56 "read data from given file"
59 run :: Context.Context -> [String] -> IO ()
61 (ctx, _) <- Args.parse context usage options (nil, args)
62 (flip mapM) (ctx_input ctx) $ \path -> do
64 Hcompta.Format.Ledger.Read.file path
66 Left ko -> return $ Left (path, ko)
67 Right ok -> return $ Right ok
68 >>= return . Data.Either.partitionEithers
71 (flip mapM_) kos $ \(_path, ko) ->
72 Write.fatal context $ ko
76 (\b j -> Hcompta.Calc.Balance.journal
77 (Hcompta.Format.Ledger.Journal.to_Model j) b)
78 Hcompta.Calc.Balance.nil