lib, app, web: remove differences between command line and journal file aliases
This commit is contained in:
parent
f6ec26e321
commit
d82370d10b
@ -16,7 +16,6 @@ module Hledger.Data.Journal (
|
|||||||
addModifierTransaction,
|
addModifierTransaction,
|
||||||
addPeriodicTransaction,
|
addPeriodicTransaction,
|
||||||
addTransaction,
|
addTransaction,
|
||||||
journalApplyAliases,
|
|
||||||
journalBalanceTransactions,
|
journalBalanceTransactions,
|
||||||
journalApplyCommodityStyles,
|
journalApplyCommodityStyles,
|
||||||
commodityStylesFromAmounts,
|
commodityStylesFromAmounts,
|
||||||
@ -485,19 +484,6 @@ filterJournalTransactionsByAccount apats j@Journal{jtxns=ts} = j{jtxns=filter tm
|
|||||||
|
|
||||||
-}
|
-}
|
||||||
|
|
||||||
-- | Apply additional account aliases (eg from the command-line) to all postings in a journal.
|
|
||||||
journalApplyAliases :: [AccountAlias] -> Journal -> Journal
|
|
||||||
journalApplyAliases aliases j@Journal{jtxns=ts} =
|
|
||||||
-- (if null aliases
|
|
||||||
-- then id
|
|
||||||
-- else (dbgtrace $
|
|
||||||
-- "applying additional command-line aliases:\n"
|
|
||||||
-- ++ chomp (unlines $ map (" "++) $ lines $ ppShow aliases))) $
|
|
||||||
j{jtxns=map dotransaction ts}
|
|
||||||
where
|
|
||||||
dotransaction t@Transaction{tpostings=ps} = t{tpostings=map doposting ps}
|
|
||||||
doposting p@Posting{paccount=a} = p{paccount= accountNameApplyAliases aliases a}
|
|
||||||
|
|
||||||
-- | Do post-parse processing on a parsed journal to make it ready for
|
-- | Do post-parse processing on a parsed journal to make it ready for
|
||||||
-- use. Reverse parsed data to normal order, canonicalise amount
|
-- use. Reverse parsed data to normal order, canonicalise amount
|
||||||
-- formats, check/ensure that transactions are balanced, and maybe
|
-- formats, check/ensure that transactions are balanced, and maybe
|
||||||
|
|||||||
@ -25,7 +25,6 @@ module Hledger.Read (
|
|||||||
readJournal',
|
readJournal',
|
||||||
|
|
||||||
-- * Re-exported
|
-- * Re-exported
|
||||||
JournalReader.accountaliasp,
|
|
||||||
JournalReader.postingp,
|
JournalReader.postingp,
|
||||||
module Hledger.Read.Common,
|
module Hledger.Read.Common,
|
||||||
|
|
||||||
|
|||||||
@ -63,8 +63,7 @@ module Hledger.Read.JournalReader (
|
|||||||
-- numberp,
|
-- numberp,
|
||||||
statusp,
|
statusp,
|
||||||
emptyorcommentlinep,
|
emptyorcommentlinep,
|
||||||
followingcommentp,
|
followingcommentp
|
||||||
accountaliasp
|
|
||||||
|
|
||||||
-- * Tests
|
-- * Tests
|
||||||
,tests_Hledger_Read_JournalReader
|
,tests_Hledger_Read_JournalReader
|
||||||
@ -120,7 +119,17 @@ reader = Reader
|
|||||||
-- | Parse and post-process a "Journal" from hledger's journal file
|
-- | Parse and post-process a "Journal" from hledger's journal file
|
||||||
-- format, or give an error.
|
-- format, or give an error.
|
||||||
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
|
parse :: InputOpts -> FilePath -> Text -> ExceptT String IO Journal
|
||||||
parse = parseAndFinaliseJournal journalp
|
parse iopts = parseAndFinaliseJournal journalp' iopts
|
||||||
|
where
|
||||||
|
journalp' = do
|
||||||
|
-- reverse parsed aliases to ensure that they are applied in order given on commandline
|
||||||
|
mapM_ addAccountAlias (reverse $ aliasesFromOpts iopts)
|
||||||
|
journalp
|
||||||
|
|
||||||
|
-- | Get the account name aliases from options, if any.
|
||||||
|
aliasesFromOpts :: InputOpts -> [AccountAlias]
|
||||||
|
aliasesFromOpts = map (\a -> fromparse $ runParser accountaliasp ("--alias "++quoteIfNeeded a) $ T.pack a)
|
||||||
|
. aliases_
|
||||||
|
|
||||||
--- * parsers
|
--- * parsers
|
||||||
--- ** journal
|
--- ** journal
|
||||||
|
|||||||
@ -78,7 +78,6 @@ withJournalDoUICommand uopts@UIOpts{cliopts_=copts} cmd = do
|
|||||||
let fn = cmd uopts
|
let fn = cmd uopts
|
||||||
. pivotByOpts copts
|
. pivotByOpts copts
|
||||||
. anonymiseByOpts copts
|
. anonymiseByOpts copts
|
||||||
. journalApplyAliases (aliasesFromOpts copts)
|
|
||||||
<=< journalApplyValue (reportopts_ copts)
|
<=< journalApplyValue (reportopts_ copts)
|
||||||
<=< journalAddForecast copts
|
<=< journalAddForecast copts
|
||||||
either error' fn ej
|
either error' fn ej
|
||||||
|
|||||||
@ -63,7 +63,6 @@ withJournalDo' opts@WebOpts {cliopts_ = cliopts} cmd = do
|
|||||||
let fn = cmd opts
|
let fn = cmd opts
|
||||||
. pivotByOpts cliopts
|
. pivotByOpts cliopts
|
||||||
. anonymiseByOpts cliopts
|
. anonymiseByOpts cliopts
|
||||||
. journalApplyAliases (aliasesFromOpts cliopts)
|
|
||||||
<=< journalApplyValue (reportopts_ cliopts)
|
<=< journalApplyValue (reportopts_ cliopts)
|
||||||
<=< journalAddForecast cliopts
|
<=< journalAddForecast cliopts
|
||||||
readJournalFile def f >>= either error' fn
|
readJournalFile def f >>= either error' fn
|
||||||
|
|||||||
@ -44,7 +44,6 @@ module Hledger.Cli.CliOptions (
|
|||||||
-- possibly these should move into argsToCliOpts
|
-- possibly these should move into argsToCliOpts
|
||||||
-- * CLI option accessors
|
-- * CLI option accessors
|
||||||
-- | These do the extra processing required for some options.
|
-- | These do the extra processing required for some options.
|
||||||
aliasesFromOpts,
|
|
||||||
journalFilePathFromOpts,
|
journalFilePathFromOpts,
|
||||||
rulesFilePathFromOpts,
|
rulesFilePathFromOpts,
|
||||||
outputFileFromOpts,
|
outputFileFromOpts,
|
||||||
@ -484,11 +483,6 @@ getHledgerCliOpts mode' = do
|
|||||||
|
|
||||||
-- CliOpts accessors
|
-- CliOpts accessors
|
||||||
|
|
||||||
-- | Get the account name aliases from options, if any.
|
|
||||||
aliasesFromOpts :: CliOpts -> [AccountAlias]
|
|
||||||
aliasesFromOpts = map (\a -> fromparse $ runParser accountaliasp ("--alias "++quoteIfNeeded a) $ T.pack a)
|
|
||||||
. aliases_ . inputopts_
|
|
||||||
|
|
||||||
-- | Get the (tilde-expanded, absolute) journal file path from
|
-- | Get the (tilde-expanded, absolute) journal file path from
|
||||||
-- 1. options, 2. an environment variable, or 3. the default.
|
-- 1. options, 2. an environment variable, or 3. the default.
|
||||||
-- Actually, returns one or more file paths. There will be more
|
-- Actually, returns one or more file paths. There will be more
|
||||||
|
|||||||
@ -70,7 +70,6 @@ withJournalDo opts cmd = do
|
|||||||
let f = cmd opts
|
let f = cmd opts
|
||||||
. pivotByOpts opts
|
. pivotByOpts opts
|
||||||
. anonymiseByOpts opts
|
. anonymiseByOpts opts
|
||||||
. journalApplyAliases (aliasesFromOpts opts)
|
|
||||||
<=< journalApplyValue (reportopts_ opts)
|
<=< journalApplyValue (reportopts_ opts)
|
||||||
<=< journalAddForecast opts
|
<=< journalAddForecast opts
|
||||||
either error' f ej
|
either error' f ej
|
||||||
@ -161,7 +160,7 @@ writeOutput opts s = do
|
|||||||
journalReload :: CliOpts -> IO (Either String Journal)
|
journalReload :: CliOpts -> IO (Either String Journal)
|
||||||
journalReload opts = do
|
journalReload opts = do
|
||||||
journalpaths <- journalFilePathFromOpts opts
|
journalpaths <- journalFilePathFromOpts opts
|
||||||
((pivotByOpts opts . journalApplyAliases (aliasesFromOpts opts)) <$>) <$>
|
(pivotByOpts opts <$>) <$>
|
||||||
readJournalFiles (inputopts_ opts) journalpaths
|
readJournalFiles (inputopts_ opts) journalpaths
|
||||||
|
|
||||||
-- | Re-read the option-specified journal file(s), but only if any of
|
-- | Re-read the option-specified journal file(s), but only if any of
|
||||||
|
|||||||
@ -122,6 +122,36 @@ alias E=F
|
|||||||
>>>2
|
>>>2
|
||||||
>>>=0
|
>>>=0
|
||||||
|
|
||||||
|
# alias options
|
||||||
|
hledger -f- balance --alias=cc=credit-card --alias=b=bank
|
||||||
|
<<<
|
||||||
|
01/01 Opening balances
|
||||||
|
(bank) 100
|
||||||
|
(credit-card) -10
|
||||||
|
|
||||||
|
01/02
|
||||||
|
expenses 5
|
||||||
|
cc
|
||||||
|
|
||||||
|
01/03
|
||||||
|
expenses 5
|
||||||
|
b
|
||||||
|
|
||||||
|
01/04 Credit card charge
|
||||||
|
cc = 0
|
||||||
|
b
|
||||||
|
|
||||||
|
01/05
|
||||||
|
expenses 5
|
||||||
|
b = 75
|
||||||
|
>>>
|
||||||
|
75 bank
|
||||||
|
15 expenses
|
||||||
|
--------------------
|
||||||
|
90
|
||||||
|
>>>2
|
||||||
|
>>>=0
|
||||||
|
|
||||||
# query will search both origin and substitution in alias
|
# query will search both origin and substitution in alias
|
||||||
hledger -f- reg '^a$' '^b$'
|
hledger -f- reg '^a$' '^b$'
|
||||||
<<<
|
<<<
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user