cli: make rewrite a builtin command

This commit is contained in:
Simon Michael 2017-09-12 19:51:27 -07:00
parent 89f694707b
commit d2d7dbbce0
5 changed files with 30 additions and 38 deletions

View File

@ -28,6 +28,7 @@ module Hledger.Cli.Commands (
,module Hledger.Cli.Commands.Printunique
,module Hledger.Cli.Commands.Register
,module Hledger.Cli.Commands.Registermatch
,module Hledger.Cli.Commands.Rewrite
,module Hledger.Cli.Commands.Stats
)
where
@ -63,6 +64,7 @@ import Hledger.Cli.Commands.Print
import Hledger.Cli.Commands.Printunique
import Hledger.Cli.Commands.Register
import Hledger.Cli.Commands.Registermatch
import Hledger.Cli.Commands.Rewrite
import Hledger.Cli.Commands.Stats

View File

@ -1,33 +1,26 @@
#!/usr/bin/env stack
{- stack runghc --verbosity info
--package hledger-lib
--package hledger
--package here
--package megaparsec
--package text
--package Diff
-}
{-# OPTIONS_GHC -Wno-missing-signatures -Wno-name-shadowing #-}
{-# LANGUAGE OverloadedStrings, LambdaCase, DeriveTraversable, ViewPatterns, QuasiQuotes #-}
module Hledger.Cli.Commands.Rewrite (
rewritemode
,rewrite
)
where
import Control.Monad.Writer
import Data.List (sortOn, foldl')
import Data.String.Here
import qualified Data.Text as T
-- hledger lib, cli and cmdargs utils
import Hledger.Cli hiding (outputflags)
-- more utils for parsing
-- #if !MIN_VERSION_base(4,8,0)
-- import Control.Applicative.Compat ((<*))
-- #endif
import Hledger
import Hledger.Data.AutoTransaction (runModifierTransaction)
import Hledger.Cli.CliOptions
import Hledger.Cli.Commands.Print
--import Hledger.Cli hiding (outputflags)
import System.Console.CmdArgs.Explicit
import Text.Printf
import Text.Megaparsec
import qualified Data.Algorithm.Diff as D
import Hledger.Data.AutoTransaction (runModifierTransaction)
------------------------------------------------------------------------------
cmdmode = hledgerCommandMode
rewritemode = hledgerCommandMode
[here| rewrite
Print all transactions, adding custom postings to the matched ones.
@ -166,13 +159,10 @@ https://github.com/simonmichael/hledger/issues/99
-- TODO interpolating match groups in replacement
-- TODO allow using this on unbalanced entries, eg to rewrite while editing
main :: IO ()
main = do
opts@CliOpts{rawopts_=rawopts,reportopts_=ropts} <- getHledgerCliOpts cmdmode
rewrite opts@CliOpts{rawopts_=rawopts,reportopts_=ropts} j@Journal{jtxns=ts} = do
d <- getCurrentDay
let q = queryFromOpts d ropts
modifier <- modifierTransactionFromOpts rawopts
withJournalDo opts $ \opts' j@Journal{jtxns=ts} -> do
-- create re-writer
let modifiers = modifier : jmodifiertxns j
-- Note that some query matches require transaction. Thus modifiers
@ -181,7 +171,7 @@ main = do
-- rewrite matched transactions
let j' = j{jtxns=map modifier' ts}
-- run the print command, showing all transactions
outputFromOpts rawopts opts'{reportopts_=ropts{query_=""}} j j'
outputFromOpts rawopts opts{reportopts_=ropts{query_=""}} j j'
postingp' :: T.Text -> IO Posting
postingp' t = runErroringJournalParser (postingp Nothing <* eof) t' >>= \case

View File

@ -89,11 +89,6 @@ is an old pie chart generator, in need of some love.
[hledger-check.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-check.hs)
checks more powerful account balance assertions.
### rewrite
[hledger-rewrite.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-rewrite.hs#L28)
Adds one or more custom postings to matched transactions.
### tags
[hledger-tags.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-tags.hs)

View File

@ -685,6 +685,9 @@ Print the one posting whose transaction description is closest to DESC,
in the style of the register command.
Helps ledger-autosync detect already-seen transactions when importing.
## rewrite
Print all transactions, adding custom postings to the matched ones.
## stats
Show some journal statistics.

View File

@ -120,6 +120,7 @@ library:
- Hledger.Cli.Commands.Printunique
- Hledger.Cli.Commands.Register
- Hledger.Cli.Commands.Registermatch
- Hledger.Cli.Commands.Rewrite
- Hledger.Cli.Commands.Stats
- Hledger.Cli.CompoundBalanceCommand
- Text.Tabular.AsciiWide
@ -130,6 +131,7 @@ library:
- cmdargs >=0.10 && <0.11
- csv
- data-default >=0.5
- Diff
- hashable >=1.2.4
- haskeline >=0.6 && <=0.8
- HUnit