{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}
module Utility.UserInfo (
myHomeDir,
myUserName,
myUserGecos,
) where
import Utility.Env.Basic
import Utility.Exception
#ifndef mingw32_HOST_OS
import Utility.Data
import Control.Applicative
#endif
import System.Posix.User
#if MIN_VERSION_unix(2,8,0)
import System.Posix.User.ByteString (UserEntry)
#endif
import Prelude
myHomeDir :: IO FilePath
myHomeDir :: IO FilePath
myHomeDir = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. FilePath -> a
giveup forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [FilePath]
env UserEntry -> FilePath
homeDirectory
where
#ifndef mingw32_HOST_OS
env :: [FilePath]
env = [FilePath
"HOME"]
#else
env = ["USERPROFILE", "HOME"]
#endif
myUserName :: IO (Either String String)
myUserName :: IO (Either FilePath FilePath)
myUserName = [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [FilePath]
env UserEntry -> FilePath
userName
where
#ifndef mingw32_HOST_OS
env :: [FilePath]
env = [FilePath
"USER", FilePath
"LOGNAME"]
#else
env = ["USERNAME", "USER", "LOGNAME"]
#endif
myUserGecos :: IO (Maybe String)
#if defined(mingw32_HOST_OS)
myUserGecos = return Nothing
#else
myUserGecos :: IO (Maybe FilePath)
myUserGecos = forall a b. Either a b -> Maybe b
eitherToMaybe forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [] UserEntry -> FilePath
userGecos
#endif
myVal :: [String] -> (UserEntry -> String) -> IO (Either String String)
myVal :: [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [FilePath]
envvars UserEntry -> FilePath
extract = [FilePath] -> IO (Either FilePath FilePath)
go [FilePath]
envvars
where
go :: [FilePath] -> IO (Either FilePath FilePath)
go [] = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall {b}. Either FilePath b
envnotset) (forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. UserEntry -> FilePath
extract) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Either SomeException UserEntry)
get
go (FilePath
v:[FilePath]
vs) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([FilePath] -> IO (Either FilePath FilePath)
go [FilePath]
vs) (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> IO (Maybe FilePath)
getEnv FilePath
v
#ifndef mingw32_HOST_OS
get :: IO (Either SomeException UserEntry)
get = forall (m :: * -> *) a.
MonadCatch m =>
m a -> m (Either SomeException a)
tryNonAsync forall a b. (a -> b) -> a -> b
$ UserID -> IO UserEntry
getUserEntryForID forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO UserID
getEffectiveUserID
#else
get = return envnotset
#endif
envnotset :: Either FilePath b
envnotset = forall a b. a -> Either a b
Left (FilePath
"environment not set: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> FilePath
show [FilePath]
envvars)