check: new command incorporating check-dates, check-dupes, etc.

This is intended to work in three modes:

- hledger check: just run the standard data checks, like all other
commands but with no output on success. Equivalent to
hledger stats >/dev/null but simpler and platform-independent.

- hledger check --strict: run the standard + strict data checks,
like other commands in strict mode.

- hledger check CHECK1 CHECK2 ...: run the named checks. This allows
running more or less checks than the default or strict mode, or
a single check of interest. The arguments are standard lowercase names
for the checks. For now this command supports two checks: "dates" and
"leafnames". These are equivalent to the check-dates and check-dupes
commands which are now hidden and considered deprecated, though
still supported for the moment.

This command needs more work and I'm rushing it a little, but I think
it's the right direction and I'd like to put it out there to get
feedback.
This commit is contained in:
Simon Michael 2020-11-29 13:27:35 -08:00
parent 325a155f0b
commit 724fb9961d
7 changed files with 85 additions and 10 deletions

View File

@ -72,6 +72,7 @@ import Hledger.Cli.Commands.Balance
import Hledger.Cli.Commands.Balancesheet
import Hledger.Cli.Commands.Balancesheetequity
import Hledger.Cli.Commands.Cashflow
import Hledger.Cli.Commands.Check
import Hledger.Cli.Commands.Checkdates
import Hledger.Cli.Commands.Checkdupes
import Hledger.Cli.Commands.Close
@ -109,6 +110,7 @@ builtinCommands = [
,(balancesheetequitymode , balancesheetequity)
,(balancesheetmode , balancesheet)
,(cashflowmode , cashflow)
,(checkmode , check)
,(checkdatesmode , checkdates)
,(checkdupesmode , checkdupes)
,(closemode , close)
@ -166,9 +168,7 @@ commandsList progversion othercmds = [
,""
,"Data management:"
,"+autosync download/deduplicate/convert OFX data"
,"+check check more powerful balance assertions"
," check-dates check transactions are ordered by date"
," check-dupes check for accounts with the same leaf name"
," check check for various kinds of issue in the data"
," close (equity) generate balance-resetting transactions"
," diff compare account transactions in two journal files"
,"+interest generate interest transactions"

View File

@ -0,0 +1,67 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Hledger.Cli.Commands.Check (
checkmode
,check
) where
import Hledger
import Hledger.Cli.CliOptions
import Hledger.Cli.Commands.Checkdupes (checkdupes)
import Hledger.Cli.Commands.Checkdates (checkdates)
import System.Console.CmdArgs.Explicit
import Data.Either (partitionEithers)
import Data.Char (toUpper)
import Safe (readMay)
import Control.Monad (forM_)
checkmode :: Mode RawOpts
checkmode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Check.txt")
[]
[generalflagsgroup1]
hiddenflags
([], Just $ argsFlag "[CHECKS]")
check :: CliOpts -> Journal -> IO ()
check copts@CliOpts{..} j = do
let
args = listofstringopt "args" rawopts_
-- we must reset the report spec generated by argsToCliOpts
-- since we are not using arguments as a query in the usual way,
copts' =
case updateReportSpecFromOpts update reportspec_ of
Left e -> error' e
Right rs -> copts{reportspec_=rs}
where update ropts = ropts{querystring_=[]}
case partitionEithers (map parseCheck args) of
(unknowns@(_:_), _) -> error' $ "These checks are unknown: "++unwords unknowns
([], checks) -> forM_ checks $ runCheck copts' j
-- | A type of error check that we can perform on the data.
data Check =
Dates
| Leafnames
deriving (Read,Show,Eq)
-- | Parse the lower-case name of an error check, or return the name unparsed.
parseCheck :: String -> Either String Check
parseCheck s = maybe (Left s) Right $ readMay $ capitalise s
capitalise :: String -> String
capitalise (c:cs) = toUpper c : cs
capitalise s = s
-- | Parse a check argument: a string which is the lower-case name of an error check,
-- followed by zero or more space-separated arguments for that check.
-- parseCheckArgument :: String -> Either String (Check,[String])
runCheck :: CliOpts -> Journal -> Check -> IO ()
runCheck copts j =
\case
Dates -> checkdates copts j
Leafnames -> checkdupes copts j

View File

@ -0,0 +1,4 @@
check\
Check for various kinds of issue in the data.
_FLAGS

View File

@ -0,0 +1,4 @@
check
Check for various kinds of issue in the data.
_FLAGS

View File

@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 4517e1a53a08aa05c53bb06d8591b5591f3ae6f688bf39f809c8f774fbd1d41c
-- hash: 3b5d4e8c7e8f90c11eea9adcc8b197ce679113e8f72f7a02a060a328a43db663
name: hledger
version: 1.19.99
@ -66,6 +66,7 @@ extra-source-files:
Hledger/Cli/Commands/Balancesheet.txt
Hledger/Cli/Commands/Balancesheetequity.txt
Hledger/Cli/Commands/Cashflow.txt
Hledger/Cli/Commands/Check.txt
Hledger/Cli/Commands/Checkdates.txt
Hledger/Cli/Commands/Checkdupes.txt
Hledger/Cli/Commands/Close.txt
@ -122,6 +123,7 @@ library
Hledger.Cli.Commands.Balancesheet
Hledger.Cli.Commands.Balancesheetequity
Hledger.Cli.Commands.Cashflow
Hledger.Cli.Commands.Check
Hledger.Cli.Commands.Checkdates
Hledger.Cli.Commands.Checkdupes
Hledger.Cli.Commands.Close

View File

@ -1618,13 +1618,9 @@ _include_({{Hledger/Cli/Commands/Balancesheetequity.md}})
_include_({{Hledger/Cli/Commands/Cashflow.md}})
## check-dates
## check
_include_({{Hledger/Cli/Commands/Checkdates.md}})
## check-dupes
_include_({{Hledger/Cli/Commands/Checkdupes.md}})
_include_({{Hledger/Cli/Commands/Check.md}})
## close

View File

@ -60,6 +60,7 @@ extra-source-files:
- Hledger/Cli/Commands/Balancesheet.txt
- Hledger/Cli/Commands/Balancesheetequity.txt
- Hledger/Cli/Commands/Cashflow.txt
- Hledger/Cli/Commands/Check.txt
- Hledger/Cli/Commands/Checkdates.txt
- Hledger/Cli/Commands/Checkdupes.txt
- Hledger/Cli/Commands/Close.txt
@ -168,6 +169,7 @@ library:
- Hledger.Cli.Commands.Balancesheet
- Hledger.Cli.Commands.Balancesheetequity
- Hledger.Cli.Commands.Cashflow
- Hledger.Cli.Commands.Check
- Hledger.Cli.Commands.Checkdates
- Hledger.Cli.Commands.Checkdupes
- Hledger.Cli.Commands.Close