cli: make rewrite a builtin command
This commit is contained in:
parent
89f694707b
commit
d2d7dbbce0
@ -28,6 +28,7 @@ module Hledger.Cli.Commands (
|
|||||||
,module Hledger.Cli.Commands.Printunique
|
,module Hledger.Cli.Commands.Printunique
|
||||||
,module Hledger.Cli.Commands.Register
|
,module Hledger.Cli.Commands.Register
|
||||||
,module Hledger.Cli.Commands.Registermatch
|
,module Hledger.Cli.Commands.Registermatch
|
||||||
|
,module Hledger.Cli.Commands.Rewrite
|
||||||
,module Hledger.Cli.Commands.Stats
|
,module Hledger.Cli.Commands.Stats
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
@ -63,6 +64,7 @@ import Hledger.Cli.Commands.Print
|
|||||||
import Hledger.Cli.Commands.Printunique
|
import Hledger.Cli.Commands.Printunique
|
||||||
import Hledger.Cli.Commands.Register
|
import Hledger.Cli.Commands.Register
|
||||||
import Hledger.Cli.Commands.Registermatch
|
import Hledger.Cli.Commands.Registermatch
|
||||||
|
import Hledger.Cli.Commands.Rewrite
|
||||||
import Hledger.Cli.Commands.Stats
|
import Hledger.Cli.Commands.Stats
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -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 #-}
|
{-# LANGUAGE OverloadedStrings, LambdaCase, DeriveTraversable, ViewPatterns, QuasiQuotes #-}
|
||||||
|
|
||||||
|
module Hledger.Cli.Commands.Rewrite (
|
||||||
|
rewritemode
|
||||||
|
,rewrite
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
import Control.Monad.Writer
|
import Control.Monad.Writer
|
||||||
import Data.List (sortOn, foldl')
|
import Data.List (sortOn, foldl')
|
||||||
import Data.String.Here
|
import Data.String.Here
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
-- hledger lib, cli and cmdargs utils
|
import Hledger
|
||||||
import Hledger.Cli hiding (outputflags)
|
import Hledger.Data.AutoTransaction (runModifierTransaction)
|
||||||
-- more utils for parsing
|
import Hledger.Cli.CliOptions
|
||||||
-- #if !MIN_VERSION_base(4,8,0)
|
import Hledger.Cli.Commands.Print
|
||||||
-- import Control.Applicative.Compat ((<*))
|
--import Hledger.Cli hiding (outputflags)
|
||||||
-- #endif
|
import System.Console.CmdArgs.Explicit
|
||||||
import Text.Printf
|
import Text.Printf
|
||||||
import Text.Megaparsec
|
import Text.Megaparsec
|
||||||
import qualified Data.Algorithm.Diff as D
|
import qualified Data.Algorithm.Diff as D
|
||||||
import Hledger.Data.AutoTransaction (runModifierTransaction)
|
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
rewritemode = hledgerCommandMode
|
||||||
cmdmode = hledgerCommandMode
|
|
||||||
[here| rewrite
|
[here| rewrite
|
||||||
Print all transactions, adding custom postings to the matched ones.
|
Print all transactions, adding custom postings to the matched ones.
|
||||||
|
|
||||||
@ -166,22 +159,19 @@ https://github.com/simonmichael/hledger/issues/99
|
|||||||
-- TODO interpolating match groups in replacement
|
-- TODO interpolating match groups in replacement
|
||||||
-- TODO allow using this on unbalanced entries, eg to rewrite while editing
|
-- TODO allow using this on unbalanced entries, eg to rewrite while editing
|
||||||
|
|
||||||
main :: IO ()
|
rewrite opts@CliOpts{rawopts_=rawopts,reportopts_=ropts} j@Journal{jtxns=ts} = do
|
||||||
main = do
|
|
||||||
opts@CliOpts{rawopts_=rawopts,reportopts_=ropts} <- getHledgerCliOpts cmdmode
|
|
||||||
d <- getCurrentDay
|
d <- getCurrentDay
|
||||||
let q = queryFromOpts d ropts
|
let q = queryFromOpts d ropts
|
||||||
modifier <- modifierTransactionFromOpts rawopts
|
modifier <- modifierTransactionFromOpts rawopts
|
||||||
withJournalDo opts $ \opts' j@Journal{jtxns=ts} -> do
|
-- create re-writer
|
||||||
-- create re-writer
|
let modifiers = modifier : jmodifiertxns j
|
||||||
let modifiers = modifier : jmodifiertxns j
|
-- Note that some query matches require transaction. Thus modifiers
|
||||||
-- Note that some query matches require transaction. Thus modifiers
|
-- pipeline should include txnTieKnot on every step.
|
||||||
-- pipeline should include txnTieKnot on every step.
|
modifier' = foldr (flip (.) . fmap txnTieKnot . runModifierTransaction q) id modifiers
|
||||||
modifier' = foldr (flip (.) . fmap txnTieKnot . runModifierTransaction q) id modifiers
|
-- rewrite matched transactions
|
||||||
-- rewrite matched transactions
|
let j' = j{jtxns=map modifier' ts}
|
||||||
let j' = j{jtxns=map modifier' ts}
|
-- run the print command, showing all transactions
|
||||||
-- 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.Text -> IO Posting
|
||||||
postingp' t = runErroringJournalParser (postingp Nothing <* eof) t' >>= \case
|
postingp' t = runErroringJournalParser (postingp Nothing <* eof) t' >>= \case
|
||||||
@ -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)
|
[hledger-check.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-check.hs)
|
||||||
checks more powerful account balance assertions.
|
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
|
### tags
|
||||||
|
|
||||||
[hledger-tags.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-tags.hs)
|
[hledger-tags.hs](https://github.com/simonmichael/hledger/blob/master/bin/hledger-tags.hs)
|
||||||
|
|||||||
@ -685,6 +685,9 @@ Print the one posting whose transaction description is closest to DESC,
|
|||||||
in the style of the register command.
|
in the style of the register command.
|
||||||
Helps ledger-autosync detect already-seen transactions when importing.
|
Helps ledger-autosync detect already-seen transactions when importing.
|
||||||
|
|
||||||
|
## rewrite
|
||||||
|
Print all transactions, adding custom postings to the matched ones.
|
||||||
|
|
||||||
## stats
|
## stats
|
||||||
Show some journal statistics.
|
Show some journal statistics.
|
||||||
|
|
||||||
|
|||||||
@ -120,6 +120,7 @@ library:
|
|||||||
- Hledger.Cli.Commands.Printunique
|
- Hledger.Cli.Commands.Printunique
|
||||||
- Hledger.Cli.Commands.Register
|
- Hledger.Cli.Commands.Register
|
||||||
- Hledger.Cli.Commands.Registermatch
|
- Hledger.Cli.Commands.Registermatch
|
||||||
|
- Hledger.Cli.Commands.Rewrite
|
||||||
- Hledger.Cli.Commands.Stats
|
- Hledger.Cli.Commands.Stats
|
||||||
- Hledger.Cli.CompoundBalanceCommand
|
- Hledger.Cli.CompoundBalanceCommand
|
||||||
- Text.Tabular.AsciiWide
|
- Text.Tabular.AsciiWide
|
||||||
@ -130,6 +131,7 @@ library:
|
|||||||
- cmdargs >=0.10 && <0.11
|
- cmdargs >=0.10 && <0.11
|
||||||
- csv
|
- csv
|
||||||
- data-default >=0.5
|
- data-default >=0.5
|
||||||
|
- Diff
|
||||||
- hashable >=1.2.4
|
- hashable >=1.2.4
|
||||||
- haskeline >=0.6 && <=0.8
|
- haskeline >=0.6 && <=0.8
|
||||||
- HUnit
|
- HUnit
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user