module Data.Environment
  ( Environment (),
    getEnvironment,
    mkEnvironment,
    emptyEnvironment,
    maybeEnvironment,
    lookupEnv,
    redactEnv,
    Data.Environment.toList,
  )
where

import Data.Aeson
import Data.Map qualified as M
import Hasura.Prelude
import System.Environment qualified

-- | Server process environment variables
newtype Environment = Environment (M.Map String String) deriving (Environment -> Environment -> Bool
(Environment -> Environment -> Bool)
-> (Environment -> Environment -> Bool) -> Eq Environment
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Environment -> Environment -> Bool
$c/= :: Environment -> Environment -> Bool
== :: Environment -> Environment -> Bool
$c== :: Environment -> Environment -> Bool
Eq, Int -> Environment -> ShowS
[Environment] -> ShowS
Environment -> String
(Int -> Environment -> ShowS)
-> (Environment -> String)
-> ([Environment] -> ShowS)
-> Show Environment
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Environment] -> ShowS
$cshowList :: [Environment] -> ShowS
show :: Environment -> String
$cshow :: Environment -> String
showsPrec :: Int -> Environment -> ShowS
$cshowsPrec :: Int -> Environment -> ShowS
Show, (forall x. Environment -> Rep Environment x)
-> (forall x. Rep Environment x -> Environment)
-> Generic Environment
forall x. Rep Environment x -> Environment
forall x. Environment -> Rep Environment x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Environment x -> Environment
$cfrom :: forall x. Environment -> Rep Environment x
Generic, [Environment] -> Value
[Environment] -> Encoding
Environment -> Value
Environment -> Encoding
(Environment -> Value)
-> (Environment -> Encoding)
-> ([Environment] -> Value)
-> ([Environment] -> Encoding)
-> ToJSON Environment
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Environment] -> Encoding
$ctoEncodingList :: [Environment] -> Encoding
toJSONList :: [Environment] -> Value
$ctoJSONList :: [Environment] -> Value
toEncoding :: Environment -> Encoding
$ctoEncoding :: Environment -> Encoding
toJSON :: Environment -> Value
$ctoJSON :: Environment -> Value
ToJSON, b -> Environment -> Environment
NonEmpty Environment -> Environment
Environment -> Environment -> Environment
(Environment -> Environment -> Environment)
-> (NonEmpty Environment -> Environment)
-> (forall b. Integral b => b -> Environment -> Environment)
-> Semigroup Environment
forall b. Integral b => b -> Environment -> Environment
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: b -> Environment -> Environment
$cstimes :: forall b. Integral b => b -> Environment -> Environment
sconcat :: NonEmpty Environment -> Environment
$csconcat :: NonEmpty Environment -> Environment
<> :: Environment -> Environment -> Environment
$c<> :: Environment -> Environment -> Environment
Semigroup, Semigroup Environment
Environment
Semigroup Environment
-> Environment
-> (Environment -> Environment -> Environment)
-> ([Environment] -> Environment)
-> Monoid Environment
[Environment] -> Environment
Environment -> Environment -> Environment
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [Environment] -> Environment
$cmconcat :: [Environment] -> Environment
mappend :: Environment -> Environment -> Environment
$cmappend :: Environment -> Environment -> Environment
mempty :: Environment
$cmempty :: Environment
$cp1Monoid :: Semigroup Environment
Monoid)

instance FromJSON Environment

getEnvironment :: IO Environment
getEnvironment :: IO Environment
getEnvironment = [(String, String)] -> Environment
mkEnvironment ([(String, String)] -> Environment)
-> IO [(String, String)] -> IO Environment
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [(String, String)]
System.Environment.getEnvironment

maybeEnvironment :: Maybe Environment -> Environment
maybeEnvironment :: Maybe Environment -> Environment
maybeEnvironment = Environment -> Maybe Environment -> Environment
forall a. a -> Maybe a -> a
fromMaybe Environment
emptyEnvironment

mkEnvironment :: [(String, String)] -> Environment
mkEnvironment :: [(String, String)] -> Environment
mkEnvironment = Map String String -> Environment
Environment (Map String String -> Environment)
-> ([(String, String)] -> Map String String)
-> [(String, String)]
-> Environment
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(String, String)] -> Map String String
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList

emptyEnvironment :: Environment
emptyEnvironment :: Environment
emptyEnvironment = Map String String -> Environment
Environment Map String String
forall k a. Map k a
M.empty

lookupEnv :: Environment -> String -> Maybe String
lookupEnv :: Environment -> String -> Maybe String
lookupEnv (Environment Map String String
es) String
k = String -> Map String String -> Maybe String
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
k Map String String
es

toList :: Environment -> [(String, String)]
toList :: Environment -> [(String, String)]
toList (Environment Map String String
e) = Map String String -> [(String, String)]
forall k a. Map k a -> [(k, a)]
M.toList Map String String
e

redactEnv :: Environment -> Environment
redactEnv :: Environment -> Environment
redactEnv (Environment Map String String
e) = Map String String -> Environment
Environment (Map String String -> Environment)
-> Map String String -> Environment
forall a b. (a -> b) -> a -> b
$ ShowS -> Map String String -> Map String String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> ShowS
forall a b. a -> b -> a
const String
"******") Map String String
e