{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands.Import (
importmode
,importcmd
)
where
import Control.Monad
import Data.List
import qualified Data.Text.IO as T
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Cli.Commands.Add (journalAddTransaction)
import System.Console.CmdArgs.Explicit
import Text.Printf
importmode :: Mode RawOpts
importmode = CommandDoc
-> [Flag RawOpts]
-> [(CommandDoc, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Import.txt")
[forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone [CommandDoc
"catchup"] (CommandDoc -> RawOpts -> RawOpts
setboolopt CommandDoc
"catchup") CommandDoc
"just mark all transactions as already imported"
,forall a. [CommandDoc] -> (a -> a) -> CommandDoc -> Flag a
flagNone [CommandDoc
"dry-run"] (CommandDoc -> RawOpts -> RawOpts
setboolopt CommandDoc
"dry-run") CommandDoc
"just show the transactions to be imported"
]
[(CommandDoc, [Flag RawOpts])
generalflagsgroup1]
[Flag RawOpts]
hiddenflags
([], forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ CommandDoc -> Arg RawOpts
argsFlag CommandDoc
"FILE [...]")
importcmd :: CliOpts -> Journal -> IO ()
importcmd opts :: CliOpts
opts@CliOpts{rawopts_ :: CliOpts -> RawOpts
rawopts_=RawOpts
rawopts,inputopts_ :: CliOpts -> InputOpts
inputopts_=InputOpts
iopts} Journal
j = do
let
inputfiles :: [CommandDoc]
inputfiles = CommandDoc -> RawOpts -> [CommandDoc]
listofstringopt CommandDoc
"args" RawOpts
rawopts
inputstr :: CommandDoc
inputstr = forall a. [a] -> [[a]] -> [a]
intercalate CommandDoc
", " forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map CommandDoc -> CommandDoc
quoteIfNeeded [CommandDoc]
inputfiles
catchup :: Bool
catchup = CommandDoc -> RawOpts -> Bool
boolopt CommandDoc
"catchup" RawOpts
rawopts
dryrun :: Bool
dryrun = CommandDoc -> RawOpts -> Bool
boolopt CommandDoc
"dry-run" RawOpts
rawopts
combinedStyles :: Maybe (Map CommoditySymbol AmountStyle)
combinedStyles =
let
maybeInputStyles :: Maybe (Map CommoditySymbol AmountStyle)
maybeInputStyles = BalancingOpts -> Maybe (Map CommoditySymbol AmountStyle)
commodity_styles_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. InputOpts -> BalancingOpts
balancingopts_ forall a b. (a -> b) -> a -> b
$ InputOpts
iopts
inferredStyles :: Map CommoditySymbol AmountStyle
inferredStyles = Journal -> Map CommoditySymbol AmountStyle
journalCommodityStyles Journal
j
in
case Maybe (Map CommoditySymbol AmountStyle)
maybeInputStyles of
Maybe (Map CommoditySymbol AmountStyle)
Nothing -> forall a. a -> Maybe a
Just Map CommoditySymbol AmountStyle
inferredStyles
Just Map CommoditySymbol AmountStyle
inputStyles -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Map CommoditySymbol AmountStyle
inputStyles forall a. Semigroup a => a -> a -> a
<> Map CommoditySymbol AmountStyle
inferredStyles
iopts' :: InputOpts
iopts' = InputOpts
iopts{new_ :: Bool
new_=Bool
True, new_save_ :: Bool
new_save_=Bool -> Bool
not Bool
dryrun, balancingopts_ :: BalancingOpts
balancingopts_=BalancingOpts
defbalancingopts{commodity_styles_ :: Maybe (Map CommoditySymbol AmountStyle)
commodity_styles_= Maybe (Map CommoditySymbol AmountStyle)
combinedStyles}}
case [CommandDoc]
inputfiles of
[] -> forall a. CommandDoc -> a
error' CommandDoc
"please provide one or more input files as arguments"
[CommandDoc]
fs -> do
Either CommandDoc Journal
enewj <- forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall a b. (a -> b) -> a -> b
$ InputOpts -> [CommandDoc] -> ExceptT CommandDoc IO Journal
readJournalFiles InputOpts
iopts' [CommandDoc]
fs
case Either CommandDoc Journal
enewj of
Left CommandDoc
e -> forall a. CommandDoc -> a
error' CommandDoc
e
Right Journal
newj ->
case forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Transaction -> Day
tdate forall a b. (a -> b) -> a -> b
$ Journal -> [Transaction]
jtxns Journal
newj of
[] -> do
let semicolon :: CommandDoc
semicolon = if Bool
dryrun then CommandDoc
"; " else CommandDoc
"" :: String
forall r. PrintfType r => CommandDoc -> r
printf CommandDoc
"%sno new transactions found in %s\n\n" CommandDoc
semicolon CommandDoc
inputstr
[Transaction]
newts | Bool
dryrun -> do
forall r. PrintfType r => CommandDoc -> r
printf CommandDoc
"; would import %d new transactions from %s:\n\n" (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Transaction]
newts) CommandDoc
inputstr
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (CommoditySymbol -> IO ()
T.putStr forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> CommoditySymbol
showTransaction) [Transaction]
newts
[Transaction]
newts | Bool
catchup -> do
forall r. PrintfType r => CommandDoc -> r
printf CommandDoc
"marked %s as caught up, skipping %d unimported transactions\n\n" CommandDoc
inputstr (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Transaction]
newts)
[Transaction]
newts -> do
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ (Journal -> CliOpts -> Transaction -> IO Journal
`journalAddTransaction` CliOpts
opts) Journal
j [Transaction]
newts
forall r. PrintfType r => CommandDoc -> r
printf CommandDoc
"imported %d new transactions from %s\n" (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Transaction]
newts) CommandDoc
inputstr