Created
August 21, 2017 21:14
-
-
Save simonmichael/47bc2c6c2e001aa191e330957f433cb6 to your computer and use it in GitHub Desktop.
hledger-import WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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, QuasiQuotes, LambdaCase, DeriveTraversable, ViewPatterns #-} | |
import Control.Monad.Writer | |
--import Data.List (sortOn, foldl') | |
import Data.String.Here | |
--import qualified Data.Text as T | |
--import Text.Printf | |
--import Text.Megaparsec | |
--import qualified Data.Algorithm.Diff as D | |
-- hledger lib, cli and cmdargs utils | |
import Hledger.Cli hiding (outputflags) | |
------------------------------------------------------------------------------ | |
cmdmode = hledgerCommandMode | |
[here| import | |
Add new transactions from FILE to the main journal, | |
TODO: ignoring any that have already been imported. | |
FLAGS | |
Examples: | |
``` | |
hledger import somebank.csv | |
hledger -f project.journal import subproject.journal | |
``` | |
|] | |
[-- flagReq ["flag1"] ... | |
--,flagNone ["flag2"] ... | |
] | |
[generalflagsgroup1] | |
[] | |
([], Just $ argsFlag "FILE") | |
------------------------------------------------------------------------------ | |
main :: IO () | |
main = do | |
opts <- getHledgerCliOpts cmdmode | |
let args = listofstringopt "args" $ rawopts_ opts | |
case args of | |
[newfile] -> do | |
withJournalDo opts $ \opts' mainj -> do | |
enewj <- readJournalFile Nothing Nothing False newfile | |
case enewj of | |
Left e -> error' e | |
Right newj -> do | |
let newtxns = jtxns newj | |
mainjs <- forM newtxns $ journalAddTransaction mainj opts' | |
putStrLn "After import:" | |
print $ jtxns $ last mainjs | |
-- print' opts' $ last mainjs -- prints nothing for some reason | |
_ -> error' "please provide one file path argument" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment