lib: refactor, export getHomeSafe

This commit is contained in:
Simon Michael 2025-04-19 14:09:42 -10:00
parent 4881a0deaa
commit f37a4a7dcb
2 changed files with 11 additions and 2 deletions

View File

@ -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

View File

@ -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