module RichEnv
(
RichEnv (..),
Environment,
toEnvList,
toEnvMap,
setRichEnv,
setRichEnvFromCurrent,
toEnvListFromCurrent,
toEnvMapFromCurrent,
clearEnvironment,
)
where
import Data.HashMap.Strict qualified as HM
import Data.Text (Text)
import RichEnv.Setters (richEnvToValues, valuesToEnv, valuesToEnvList)
import RichEnv.Types (Environment, RichEnv (..), fromEnvironment, toEnvironment)
import RichEnv.Types.Values (Values (unValues))
import System.Environment (getEnvironment, unsetEnv)
toEnvList :: RichEnv -> Environment -> Environment
toEnvList :: RichEnv -> Environment -> Environment
toEnvList RichEnv
re = Values -> Environment
valuesToEnvList (Values -> Environment)
-> (Environment -> Values) -> Environment -> Environment
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RichEnv -> Environment -> Values
toEnvValues RichEnv
re
toEnvMap :: RichEnv -> Environment -> HM.HashMap Text Text
toEnvMap :: RichEnv -> Environment -> HashMap Text Text
toEnvMap RichEnv
re = Values -> HashMap Text Text
unValues (Values -> HashMap Text Text)
-> (Environment -> Values) -> Environment -> HashMap Text Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RichEnv -> Environment -> Values
toEnvValues RichEnv
re
toEnvValues :: RichEnv -> Environment -> Values
toEnvValues :: RichEnv -> Environment -> Values
toEnvValues = RichEnv -> Environment -> Values
richEnvToValues
setRichEnv :: RichEnv -> Environment -> IO ()
setRichEnv :: RichEnv -> Environment -> IO ()
setRichEnv RichEnv
re Environment
env = do
[(String, String)] -> IO ()
clearEnvironment ([(String, String)] -> IO ()) -> [(String, String)] -> IO ()
forall a b. (a -> b) -> a -> b
$ Environment -> [(String, String)]
fromEnvironment Environment
env
Values -> IO ()
valuesToEnv (RichEnv -> Environment -> Values
richEnvToValues RichEnv
re Environment
env)
setRichEnvFromCurrent :: RichEnv -> IO ()
setRichEnvFromCurrent :: RichEnv -> IO ()
setRichEnvFromCurrent RichEnv
re = IO [(String, String)]
getEnvironment IO [(String, String)] -> ([(String, String)] -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= RichEnv -> Environment -> IO ()
setRichEnv RichEnv
re (Environment -> IO ())
-> ([(String, String)] -> Environment)
-> [(String, String)]
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(String, String)] -> Environment
toEnvironment
toEnvListFromCurrent :: RichEnv -> IO Environment
toEnvListFromCurrent :: RichEnv -> IO Environment
toEnvListFromCurrent RichEnv
re = RichEnv -> Environment -> Environment
toEnvList RichEnv
re (Environment -> Environment)
-> ([(String, String)] -> Environment)
-> [(String, String)]
-> Environment
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(String, String)] -> Environment
toEnvironment ([(String, String)] -> Environment)
-> IO [(String, String)] -> IO Environment
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [(String, String)]
getEnvironment
toEnvMapFromCurrent :: RichEnv -> IO (HM.HashMap Text Text)
toEnvMapFromCurrent :: RichEnv -> IO (HashMap Text Text)
toEnvMapFromCurrent RichEnv
re = RichEnv -> Environment -> HashMap Text Text
toEnvMap RichEnv
re (Environment -> HashMap Text Text)
-> ([(String, String)] -> Environment)
-> [(String, String)]
-> HashMap Text Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(String, String)] -> Environment
toEnvironment ([(String, String)] -> HashMap Text Text)
-> IO [(String, String)] -> IO (HashMap Text Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [(String, String)]
getEnvironment
_toEnvValuesFromCurrent :: RichEnv -> IO Values
_toEnvValuesFromCurrent :: RichEnv -> IO Values
_toEnvValuesFromCurrent RichEnv
re = RichEnv -> Environment -> Values
toEnvValues RichEnv
re (Environment -> Values)
-> ([(String, String)] -> Environment)
-> [(String, String)]
-> Values
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(String, String)] -> Environment
toEnvironment ([(String, String)] -> Values)
-> IO [(String, String)] -> IO Values
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [(String, String)]
getEnvironment
clearEnvironment ::
[(String, String)] ->
IO ()
clearEnvironment :: [(String, String)] -> IO ()
clearEnvironment = ((String, String) -> IO ()) -> [(String, String)] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String -> IO ()
unsetEnv (String -> IO ())
-> ((String, String) -> String) -> (String, String) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, String) -> String
forall a b. (a, b) -> a
fst)