2022-12-11 17:51:11 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Main (main) where
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
import Internal.Config
|
|
|
|
import Internal.Database.Ops
|
|
|
|
import Internal.Insert
|
|
|
|
import Internal.Types
|
|
|
|
import Internal.Utils
|
|
|
|
import Options.Applicative
|
|
|
|
import RIO
|
|
|
|
import RIO.FilePath
|
|
|
|
import qualified RIO.Text as T
|
2022-12-11 18:34:05 -05:00
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
main :: IO ()
|
|
|
|
main = parse =<< execParser o
|
|
|
|
where
|
2023-01-05 22:16:06 -05:00
|
|
|
o =
|
|
|
|
info
|
|
|
|
(options <**> helper)
|
|
|
|
( fullDesc
|
|
|
|
<> progDesc "Pwn your budget"
|
|
|
|
<> header "pwncash - your budget, your life"
|
|
|
|
)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
data Options = Options FilePath Mode
|
|
|
|
|
2023-01-05 22:16:06 -05:00
|
|
|
data Mode
|
|
|
|
= Reset
|
2022-12-11 17:51:11 -05:00
|
|
|
| DumpCurrencies
|
|
|
|
| DumpAccounts
|
|
|
|
| DumpAccountKeys
|
|
|
|
| Sync
|
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
configFile :: Parser FilePath
|
2023-01-05 22:16:06 -05:00
|
|
|
configFile =
|
|
|
|
strOption
|
|
|
|
( long "config"
|
|
|
|
<> short 'c'
|
|
|
|
<> metavar "CONFIG"
|
|
|
|
<> value "main.dhall"
|
|
|
|
<> help "config file to use"
|
|
|
|
)
|
2022-12-11 18:34:05 -05:00
|
|
|
|
2022-12-11 17:51:11 -05:00
|
|
|
options :: Parser Options
|
2023-01-05 22:16:06 -05:00
|
|
|
options =
|
|
|
|
getConf reset
|
|
|
|
<|> getConf dumpCurrencies
|
|
|
|
<|> getConf dumpAccounts
|
|
|
|
<|> getConf dumpAccountKeys
|
|
|
|
<|> getConf sync
|
2022-12-11 18:34:05 -05:00
|
|
|
where
|
|
|
|
getConf m = Options <$> configFile <*> m
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
reset :: Parser Mode
|
2023-01-05 22:16:06 -05:00
|
|
|
reset =
|
|
|
|
flag'
|
|
|
|
Reset
|
|
|
|
( long "reset"
|
|
|
|
<> short 'R'
|
|
|
|
<> help "Reset the database"
|
|
|
|
)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
dumpCurrencies :: Parser Mode
|
2023-01-05 22:16:06 -05:00
|
|
|
dumpCurrencies =
|
|
|
|
flag'
|
|
|
|
DumpCurrencies
|
|
|
|
( long "currencies"
|
|
|
|
<> short 'C'
|
|
|
|
<> help "Dump all currencies in the configuration"
|
|
|
|
)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
dumpAccounts :: Parser Mode
|
2023-01-05 22:16:06 -05:00
|
|
|
dumpAccounts =
|
|
|
|
flag'
|
|
|
|
DumpAccounts
|
|
|
|
( long "accounts"
|
|
|
|
<> short 'A'
|
|
|
|
<> help "Dump all accounts in the configuration"
|
|
|
|
)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
-- TODO 'alias' is a better name for these
|
2022-12-11 18:34:05 -05:00
|
|
|
dumpAccountKeys :: Parser Mode
|
2023-01-05 22:16:06 -05:00
|
|
|
dumpAccountKeys =
|
|
|
|
flag'
|
|
|
|
DumpAccountKeys
|
|
|
|
( long "account_keys"
|
|
|
|
<> short 'K'
|
|
|
|
<> help "Dump all account keys/aliases"
|
|
|
|
)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
sync :: Parser Mode
|
2023-01-05 22:16:06 -05:00
|
|
|
sync =
|
|
|
|
flag'
|
|
|
|
Sync
|
|
|
|
( long "sync"
|
|
|
|
<> short 'S'
|
|
|
|
<> help "Sync config to database"
|
|
|
|
)
|
2022-12-11 17:51:11 -05:00
|
|
|
|
|
|
|
parse :: Options -> IO ()
|
2023-01-05 22:16:06 -05:00
|
|
|
parse (Options c Reset) = do
|
2022-12-11 18:53:54 -05:00
|
|
|
config <- readConfig c
|
|
|
|
migrate_ (sqlConfig config) nukeTables
|
2023-01-05 22:16:06 -05:00
|
|
|
parse (Options c DumpAccounts) = runDumpAccounts c
|
2022-12-11 18:34:05 -05:00
|
|
|
parse (Options c DumpAccountKeys) = runDumpAccountKeys c
|
2023-01-05 22:16:06 -05:00
|
|
|
parse (Options c DumpCurrencies) = runDumpCurrencies c
|
|
|
|
parse (Options c Sync) = runSync c
|
2022-12-11 18:34:05 -05:00
|
|
|
|
|
|
|
runDumpCurrencies :: FilePath -> IO ()
|
|
|
|
runDumpCurrencies c = do
|
|
|
|
cs <- currencies <$> readConfig c
|
2022-12-11 17:51:11 -05:00
|
|
|
putStrLn $ T.unpack $ T.unlines $ fmap fmt cs
|
|
|
|
where
|
2023-01-05 22:16:06 -05:00
|
|
|
fmt Currency {curSymbol = s, curFullname = f} =
|
2022-12-11 17:51:11 -05:00
|
|
|
T.concat [s, ": ", f]
|
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
runDumpAccounts :: FilePath -> IO ()
|
|
|
|
runDumpAccounts c = do
|
|
|
|
ar <- accounts <$> readConfig c
|
2022-12-11 17:51:11 -05:00
|
|
|
mapM_ (\(h, f) -> printTree h $ f ar) ps
|
|
|
|
where
|
2023-01-05 22:16:06 -05:00
|
|
|
ps =
|
|
|
|
[ ("Assets", arAssets)
|
|
|
|
, ("Equity", arEquity)
|
|
|
|
, ("Expenses", arExpenses)
|
|
|
|
, ("Income", arIncome)
|
|
|
|
, ("Liabilities", arLiabilities)
|
|
|
|
]
|
2022-12-11 17:51:11 -05:00
|
|
|
printTree h ts = do
|
|
|
|
putStrLn h
|
|
|
|
mapM (go 1) ts
|
|
|
|
go i (Placeholder d n cs) = do
|
|
|
|
printAcnt i d n
|
|
|
|
mapM_ (go (i + 1)) cs
|
|
|
|
go i (Account d n) = printAcnt i d n
|
|
|
|
printAcnt i d n = do
|
|
|
|
let ind = T.replicate (i * 2) " "
|
|
|
|
putStrLn $ T.unpack $ T.concat [ind, n, ": ", d]
|
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
runDumpAccountKeys :: FilePath -> IO ()
|
|
|
|
runDumpAccountKeys c = do
|
|
|
|
ar <- accounts <$> readConfig c
|
2023-01-05 22:16:06 -05:00
|
|
|
let ks =
|
|
|
|
paths2IDs $
|
|
|
|
fmap (double . fst) $
|
|
|
|
concatMap (t3 . uncurry tree2Records) $
|
|
|
|
flattenAcntRoot ar
|
2022-12-11 17:51:11 -05:00
|
|
|
mapM_ (uncurry printPair) ks
|
|
|
|
where
|
|
|
|
printPair i p = do
|
|
|
|
putStrLn $ T.unpack $ T.concat [acntPath2Text p, ": ", i]
|
|
|
|
t3 (_, _, x) = x
|
|
|
|
double x = (x, x)
|
|
|
|
|
2022-12-11 18:34:05 -05:00
|
|
|
runSync :: FilePath -> IO ()
|
|
|
|
runSync c = do
|
|
|
|
config <- readConfig c
|
2022-12-11 18:53:54 -05:00
|
|
|
migrate_ (sqlConfig config) $ do
|
2022-12-11 17:51:11 -05:00
|
|
|
s <- getDBState config
|
2022-12-11 18:34:05 -05:00
|
|
|
flip runReaderT (s $ takeDirectory c) $ do
|
2022-12-11 17:51:11 -05:00
|
|
|
insertBudget $ budget config
|
|
|
|
insertStatements config
|
|
|
|
showBalances
|