| 1 |
{-# LANGUAGE NamedFieldPuns #-} |
| 2 |
{-# LANGUAGE TupleSections #-} |
| 3 |
{-# LANGUAGE LambdaCase #-} |
| 4 |
{-# LANGUAGE RecordWildCards #-} |
| 5 |
{-# LANGUAGE TemplateHaskell #-} |
| 6 |
|
| 7 |
module Hledger.Cli.Commands.Check ( |
| 8 |
checkmode |
| 9 |
,check |
| 10 |
) where |
| 11 |
|
| 12 |
import Data.Char (toLower) |
| 13 |
import Data.Either (partitionEithers) |
| 14 |
import Data.List (isPrefixOf, find, sort) |
| 15 |
import Control.Monad (forM_) |
| 16 |
import System.Console.CmdArgs.Explicit |
| 17 |
|
| 18 |
import Hledger |
| 19 |
import Hledger.Cli.CliOptions |
| 20 |
|
| 21 |
checkmode :: Mode RawOpts |
| 22 |
checkmode = hledgerCommandMode |
| 23 |
$(embedFileRelative "Hledger/Cli/Commands/Check.txt") |
| 24 |
[] |
| 25 |
cligeneralflagsgroups1 |
| 26 |
hiddenflags |
| 27 |
([], Just $ argsFlag "[CHECKS]") |
| 28 |
|
| 29 |
check :: CliOpts -> Journal -> IO () |
| 30 |
check copts@CliOpts{rawopts_} j = do |
| 31 |
let |
| 32 |
args = listofstringopt "args" rawopts_ |
| 33 |
-- reset the report spec that was generated by argsToCliOpts, |
| 34 |
-- since we are not using arguments as a query in the usual way |
| 35 |
copts' = cliOptsUpdateReportSpecWith (\ropts -> ropts{querystring_=[]}) copts |
| 36 |
|
| 37 |
case partitionEithers (map parseCheckArgument args) of |
| 38 |
(unknowns@(_:_), _) -> error' $ "These checks are unknown: "++unwords unknowns |
| 39 |
([], checks) -> forM_ (sort checks) $ runCheck copts' j |
| 40 |
|
| 41 |
-- | Regenerate this CliOpts' report specification, after updating its |
| 42 |
-- underlying report options with the given update function. |
| 43 |
-- This can raise an error if there is a problem eg due to missing or |
| 44 |
-- unparseable options data. See also updateReportSpecFromOpts. |
| 45 |
cliOptsUpdateReportSpecWith :: (ReportOpts -> ReportOpts) -> CliOpts -> CliOpts |
| 46 |
cliOptsUpdateReportSpecWith roptsupdate copts@CliOpts{reportspec_} = |
| 47 |
case updateReportSpecWith roptsupdate reportspec_ of |
| 48 |
Left e -> error' e -- PARTIAL: |
| 49 |
Right rs -> copts{reportspec_=rs} |
| 50 |
|
| 51 |
-- | A type of error check that we can perform on the data. |
| 52 |
-- If performing multiple checks, they will be performed in the order defined here, generally. |
| 53 |
-- (We report only the first failure, so the more useful checks should come first.) |
| 54 |
data Check = |
| 55 |
-- keep the order here synced with Check.md and Hledger.Data.JournalChecks.journalStrictChecks. |
| 56 |
-- done always |
| 57 |
Parseable |
| 58 |
| Autobalanced |
| 59 |
| Assertions -- unless -I is used |
| 60 |
-- done when --strict is used, or when specified with the check command |
| 61 |
| Balanced |
| 62 |
| Commodities |
| 63 |
| Accounts |
| 64 |
-- done when specified with the check command |
| 65 |
| Ordereddates |
| 66 |
| Payees |
| 67 |
| Tags |
| 68 |
| Recentassertions |
| 69 |
| Uniqueleafnames |
| 70 |
deriving (Read,Show,Eq,Enum,Bounded,Ord) |
| 71 |
|
| 72 |
-- | Parse the name (or a name prefix) of an error check, or return the name unparsed. |
| 73 |
-- Check names are conventionally all lower case, but this parses case insensitively. |
| 74 |
parseCheck :: String -> Either String Check |
| 75 |
parseCheck s = |
| 76 |
maybe (Left s) (Right . read) $ -- PARTIAL: read should not fail here |
| 77 |
find (s' `isPrefixOf`) $ checknames |
| 78 |
where |
| 79 |
s' = capitalise $ map toLower s |
| 80 |
checknames = map show [minBound..maxBound::Check] |
| 81 |
|
| 82 |
-- | Parse a check argument: a string which is the lower-case name of an error check, |
| 83 |
-- or a prefix thereof, followed by zero or more space-separated arguments for that check. |
| 84 |
parseCheckArgument :: String -> Either String (Check,[String]) |
| 85 |
parseCheckArgument s = |
| 86 |
dbg3 "check argument" $ |
| 87 |
((,checkargs)) <$> parseCheck checkname |
| 88 |
where |
| 89 |
(checkname:checkargs) = words' s |
| 90 |
|
| 91 |
-- XXX do all of these print on stderr ? |
| 92 |
-- | Run the named error check, possibly with some arguments, |
| 93 |
-- on this journal with these options. |
| 94 |
runCheck :: CliOpts -> Journal -> (Check,[String]) -> IO () |
| 95 |
runCheck _opts j (chck,_) = do |
| 96 |
let |
| 97 |
results = case chck of |
| 98 |
-- these checks are assumed to have passed earlier during journal parsing (if enabled): |
| 99 |
Parseable -> Right () |
| 100 |
Autobalanced -> Right () |
| 101 |
Balanced -> Right () |
| 102 |
Assertions -> Right () |
| 103 |
Accounts -> journalCheckAccounts j |
| 104 |
Commodities -> journalCheckCommodities j |
| 105 |
Ordereddates -> journalCheckOrdereddates j |
| 106 |
Payees -> journalCheckPayees j |
| 107 |
Recentassertions -> journalCheckRecentAssertions j |
| 108 |
Tags -> journalCheckTags j |
| 109 |
Uniqueleafnames -> journalCheckUniqueleafnames j |
| 110 |
|
| 111 |
case results of |
| 112 |
Right () -> return () |
| 113 |
Left err -> error' err |