aboutsummaryrefslogtreecommitdiff
path: root/Utility/UserInfo.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2016-06-08 15:04:15 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2016-06-08 15:04:15 -0400
commita5681bf82cb2f43c4615529fb5eca3dd1f15268a (patch)
treed49ef2805c93f144f283e5d8209b44e0bb03ad65 /Utility/UserInfo.hs
parentcb25507c65962391a5061c1915541008d930df46 (diff)
also avoid crashing in most circumstances if unable to determine the username
Mostly the username is only used for the git committer or other display purposes, and we can just fall back to a dummy value in these cases. The only remaining place where an error is thrown is when starting local pairing, which needs the username to be known.
Diffstat (limited to 'Utility/UserInfo.hs')
-rw-r--r--Utility/UserInfo.hs16
1 files changed, 8 insertions, 8 deletions
diff --git a/Utility/UserInfo.hs b/Utility/UserInfo.hs
index c2edde24e..ec0b0d0b2 100644
--- a/Utility/UserInfo.hs
+++ b/Utility/UserInfo.hs
@@ -15,7 +15,7 @@ module Utility.UserInfo (
) where
import Utility.Env
-import Utility.Exception
+import Utility.Data
import System.PosixCompat
import Control.Applicative
@@ -25,7 +25,7 @@ import Prelude
-
- getpwent will fail on LDAP or NIS, so use HOME if set. -}
myHomeDir :: IO FilePath
-myHomeDir = myVal env homeDirectory
+myHomeDir = either error return =<< myVal env homeDirectory
where
#ifndef mingw32_HOST_OS
env = ["HOME"]
@@ -34,7 +34,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
@@ -48,15 +48,15 @@ myUserGecos :: IO (Maybe String)
#if defined(__ANDROID__) || defined(mingw32_HOST_OS)
myUserGecos = return Nothing
#else
-myUserGecos = catchMaybeIO $ 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