@@ -25,6 +25,7 @@ module Hledger.Read (
25
25
readJournalFiles ,
26
26
readJournalFile ,
27
27
requireJournalFileExists ,
28
+ requireJournalFileExists' ,
28
29
ensureJournalFileExists ,
29
30
30
31
-- * Journal parsing
@@ -172,32 +173,45 @@ readJournalFile iopts prefixedfile = do
172
173
let
173
174
(mfmt, f) = splitReaderPrefix prefixedfile
174
175
iopts' = iopts{mformat_= asum [mfmt, mformat_ iopts]}
175
- requireJournalFileExists f
176
- t <- readFileOrStdinPortably f
177
- -- <- T.readFile f -- or without line ending translation, for testing
178
- ej <- readJournal iopts' (Just f) t
179
- case ej of
180
- Left e -> return $ Left e
181
- Right j | new_ iopts -> do
182
- ds <- previousLatestDates f
183
- let (newj, newds) = journalFilterSinceLatestDates ds j
184
- when (new_save_ iopts && not (null newds)) $ saveLatestDates newds f
185
- return $ Right newj
186
- Right j -> return $ Right j
176
+ exists <- requireJournalFileExists' f
177
+ case exists of
178
+ Left e -> return $ Left e
179
+ Right _ -> do
180
+ t <- readFileOrStdinPortably f
181
+ -- <- T.readFile f -- or without line ending translation, for testing
182
+ ej <- readJournal iopts' (Just f) t
183
+ case ej of
184
+ Left e -> return $ Left e
185
+ Right j | new_ iopts -> do
186
+ ds <- previousLatestDates f
187
+ let (newj, newds) = journalFilterSinceLatestDates ds j
188
+ when (new_save_ iopts && not (null newds)) $ saveLatestDates newds f
189
+ return $ Right newj
190
+ Right j -> return $ Right j
187
191
188
192
--- ** utilities
189
193
190
194
-- | If the specified journal file does not exist (and is not "-"),
191
195
-- give a helpful error and quit.
192
196
requireJournalFileExists :: FilePath -> IO ()
193
- requireJournalFileExists " -" = return ()
194
197
requireJournalFileExists f = do
198
+ res <- requireJournalFileExists' f
199
+ either (\ e -> hPutStr stderr e >> exitFailure) pure res
200
+
201
+ -- | If the specified journal file does not exist (and is not "-"),
202
+ -- give a helpful error.
203
+ requireJournalFileExists' :: FilePath -> IO (Either String () )
204
+ requireJournalFileExists' " -" = return $ Right ()
205
+ requireJournalFileExists' f = do
195
206
exists <- doesFileExist f
196
- unless exists $ do -- XXX might not be a journal file
197
- hPutStr stderr $ " The hledger journal file \" " <> f <> " \" was not found.\n "
198
- hPutStr stderr " Please create it first, eg with \" hledger add\" or a text editor.\n "
199
- hPutStr stderr " Or, specify an existing journal file with -f or LEDGER_FILE.\n "
200
- exitFailure
207
+ if exists then
208
+ return $ Right ()
209
+ else
210
+ return $ Left $ unlines [ " The hledger journal file \" " <> f <> " \" was not found."
211
+ , " Please create it first, eg with \" hledger add\" or a text editor."
212
+ , " Or, specify an existing journal file with -f or LEDGER_FILE."
213
+ ]
214
+
201
215
202
216
-- | Ensure there is a journal file at the given path, creating an empty one if needed.
203
217
-- On Windows, also ensure that the path contains no trailing dots
0 commit comments