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.
This commit is contained in:
Joey Hess 2016-06-08 15:04:15 -04:00
parent a3ff99ea3b
commit 8e4cbefbc6
Failed to extract signature
6 changed files with 20 additions and 22 deletions

View file

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