From 0ade323d2ac6adf4c364bc9147337917735996f1 Mon Sep 17 00:00:00 2001 From: Joseph Weston Date: Mon, 23 Jul 2018 11:39:01 +0200 Subject: [PATCH] lib: use glob matching if filename in 'include' directive is a glob pattern --- hledger-lib/Hledger/Read/JournalReader.hs | 14 +++++++++----- hledger-lib/package.yaml | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index d5ddbc1d9..204e93465 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -94,6 +94,7 @@ import Text.Megaparsec.Char import Text.Megaparsec.Custom import Text.Printf import System.FilePath +import System.FilePath.Glob hiding (match) import Hledger.Data import Hledger.Read.Common @@ -189,15 +190,18 @@ includedirectivep :: MonadIO m => JournalParser m () includedirectivep = do string "include" lift (skipSome spacenonewline) - filename <- T.unpack <$> takeWhileP Nothing (/= '\n') -- don't consume newline yet + fileglob <- T.unpack <$> takeWhileP Nothing (/= '\n') -- don't consume newline yet parentpos <- getPosition curdir <- lift $ expandPath (takeDirectory $ sourceName parentpos) "" - `orRethrowIOError` (show parentpos ++ " locating " ++ filename) - -- correctly handles case when 'filename' is absolute - let filepaths = [curdir filename] - -- read child inputs + `orRethrowIOError` (show parentpos ++ " locating " ++ fileglob) + + filepaths <- if isLiteral (compile fileglob) + -- and globDir1 correctly handle case when 'fileglob' is absolute + then pure [curdir fileglob] + else liftIO $ globDir1 (compile fileglob) curdir + forM_ filepaths $ parseChild parentpos void newline diff --git a/hledger-lib/package.yaml b/hledger-lib/package.yaml index bf2085fde..ab2a8c8f0 100644 --- a/hledger-lib/package.yaml +++ b/hledger-lib/package.yaml @@ -72,6 +72,7 @@ dependencies: - utf8-string >=0.3.5 - HUnit - extra +- Glob >= 0.9 # for ledger-parse: #- parsers >=0.5 #- system-filepath