1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 module Hcompta.CLI.Command.Print where
5 import Control.Arrow (first)
6 import Control.Applicative ((<$>))
7 import Control.Monad.IO.Class (liftIO)
8 import Control.Monad.Trans.Except (runExceptT)
9 import qualified Data.Either
10 import System.Console.GetOpt
14 import System.Environment as Env (getProgName)
15 import System.Exit (exitWith, ExitCode(..))
16 import qualified System.IO as IO
18 import qualified Hcompta.CLI.Args as Args
19 import qualified Hcompta.CLI.Context as Context
20 import qualified Hcompta.CLI.Format.Ledger as CLI.Ledger
21 import qualified Hcompta.CLI.Write as Write
22 import qualified Hcompta.Format.Ledger.Journal as Ledger.Journal
23 import qualified Hcompta.Format.Ledger.Read as Ledger.Read
24 import qualified Hcompta.Format.Ledger.Write as Ledger.Write
25 import qualified Hcompta.Lib.Leijen as W
29 { ctx_input :: [FilePath]
42 bin <- Env.getProgName
45 , " "++bin++" print [option..]"
47 , usageInfo "OPTIONS" options
50 options :: Args.Options Ctx
53 (NoArg (\_context _ctx -> do
54 usage >>= IO.hPutStr IO.stderr
55 exitWith ExitSuccess))
57 , Option "i" ["input"]
58 (ReqArg (\s _context ctx -> do
59 return $ ctx{ctx_input=s:ctx_input ctx}) "FILE")
60 "read data from given file, can be use multiple times"
62 (OptArg (\arg context ctx -> do
63 ctx_align <- case arg of
64 Nothing -> return $ True
65 Just "yes" -> return $ True
66 Just "no" -> return $ False
67 Just _ -> Write.fatal context $
68 W.text "--align option expects \"yes\", or \"no\" as value"
69 return $ ctx{ctx_align})
74 run :: Context.Context -> [String] -> IO ()
77 first (\ctx -> ctx{ctx_input=reverse $ ctx_input ctx}) <$>
78 Args.parse context usage options (nil, args)
80 CLI.Ledger.paths context $ ctx_input ctx
81 >>= do mapM $ \path -> do
85 Left ko -> return $ Left (path, ko)
86 Right ok -> return $ Right ok
87 >>= return . Data.Either.partitionEithers
90 (flip mapM_) kos $ \(_path, ko) -> Write.fatal context ko
92 style_color <- Write.with_color context IO.stdout
93 let sty = Ledger.Write.Style
94 { Ledger.Write.style_align = ctx_align ctx
95 , Ledger.Write.style_color
97 let journal = Ledger.Journal.flatten $ Ledger.Journal.unions journals
98 Ledger.Write.put sty IO.stdout $ do
99 Ledger.Write.journal journal