Debian Codegraph @

Hledger/Cli/Commands/Import.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 {-# LANGUAGE MultiWayIf #-}
4 {-# LANGUAGE NamedFieldPuns #-}
5
6 module Hledger.Cli.Commands.Import (
7 importmode
8 ,importcmd
9 )
10 where
11
12 import Control.Monad
13 import Data.List
14 import Data.Text.IO qualified as T
15 import System.Console.CmdArgs.Explicit
16 import Text.Printf
17
18 import Hledger
19 import Hledger.Cli.CliOptions
20 import Hledger.Cli.Commands.Add (journalAddTransaction)
21 import System.IO (stderr)
22 import System.FilePath (takeFileName)
23
24 importmode = hledgerCommandMode
25 $(embedFileRelative "Hledger/Cli/Commands/Import.txt")
26 [flagNone ["catchup"] (setboolopt "catchup") "just mark all transactions as already imported"
27 ,flagNone ["dry-run"] (setboolopt "dry-run") "just show the transactions to be imported"
28 ]
29 cligeneralflagsgroups1
30 hiddenflags
31 ([], Just $ argsFlag "[-f JOURNALFILE] DATAFILES...")
32
33 importcmd opts@CliOpts{rawopts_=rawopts,inputopts_=iopts} j = do
34 -- XXX could be helpful to show the last-seen date, and number of old transactions, too
35 let
36 inputfiles = listofstringopt "args" rawopts
37 inputstr = intercalate ", " $ map (quoteIfNeeded.takeFileName) inputfiles
38 catchup = boolopt "catchup" rawopts
39 dryrun = boolopt "dry-run" rawopts
40 combinedStyles =
41 let
42 maybeInputStyles = commodity_styles_ . balancingopts_ $ iopts
43 inferredStyles = journalCommodityStyles j
44 in
45 case maybeInputStyles of
46 Nothing -> Just inferredStyles
47 Just inputStyles -> Just $ inputStyles <> inferredStyles
48
49 iopts' = iopts{
50 new_=True, -- read only new transactions since last time
51 new_save_=False, -- defer saving .latest files until the end
52 strict_=False, -- defer strict checks until the end
53 balancingopts_=defbalancingopts{commodity_styles_= combinedStyles} -- use amount styles from both when balancing txns
54 }
55
56 case inputfiles of
57 [] -> error' "please provide one or more data files as arguments" -- PARTIAL:
58 fs -> do
59 enewjandlatestdatesforfiles <- runExceptT $ readJournalFilesAndLatestDates iopts' fs
60 case enewjandlatestdatesforfiles of
61 Left err -> error' err
62 Right (newj, latestdatesforfiles) ->
63 case sortOn tdate $ jtxns newj of
64 [] -> hPrintf stderr "no new transactions found in %s\n" inputstr
65
66 newts | catchup ->
67 if dryrun
68 then hPrintf stderr "would skip %d new transactions (dry run)\n\n" (length newts)
69 else do
70 hPrintf stderr "marked %s as caught up, skipping %d transactions\n\n" inputstr (length newts)
71 saveLatestDatesForFiles latestdatesforfiles
72
73 newts -> do
74 if dryrun
75 then do
76 -- show txns to be imported
77 hPrintf stderr "would import %d new transactions from %s:\n\n" (length newts) inputstr
78 mapM_ (T.putStr . showTransaction) newts
79
80 -- then check the whole journal with them added, if in strict mode
81 when (strict_ iopts) $ strictChecks
82
83 else do
84 -- first check the whole journal with them added, if in strict mode
85 when (strict_ iopts) $ strictChecks
86
87 -- then append the transactions to the main journal file.
88 -- XXX This writes unix line endings (\n), some at least,
89 -- even if the file uses dos line endings (\r\n), which could leave
90 -- mixed line endings in the file. See also writeFileWithBackupIfChanged.
91 foldM_ (`journalAddTransaction` opts) j newts -- gets forced somehow.. (how ?)
92
93 hPrintf stderr "imported %d new transactions from %s to %s\n" (length newts) inputstr (journalFilePath j)
94
95 -- and if we got this far, update each file's .latest file
96 saveLatestDatesForFiles latestdatesforfiles
97
98 where
99 -- add the new transactions to the journal in memory and check the whole thing
100 strictChecks = either fail pure $ journalStrictChecks j'
101 where j' = foldl' (flip addTransaction) j newts