extra: hledger-rewrite script
This commit is contained in:
		
							parent
							
								
									f46972714d
								
							
						
					
					
						commit
						dc8b687056
					
				
							
								
								
									
										30
									
								
								extra/hledger-rewrite.hs
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										30
									
								
								extra/hledger-rewrite.hs
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,30 @@ | |||||||
|  | #!/usr/bin/env runhaskell | ||||||
|  | {-| | ||||||
|  | hledger-rewrite PATTERNS --add-posting "ACCT  AMTEXPR" ... | ||||||
|  | 
 | ||||||
|  | Skeleton for a minimal generic rewriter of journal entries. | ||||||
|  | Reads the default journal and prints the entries, like print, | ||||||
|  | but adds the specified postings to any entries matching PATTERNS. | ||||||
|  | 
 | ||||||
|  | |-} | ||||||
|  | 
 | ||||||
|  | import Hledger.Data.Types (Journal(..)) | ||||||
|  | import Hledger | ||||||
|  | import Hledger.Cli | ||||||
|  | 
 | ||||||
|  | main = do | ||||||
|  |   putStrLn "(-f option not supported)" | ||||||
|  |   opts <- getCliOpts (defCommandMode ["hledger-rewrite"]) | ||||||
|  |   withJournalDo opts $ | ||||||
|  |     \opts j@Journal{jtxns=ts} -> print' opts j{jtxns=map rewrite ts} | ||||||
|  |     where  | ||||||
|  |       rewrite t = if matched t then t{tpostings=tpostings t ++ newps t} | ||||||
|  |                                else t | ||||||
|  |       matched t = Acct "^income" `matchesTransaction` t | ||||||
|  |       newps t = [generatePosting t "(Reserve)" (`divideMixedAmount` 10)] | ||||||
|  |       generatePosting t acct amtfn = nullposting{paccount = accountNameWithoutPostingType acct | ||||||
|  |                                                 ,ptype    = accountNamePostingType acct | ||||||
|  |                                                 ,pamount  = amtfn amt | ||||||
|  |                                                 } | ||||||
|  |         where | ||||||
|  |           amt = pamount $ head $ filter (Acct "^income" `matchesPosting`) $ tpostings t | ||||||
| @ -18,6 +18,7 @@ module Hledger.Read ( | |||||||
|        requireJournalFileExists, |        requireJournalFileExists, | ||||||
|        ensureJournalFileExists, |        ensureJournalFileExists, | ||||||
|        -- * Parsers used elsewhere |        -- * Parsers used elsewhere | ||||||
|  |        postingp, | ||||||
|        accountname, |        accountname, | ||||||
|        amountp, |        amountp, | ||||||
|        amountp', |        amountp', | ||||||
|  | |||||||
| @ -29,6 +29,7 @@ module Hledger.Read.JournalReader ( | |||||||
|   datetime, |   datetime, | ||||||
|   code, |   code, | ||||||
|   accountname, |   accountname, | ||||||
|  |   postingp, | ||||||
|   amountp, |   amountp, | ||||||
|   amountp', |   amountp', | ||||||
|   mamountp', |   mamountp', | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user