cli, web, ui: Replace withJournalDo* variants
This commit is contained in:
parent
036110529a
commit
6becbc78f3
@ -61,28 +61,20 @@ writeChan = BC.writeBChan
|
|||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
opts <- getHledgerUIOpts
|
opts <- getHledgerUIOpts
|
||||||
|
let copts = cliopts_ opts
|
||||||
|
copts' = copts
|
||||||
|
{ inputopts_ = (inputopts_ copts) { auto_ = True }
|
||||||
|
, reportopts_ = (reportopts_ copts) { forecast_ = True }
|
||||||
|
}
|
||||||
|
|
||||||
-- when (debug_ $ cliopts_ opts) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show opts)
|
-- when (debug_ $ cliopts_ opts) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show opts)
|
||||||
run opts
|
run $ opts { cliopts_ = copts' }
|
||||||
where
|
where
|
||||||
run opts
|
run opts
|
||||||
| "help" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStr (showModeUsage uimode) >> exitSuccess
|
| "help" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStr (showModeUsage uimode) >> exitSuccess
|
||||||
| "version" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn prognameandversion >> exitSuccess
|
| "version" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn prognameandversion >> exitSuccess
|
||||||
| "binary-filename" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn (binaryfilename progname)
|
| "binary-filename" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn (binaryfilename progname)
|
||||||
| otherwise = withJournalDoUICommand opts runBrickUi
|
| otherwise = withJournalDo (cliopts_ opts) (runBrickUi opts)
|
||||||
|
|
||||||
-- TODO fix nasty duplication of withJournalDo
|
|
||||||
-- | hledger-ui's version of withJournalDo, which turns on --auto and --forecast.
|
|
||||||
withJournalDoUICommand :: UIOpts -> (UIOpts -> Journal -> IO ()) -> IO ()
|
|
||||||
withJournalDoUICommand uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=iopts,reportopts_=ropts}} cmd = do
|
|
||||||
let copts' = copts{inputopts_=iopts{auto_=True}, reportopts_=ropts{forecast_=True}}
|
|
||||||
journalpath <- journalFilePathFromOpts copts'
|
|
||||||
ej <- readJournalFiles (inputopts_ copts') journalpath
|
|
||||||
let fn = cmd uopts
|
|
||||||
. pivotByOpts copts'
|
|
||||||
. anonymiseByOpts copts'
|
|
||||||
<=< journalApplyValue (reportopts_ copts')
|
|
||||||
<=< journalAddForecast copts'
|
|
||||||
either error' fn ej
|
|
||||||
|
|
||||||
runBrickUi :: UIOpts -> Journal -> IO ()
|
runBrickUi :: UIOpts -> Journal -> IO ()
|
||||||
runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportopts_=ropts}} j = do
|
runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportopts_=ropts}} j = do
|
||||||
|
|||||||
@ -38,7 +38,7 @@ hledgerWebMain = do
|
|||||||
|
|
||||||
hledgerWebDev :: IO (Int, Application)
|
hledgerWebDev :: IO (Int, Application)
|
||||||
hledgerWebDev =
|
hledgerWebDev =
|
||||||
withJournalDoWeb defwebopts (\o j -> defaultDevelApp loader $ makeApplication o j)
|
withJournalDo (cliopts_ defwebopts) (defaultDevelApp loader . makeApplication defwebopts)
|
||||||
where
|
where
|
||||||
loader =
|
loader =
|
||||||
Yesod.Default.Config.loadConfig
|
Yesod.Default.Config.loadConfig
|
||||||
@ -49,26 +49,7 @@ runWith opts
|
|||||||
| "help" `inRawOpts` rawopts_ (cliopts_ opts) = putStr (showModeUsage webmode) >> exitSuccess
|
| "help" `inRawOpts` rawopts_ (cliopts_ opts) = putStr (showModeUsage webmode) >> exitSuccess
|
||||||
| "version" `inRawOpts` rawopts_ (cliopts_ opts) = putStrLn prognameandversion >> exitSuccess
|
| "version" `inRawOpts` rawopts_ (cliopts_ opts) = putStrLn prognameandversion >> exitSuccess
|
||||||
| "binary-filename" `inRawOpts` rawopts_ (cliopts_ opts) = putStrLn (binaryfilename progname)
|
| "binary-filename" `inRawOpts` rawopts_ (cliopts_ opts) = putStrLn (binaryfilename progname)
|
||||||
| otherwise = withJournalDoWeb opts web
|
| otherwise = withJournalDo (cliopts_ opts) (web opts)
|
||||||
|
|
||||||
-- | A version of withJournalDo specialised for hledger-web.
|
|
||||||
-- Disallows the special - file to avoid some bug,
|
|
||||||
-- takes WebOpts rather than CliOpts.
|
|
||||||
withJournalDoWeb :: WebOpts -> (WebOpts -> Journal -> IO a) -> IO a
|
|
||||||
withJournalDoWeb opts@WebOpts {cliopts_ = copts} cmd = do
|
|
||||||
journalpaths <- journalFilePathFromOpts copts
|
|
||||||
|
|
||||||
-- https://github.com/simonmichael/hledger/issues/202
|
|
||||||
-- -f- gives [Error#yesod-core] <stdin>: hGetContents: illegal operation (handle is closed)
|
|
||||||
-- Also we may try to write to this file. Just disallow -.
|
|
||||||
when ("-" `elem` journalpaths) $ -- always non-empty
|
|
||||||
error' "hledger-web doesn't support -f -, please specify a file path"
|
|
||||||
mapM_ requireJournalFileExists journalpaths
|
|
||||||
|
|
||||||
-- keep synced with withJournalDo TODO refactor
|
|
||||||
readJournalFiles (inputopts_ copts) journalpaths
|
|
||||||
>>= mapM (journalTransform copts)
|
|
||||||
>>= either error' (cmd opts)
|
|
||||||
|
|
||||||
-- | The web command.
|
-- | The web command.
|
||||||
web :: WebOpts -> Journal -> IO ()
|
web :: WebOpts -> Journal -> IO ()
|
||||||
|
|||||||
@ -175,9 +175,9 @@ main = do
|
|||||||
cmdaction opts (error "journal-less command tried to use the journal")
|
cmdaction opts (error "journal-less command tried to use the journal")
|
||||||
"add" -> -- should create the journal if missing
|
"add" -> -- should create the journal if missing
|
||||||
(ensureJournalFileExists =<< (head <$> journalFilePathFromOpts opts)) >>
|
(ensureJournalFileExists =<< (head <$> journalFilePathFromOpts opts)) >>
|
||||||
withJournalDo opts cmdaction
|
withJournalDo opts (cmdaction opts)
|
||||||
_ -> -- all other commands: read the journal or fail if missing
|
_ -> -- all other commands: read the journal or fail if missing
|
||||||
withJournalDo opts cmdaction
|
withJournalDo opts (cmdaction opts)
|
||||||
)
|
)
|
||||||
`orShowHelp` cmdmode
|
`orShowHelp` cmdmode
|
||||||
|
|
||||||
|
|||||||
@ -61,19 +61,19 @@ import Hledger.Utils
|
|||||||
-- | Parse the user's specified journal file(s) as a Journal, maybe apply some
|
-- | Parse the user's specified journal file(s) as a Journal, maybe apply some
|
||||||
-- transformations according to options, and run a hledger command with it.
|
-- transformations according to options, and run a hledger command with it.
|
||||||
-- Or, throw an error.
|
-- Or, throw an error.
|
||||||
withJournalDo :: CliOpts -> (CliOpts -> Journal -> IO ()) -> IO ()
|
withJournalDo :: CliOpts -> (Journal -> IO a) -> IO a
|
||||||
withJournalDo opts cmd = do
|
withJournalDo opts cmd = do
|
||||||
-- We kludgily read the file before parsing to grab the full text, unless
|
-- We kludgily read the file before parsing to grab the full text, unless
|
||||||
-- it's stdin, or it doesn't exist and we are adding. We read it strictly
|
-- it's stdin, or it doesn't exist and we are adding. We read it strictly
|
||||||
-- to let the add command work.
|
-- to let the add command work.
|
||||||
journalpaths <- journalFilePathFromOpts opts
|
journalpaths <- journalFilePathFromOpts opts
|
||||||
readJournalFiles (inputopts_ opts) journalpaths
|
readJournalFiles (inputopts_ opts) journalpaths
|
||||||
>>= mapM (journalTransform opts)
|
>>= mapM (journalTransform opts)
|
||||||
>>= either error' (cmd opts)
|
>>= either error' cmd
|
||||||
|
|
||||||
-- | Apply some transformations to the journal if specified by options.
|
-- | Apply some transformations to the journal if specified by options.
|
||||||
-- These include:
|
-- These include:
|
||||||
--
|
--
|
||||||
-- - adding forecast transactions (--forecast)
|
-- - adding forecast transactions (--forecast)
|
||||||
-- - converting amounts to market value (--value)
|
-- - converting amounts to market value (--value)
|
||||||
-- - pivoting account names (--pivot)
|
-- - pivoting account names (--pivot)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user