From bab6ec041b64169cb13681dadd4b57c46c7dcb44 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 27 Feb 2014 11:40:41 -0800 Subject: [PATCH] cli: be more robust at finding hledger-* add-ons Previously executables with eg digits in their name were ignored. It now finds all files beginning with hledger-, optionally ending with .hs or .lhs, and with no other dots in the name. As before, we don't check for executable permission (performance ?). --- hledger/Hledger/Cli/Options.hs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/hledger/Hledger/Cli/Options.hs b/hledger/Hledger/Cli/Options.hs index f13fdbdc6..987d1fc69 100644 --- a/hledger/Hledger/Cli/Options.hs +++ b/hledger/Hledger/Cli/Options.hs @@ -554,22 +554,25 @@ getHledgerExesInPath :: IO [String] getHledgerExesInPath = do pathdirs <- splitOn ":" `fmap` getEnvSafe "PATH" pathfiles <- concat `fmap` mapM getDirectoryContentsSafe pathdirs - let hledgernamed = nub $ sort $ filter isHledgerNamed pathfiles + let hledgernamed = nub $ sort $ filter isHledgerExeName pathfiles -- hledgerexes <- filterM isExecutable hledgernamed return hledgernamed -- isExecutable f = getPermissions f >>= (return . executable) -isHledgerNamed = isRight . parsewith (do +isHledgerExeName = isRight . parsewith hledgerexenamep + where + hledgerexenamep = do string progname char '-' - many1 (letter <|> char '-') - optional $ (string ".hs" <|> string ".lhs") + many1 (noneOf ".") + optional (string ".hs" <|> string ".lhs") eof - ) getEnvSafe v = getEnv v `C.catch` (\(_::C.IOException) -> return "") -getDirectoryContentsSafe d = getDirectoryContents d `C.catch` (\(_::C.IOException) -> return []) + +getDirectoryContentsSafe d = + (filter (not . (`elem` [".",".."])) `fmap` getDirectoryContents d) `C.catch` (\(_::C.IOException) -> return []) -- | Raise an error, showing the specified message plus a hint about --help. optserror = error' . (++ " (run with --help for usage)")