diff options
| author | Joey Hess <joeyh@joeyh.name> | 2016-12-24 15:14:05 -0400 |
|---|---|---|
| committer | Joey Hess <joeyh@joeyh.name> | 2016-12-24 15:14:05 -0400 |
| commit | 44bf67b7a2da75ef80e32d6409cc41a6ab8b6ffe (patch) | |
| tree | d955382901fd4ea2bec6412d5b652d9ac7ecbe23 /src/Utility/UserInfo.hs | |
| parent | fa974cfaaac31b25ae911b5e970507d0589e567b (diff) | |
GHC's fileSystemEncoding is used for all String IO, to avoid encoding-related crashes in eg, Propellor.Property.File.
Diffstat (limited to 'src/Utility/UserInfo.hs')
| -rw-r--r-- | src/Utility/UserInfo.hs | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/Utility/UserInfo.hs b/src/Utility/UserInfo.hs index c6010116..dd66c331 100644 --- a/src/Utility/UserInfo.hs +++ b/src/Utility/UserInfo.hs @@ -15,6 +15,8 @@ module Utility.UserInfo ( ) where import Utility.Env +import Utility.Data +import Utility.Exception import System.PosixCompat import Control.Applicative @@ -24,7 +26,7 @@ import Prelude - - getpwent will fail on LDAP or NIS, so use HOME if set. -} myHomeDir :: IO FilePath -myHomeDir = myVal env homeDirectory +myHomeDir = either giveup return =<< myVal env homeDirectory where #ifndef mingw32_HOST_OS env = ["HOME"] @@ -33,7 +35,7 @@ myHomeDir = myVal env homeDirectory #endif {- Current user's user name. -} -myUserName :: IO String +myUserName :: IO (Either String String) myUserName = myVal env userName where #ifndef mingw32_HOST_OS @@ -47,15 +49,15 @@ myUserGecos :: IO (Maybe String) #if defined(__ANDROID__) || defined(mingw32_HOST_OS) myUserGecos = return Nothing #else -myUserGecos = Just <$> myVal [] userGecos +myUserGecos = eitherToMaybe <$> myVal [] userGecos #endif -myVal :: [String] -> (UserEntry -> String) -> IO String +myVal :: [String] -> (UserEntry -> String) -> IO (Either String String) myVal envvars extract = go envvars where #ifndef mingw32_HOST_OS - go [] = extract <$> (getUserEntryForID =<< getEffectiveUserID) + go [] = Right . extract <$> (getUserEntryForID =<< getEffectiveUserID) #else - go [] = extract <$> error ("environment not set: " ++ show envvars) + go [] = return $ Left ("environment not set: " ++ show envvars) #endif - go (v:vs) = maybe (go vs) return =<< getEnv v + go (v:vs) = maybe (go vs) (return . Right) =<< getEnv v |
