2023-02-24 00:43:08 +03:00
|
|
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
module Data.Environment
|
2021-09-24 01:56:37 +03:00
|
|
|
( 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
|
2020-07-14 22:00:58 +03:00
|
|
|
|
2021-07-30 14:33:06 +03:00
|
|
|
-- | Server process environment variables
|
2021-12-09 10:58:41 +03:00
|
|
|
newtype Environment = Environment (M.Map String String) deriving (Eq, Show, Generic, ToJSON, Semigroup, Monoid)
|
2020-07-14 22:00:58 +03:00
|
|
|
|
|
|
|
instance FromJSON Environment
|
|
|
|
|
2023-02-20 16:43:34 +03:00
|
|
|
-- 'getEnvironment' is allowed to use 'System.Environment.getEnvironment'
|
2023-02-24 00:43:08 +03:00
|
|
|
{-# ANN getEnvironment ("HLINT: ignore Use env_from_function_argument" :: String) #-}
|
2020-07-14 22:00:58 +03:00
|
|
|
getEnvironment :: IO Environment
|
|
|
|
getEnvironment = mkEnvironment <$> System.Environment.getEnvironment
|
|
|
|
|
|
|
|
maybeEnvironment :: Maybe Environment -> Environment
|
|
|
|
maybeEnvironment = fromMaybe emptyEnvironment
|
|
|
|
|
|
|
|
mkEnvironment :: [(String, String)] -> Environment
|
|
|
|
mkEnvironment = Environment . M.fromList
|
|
|
|
|
|
|
|
emptyEnvironment :: Environment
|
|
|
|
emptyEnvironment = Environment M.empty
|
|
|
|
|
|
|
|
lookupEnv :: Environment -> String -> Maybe String
|
|
|
|
lookupEnv (Environment es) k = M.lookup k es
|
2020-12-28 15:56:00 +03:00
|
|
|
|
|
|
|
toList :: Environment -> [(String, String)]
|
|
|
|
toList (Environment e) = M.toList e
|
2021-05-21 04:49:50 +03:00
|
|
|
|
|
|
|
redactEnv :: Environment -> Environment
|
2023-02-28 17:31:31 +03:00
|
|
|
redactEnv (Environment e) = Environment $ fmap (const "<ENV REDACTED>") e
|