From f37a4a7dcb1c070680439ee436f54b9021ab009a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 19 Apr 2025 14:09:42 -1000 Subject: [PATCH] lib: refactor, export getHomeSafe --- hledger-lib/Hledger/Read.hs | 8 ++++++-- hledger-lib/Hledger/Utils/IO.hs | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index 3028438ba..6f20677d0 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -92,6 +92,9 @@ module Hledger.Read ( defaultJournalPath, requireJournalFileExists, ensureJournalFileExists, + journalEnvVar, + -- journalEnvVar2, + journalDefaultFilename, -- * Journal parsing runExceptT, @@ -110,6 +113,7 @@ module Hledger.Read ( -- * Misc saveLatestDates, saveLatestDatesForFiles, + isWindowsUnsafeDotPath, -- * Re-exported JournalReader.tmpostingrulep, @@ -141,7 +145,7 @@ import qualified Data.Text as T import qualified Data.Text.IO as T import Data.Time (Day) import Safe (headDef, headMay) -import System.Directory (doesFileExist, getHomeDirectory) +import System.Directory (doesFileExist) import System.Environment (getEnv) import System.FilePath ((<.>), (), splitDirectories, splitFileName, takeFileName) import System.Info (os) @@ -196,7 +200,7 @@ defaultJournalPath = do `C.catch` (\(_::C.IOException) -> getEnv journalEnvVar2 `C.catch` (\(_::C.IOException) -> return "")) defpath = do - home <- getHomeDirectory `C.catch` (\(_::C.IOException) -> return "") + home <- fromMaybe "" <$> getHomeSafe return $ home journalDefaultFilename -- | A file path optionally prefixed by a reader name and colon diff --git a/hledger-lib/Hledger/Utils/IO.hs b/hledger-lib/Hledger/Utils/IO.hs index c2b4446f1..d91e0096d 100644 --- a/hledger-lib/Hledger/Utils/IO.hs +++ b/hledger-lib/Hledger/Utils/IO.hs @@ -33,6 +33,7 @@ module Hledger.Utils.IO ( getCurrentZonedTime, -- * Files + getHomeSafe, embedFileRelative, expandHomePath, expandPath, @@ -294,6 +295,10 @@ getCurrentZonedTime = do -- Files +-- | Like getHomeDirectory, but in case of IO error (home directory not found, not understood, etc.), returns "". +getHomeSafe :: IO (Maybe FilePath) +getHomeSafe = fmap Just getHomeDirectory `catch` (\(_ :: IOException) -> return Nothing) + -- | Expand a tilde (representing home directory) at the start of a file path. -- ~username is not supported. Can raise an error. expandHomePath :: FilePath -> IO FilePath