lib, app, web: remove differences between command line and journal file aliases

This commit is contained in:
Dmitry Astapov 2018-04-17 23:58:53 +01:00 committed by Simon Michael
parent f6ec26e321
commit d82370d10b
8 changed files with 43 additions and 28 deletions

View File

@ -16,7 +16,6 @@ module Hledger.Data.Journal (
addModifierTransaction,
addPeriodicTransaction,
addTransaction,
journalApplyAliases,
journalBalanceTransactions,
journalApplyCommodityStyles,
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
-- use. Reverse parsed data to normal order, canonicalise amount
-- formats, check/ensure that transactions are balanced, and maybe

View File

@ -25,7 +25,6 @@ module Hledger.Read (
readJournal',
-- * Re-exported
JournalReader.accountaliasp,
JournalReader.postingp,
module Hledger.Read.Common,

View File

@ -63,8 +63,7 @@ module Hledger.Read.JournalReader (
-- numberp,
statusp,
emptyorcommentlinep,
followingcommentp,
accountaliasp
followingcommentp
-- * Tests
,tests_Hledger_Read_JournalReader
@ -120,7 +119,17 @@ reader = Reader
-- | Parse and post-process a "Journal" from hledger's journal file
-- format, or give an error.
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
--- ** journal

View File

@ -78,7 +78,6 @@ withJournalDoUICommand uopts@UIOpts{cliopts_=copts} cmd = do
let fn = cmd uopts
. pivotByOpts copts
. anonymiseByOpts copts
. journalApplyAliases (aliasesFromOpts copts)
<=< journalApplyValue (reportopts_ copts)
<=< journalAddForecast copts
either error' fn ej

View File

@ -63,7 +63,6 @@ withJournalDo' opts@WebOpts {cliopts_ = cliopts} cmd = do
let fn = cmd opts
. pivotByOpts cliopts
. anonymiseByOpts cliopts
. journalApplyAliases (aliasesFromOpts cliopts)
<=< journalApplyValue (reportopts_ cliopts)
<=< journalAddForecast cliopts
readJournalFile def f >>= either error' fn

View File

@ -44,7 +44,6 @@ module Hledger.Cli.CliOptions (
-- possibly these should move into argsToCliOpts
-- * CLI option accessors
-- | These do the extra processing required for some options.
aliasesFromOpts,
journalFilePathFromOpts,
rulesFilePathFromOpts,
outputFileFromOpts,
@ -484,11 +483,6 @@ getHledgerCliOpts mode' = do
-- 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
-- 1. options, 2. an environment variable, or 3. the default.
-- Actually, returns one or more file paths. There will be more

View File

@ -70,7 +70,6 @@ withJournalDo opts cmd = do
let f = cmd opts
. pivotByOpts opts
. anonymiseByOpts opts
. journalApplyAliases (aliasesFromOpts opts)
<=< journalApplyValue (reportopts_ opts)
<=< journalAddForecast opts
either error' f ej
@ -161,7 +160,7 @@ writeOutput opts s = do
journalReload :: CliOpts -> IO (Either String Journal)
journalReload opts = do
journalpaths <- journalFilePathFromOpts opts
((pivotByOpts opts . journalApplyAliases (aliasesFromOpts opts)) <$>) <$>
(pivotByOpts opts <$>) <$>
readJournalFiles (inputopts_ opts) journalpaths
-- | Re-read the option-specified journal file(s), but only if any of

View File

@ -122,6 +122,36 @@ alias E=F
>>>2
>>>=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
hledger -f- reg '^a$' '^b$'
<<<