1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 module Hcompta.CLI.Command.Print where
6 import Prelude hiding (foldr)
7 -- import Control.Arrow (first)
8 -- import Control.Applicative ((<$>))
9 -- import Control.Monad ((>=>))
10 import Control.Monad.IO.Class (liftIO)
11 import Control.Monad.Trans.Except (runExceptT)
12 import qualified Data.Either
13 import Data.Foldable (foldr)
14 import System.Console.GetOpt
18 import System.Environment as Env (getProgName)
19 import System.Exit (exitWith, ExitCode(..))
20 import qualified System.IO as IO
22 import qualified Hcompta.CLI.Args as Args
23 import qualified Hcompta.CLI.Context as Context
24 import qualified Hcompta.CLI.Format.Ledger as CLI.Ledger
25 import qualified Hcompta.CLI.Write as Write
26 import qualified Hcompta.Format.Ledger as Ledger
27 import qualified Hcompta.Format.Ledger.Journal as Ledger.Journal
28 import qualified Hcompta.Format.Ledger.Read as Ledger.Read
29 import qualified Hcompta.Format.Ledger.Write as Ledger.Write
30 import qualified Hcompta.Lib.Leijen as W
31 import qualified Hcompta.Model.Filter as Filter
32 import qualified Hcompta.Model.Filter.Read as Filter.Read
36 { ctx_input :: [FilePath]
49 bin <- Env.getProgName
52 , " "++bin++" print [option..]"
54 , usageInfo "OPTIONS" options
57 options :: Args.Options Ctx
60 (NoArg (\_context _ctx -> do
61 usage >>= IO.hPutStr IO.stderr
62 exitWith ExitSuccess))
64 , Option "i" ["input"]
65 (ReqArg (\s _context ctx -> do
66 return $ ctx{ctx_input=s:ctx_input ctx}) "FILE")
67 "read data from given file, can be use multiple times"
69 (OptArg (\arg context ctx -> do
70 ctx_align <- case arg of
71 Nothing -> return $ True
72 Just "yes" -> return $ True
73 Just "no" -> return $ False
74 Just _ -> Write.fatal context $
75 W.text "--align option expects \"yes\", or \"no\" as value"
76 return $ ctx{ctx_align})
81 run :: Context.Context -> [String] -> IO ()
83 (ctx, text_filters) <- Args.parse context usage options (nil, args)
85 CLI.Ledger.paths context $ ctx_input ctx
88 liftIO $ runExceptT $ Ledger.Read.file path
90 Left ko -> return $ Left (path, ko)
91 Right ok -> return $ Right ok
92 >>= return . Data.Either.partitionEithers
94 (errs@(_:_), _journals) ->
95 (flip mapM_) errs $ \(_path, err) -> do
96 Write.fatal context $ err
98 (filters::[Filter.Test_Bool (Filter.Test_Transaction Ledger.Transaction)]) <-
99 (flip mapM) text_filters $ \s ->
100 liftIO $ Filter.Read.read Filter.Read.test_transaction s
102 Left ko -> Write.fatal context $ ko
103 Right ok -> return ok
104 Write.debug context $ "transaction_filter: " ++ show filters
105 style_color <- Write.with_color context IO.stdout
106 let sty = Ledger.Write.Style
107 { Ledger.Write.style_align = ctx_align ctx
108 , Ledger.Write.style_color
117 (foldr Filter.And Filter.Any filters) tr of
121 . Ledger.journal_transactions))
124 Ledger.Write.put sty IO.stdout $ do
125 Ledger.Write.transactions transactions