Skip to content

Commit

Permalink
Show command-line options if requested. Fixes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
apauley committed Apr 15, 2019
1 parent fbd0779 commit 1b83eeb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
19 changes: 13 additions & 6 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Hledger.Flow.Common
import Hledger.Flow.Reports
import Hledger.Flow.CSVImport

type SubcommandParams = (Maybe FilePath, Bool, Bool)
type SubcommandParams = (Maybe FilePath, Bool, Bool, Bool)
data Command = Version (Maybe Text) | Import SubcommandParams | Report SubcommandParams deriving (Show)

main :: IO ()
Expand All @@ -26,24 +26,31 @@ main = do
Report subParams -> toReportOptions subParams >>= generateReports

toImportOptions :: SubcommandParams -> IO IT.ImportOptions
toImportOptions (maybeBaseDir, verbose, sequential) = do
toImportOptions (maybeBaseDir, verbose, showOpts, sequential) = do
bd <- dirOrPwd maybeBaseDir
return IT.ImportOptions {IT.baseDir = bd, IT.verbose = verbose, IT.sequential = sequential}
return IT.ImportOptions { IT.baseDir = bd
, IT.verbose = verbose
, IT.showOptions = showOpts
, IT.sequential = sequential }

toReportOptions :: SubcommandParams -> IO RT.ReportOptions
toReportOptions (maybeBaseDir, verbose, sequential) = do
toReportOptions (maybeBaseDir, verbose, showOpts, sequential) = do
bd <- dirOrPwd maybeBaseDir
return RT.ReportOptions {RT.baseDir = bd, RT.verbose = verbose, RT.sequential = sequential}
return RT.ReportOptions { RT.baseDir = bd
, RT.verbose = verbose
, RT.showOptions = showOpts
, RT.sequential = sequential }

parser :: Parser Command
parser = fmap Import (subcommand "import" "Converts CSV transactions into categorised journal files" subcommandParser)
<|> fmap Report (subcommand "report" "Generate Reports" subcommandParser)
<|> fmap Version (subcommand "version" "Display version information" noArgs)

subcommandParser :: Parser SubcommandParams
subcommandParser = (,,)
subcommandParser = (,,,)
<$> optional (argPath "basedir" "The hledger-flow base directory")
<*> switch (long "verbose" <> short 'v' <> help "Print more verbose output")
<*> switch (long "show-options" <> help "Print the options this program will run with")
<*> switch (long "sequential" <> help "Disable parallel processing")

noArgs :: Parser (Maybe Text)
Expand Down
1 change: 1 addition & 0 deletions src/Hledger/Flow/CSVImport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ importCSVs opts = sh (
do
ch <- liftIO newTChanIO
logHandle <- fork $ consoleChannelLoop ch
liftIO $ if (showOptions opts) then channelOut ch (repr opts) else return ()
liftIO $ logVerbose opts ch "Starting import"
(journals, diff) <- time $ liftIO $ importCSVs' opts ch
liftIO $ channelOut ch $ format ("Imported "%d%" journals in "%s) (length journals) $ repr diff
Expand Down
9 changes: 6 additions & 3 deletions src/Hledger/Flow/Import/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import Turtle
import Prelude hiding (FilePath, putStrLn)
import Hledger.Flow.Types

data ImportOptions = ImportOptions { baseDir :: FilePath, verbose :: Bool, sequential :: Bool }
data ImportOptions = ImportOptions { baseDir :: FilePath
, verbose :: Bool
, showOptions :: Bool
, sequential :: Bool }
deriving (Show)

instance HasVerbosity ImportOptions where
verbose (ImportOptions _ v _) = v
verbose (ImportOptions _ v _ _) = v

instance HasBaseDir ImportOptions where
baseDir (ImportOptions bd _ _) = bd
baseDir (ImportOptions bd _ _ _) = bd

data ImportDirs = ImportDirs { importDir :: FilePath
, ownerDir :: FilePath
Expand Down
9 changes: 6 additions & 3 deletions src/Hledger/Flow/Report/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import Turtle
import Prelude hiding (FilePath, putStrLn)
import Hledger.Flow.Types

data ReportOptions = ReportOptions { baseDir :: FilePath, verbose :: Bool, sequential :: Bool }
data ReportOptions = ReportOptions { baseDir :: FilePath
, verbose :: Bool
, showOptions :: Bool
, sequential :: Bool }
deriving (Show)

instance HasVerbosity ReportOptions where
verbose (ReportOptions _ v _) = v
verbose (ReportOptions _ v _ _) = v

instance HasBaseDir ReportOptions where
baseDir (ReportOptions bd _ _) = bd
baseDir (ReportOptions bd _ _ _) = bd
1 change: 1 addition & 0 deletions src/Hledger/Flow/Reports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ generateReports opts = sh (
do
ch <- liftIO newTChanIO
logHandle <- fork $ consoleChannelLoop ch
liftIO $ if (showOptions opts) then channelOut ch (repr opts) else return ()
(reports, diff) <- time $ liftIO $ generateReports' opts ch
liftIO $ channelOut ch $ format ("Generated "%d%" reports in "%s) (length reports) $ repr diff
liftIO $ terminateChannelLoop ch
Expand Down
2 changes: 1 addition & 1 deletion test/TestHelpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extraFiles = ["import/john/bogartbank/savings/2017-opening.journal"] :: [FilePat
hiddenFiles = [".hiddenfile", "checking/.DS_Store", "import/john/bogartbank/savings/1-in/.anotherhiddenfile", "import/john/bogartbank/checking/1-in/2018/.hidden"] :: [FilePath]

defaultOpts :: FilePath -> ImportOptions
defaultOpts bd = ImportOptions bd False False
defaultOpts bd = ImportOptions bd False False False

toJournals :: [FilePath] -> [FilePath]
toJournals = map (changePathAndExtension "3-journal" "journal")
Expand Down

0 comments on commit 1b83eeb

Please sign in to comment.