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, defaultJournalPath,
requireJournalFileExists, requireJournalFileExists,
ensureJournalFileExists, ensureJournalFileExists,
journalEnvVar,
-- journalEnvVar2,
journalDefaultFilename,
-- * Journal parsing -- * Journal parsing
runExceptT, runExceptT,
@ -110,6 +113,7 @@ module Hledger.Read (
-- * Misc -- * Misc
saveLatestDates, saveLatestDates,
saveLatestDatesForFiles, saveLatestDatesForFiles,
isWindowsUnsafeDotPath,
-- * Re-exported -- * Re-exported
JournalReader.tmpostingrulep, JournalReader.tmpostingrulep,
@ -141,7 +145,7 @@ import qualified Data.Text as T
import qualified Data.Text.IO as T import qualified Data.Text.IO as T
import Data.Time (Day) import Data.Time (Day)
import Safe (headDef, headMay) import Safe (headDef, headMay)
import System.Directory (doesFileExist, getHomeDirectory) import System.Directory (doesFileExist)
import System.Environment (getEnv) import System.Environment (getEnv)
import System.FilePath ((<.>), (</>), splitDirectories, splitFileName, takeFileName) import System.FilePath ((<.>), (</>), splitDirectories, splitFileName, takeFileName)
import System.Info (os) import System.Info (os)
@ -196,7 +200,7 @@ defaultJournalPath = do
`C.catch` (\(_::C.IOException) -> getEnv journalEnvVar2 `C.catch` (\(_::C.IOException) -> getEnv journalEnvVar2
`C.catch` (\(_::C.IOException) -> return "")) `C.catch` (\(_::C.IOException) -> return ""))
defpath = do defpath = do
home <- getHomeDirectory `C.catch` (\(_::C.IOException) -> return "") home <- fromMaybe "" <$> getHomeSafe
return $ home </> journalDefaultFilename return $ home </> journalDefaultFilename
-- | A file path optionally prefixed by a reader name and colon -- | A file path optionally prefixed by a reader name and colon

View File

@ -33,6 +33,7 @@ module Hledger.Utils.IO (
getCurrentZonedTime, getCurrentZonedTime,
-- * Files -- * Files
getHomeSafe,
embedFileRelative, embedFileRelative,
expandHomePath, expandHomePath,
expandPath, expandPath,
@ -294,6 +295,10 @@ getCurrentZonedTime = do
-- Files -- 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. -- | Expand a tilde (representing home directory) at the start of a file path.
-- ~username is not supported. Can raise an error. -- ~username is not supported. Can raise an error.
expandHomePath :: FilePath -> IO FilePath expandHomePath :: FilePath -> IO FilePath