From d2d7dbbce077a491f7f7dc3441453178f9831953 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 12 Sep 2017 19:51:27 -0700 Subject: [PATCH] cli: make rewrite a builtin command --- hledger/Hledger/Cli/Commands.hs | 2 + .../Hledger/Cli/Commands/Rewrite.hs | 56 ++++++++----------- hledger/doc/addons.m4.md | 5 -- hledger/doc/commands.m4.md | 3 + hledger/package.yaml | 2 + 5 files changed, 30 insertions(+), 38 deletions(-) rename bin/hledger-rewrite.hs => hledger/Hledger/Cli/Commands/Rewrite.hs (88%) diff --git a/hledger/Hledger/Cli/Commands.hs b/hledger/Hledger/Cli/Commands.hs index 88e5fb00d..261362efd 100644 --- a/hledger/Hledger/Cli/Commands.hs +++ b/hledger/Hledger/Cli/Commands.hs @@ -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 diff --git a/bin/hledger-rewrite.hs b/hledger/Hledger/Cli/Commands/Rewrite.hs similarity index 88% rename from bin/hledger-rewrite.hs rename to hledger/Hledger/Cli/Commands/Rewrite.hs index 6f47feb89..592fc9e1a 100755 --- a/bin/hledger-rewrite.hs +++ b/hledger/Hledger/Cli/Commands/Rewrite.hs @@ -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,22 +159,19 @@ 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 - -- pipeline should include txnTieKnot on every step. - modifier' = foldr (flip (.) . fmap txnTieKnot . runModifierTransaction q) id modifiers - -- 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' + -- create re-writer + let modifiers = modifier : jmodifiertxns j + -- Note that some query matches require transaction. Thus modifiers + -- pipeline should include txnTieKnot on every step. + modifier' = foldr (flip (.) . fmap txnTieKnot . runModifierTransaction q) id modifiers + -- 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' postingp' :: T.Text -> IO Posting postingp' t = runErroringJournalParser (postingp Nothing <* eof) t' >>= \case diff --git a/hledger/doc/addons.m4.md b/hledger/doc/addons.m4.md index 23eca7448..63ea08bdc 100644 --- a/hledger/doc/addons.m4.md +++ b/hledger/doc/addons.m4.md @@ -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) diff --git a/hledger/doc/commands.m4.md b/hledger/doc/commands.m4.md index be8eb2543..a707f5442 100644 --- a/hledger/doc/commands.m4.md +++ b/hledger/doc/commands.m4.md @@ -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. diff --git a/hledger/package.yaml b/hledger/package.yaml index 5101701b0..c28fce5ff 100644 --- a/hledger/package.yaml +++ b/hledger/package.yaml @@ -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