2022-02-09 18:26:14 +03:00
|
|
|
{-# OPTIONS -Wno-redundant-constraints #-}
|
|
|
|
|
2022-02-23 18:26:20 +03:00
|
|
|
-- | Read environment variables
|
2022-02-09 18:26:14 +03:00
|
|
|
module Harness.Env (getEnvRead, getEnvJSON, getEnvString) where
|
|
|
|
|
|
|
|
import Data.Aeson qualified as Aeson
|
|
|
|
import Data.String
|
2022-02-23 18:26:20 +03:00
|
|
|
import Data.Typeable (Proxy (Proxy), Typeable, typeRep)
|
2022-02-09 18:26:14 +03:00
|
|
|
import GHC.Stack
|
|
|
|
import Hasura.Prelude
|
2022-02-23 18:26:20 +03:00
|
|
|
import System.Environment (lookupEnv)
|
2022-02-09 18:26:14 +03:00
|
|
|
|
2022-02-23 18:26:20 +03:00
|
|
|
-- * API
|
|
|
|
|
|
|
|
getEnvRead :: (Read a, Typeable a, HasCallStack) => String -> IO a
|
|
|
|
getEnvRead var =
|
|
|
|
withFrozenCallStack $ do
|
|
|
|
readVarValue var =<< getEnv var
|
2022-02-09 18:26:14 +03:00
|
|
|
|
|
|
|
getEnvString :: (IsString a, HasCallStack) => String -> IO a
|
2022-02-23 18:26:20 +03:00
|
|
|
getEnvString var =
|
|
|
|
withFrozenCallStack $
|
|
|
|
fromString <$> getEnv var
|
|
|
|
|
|
|
|
getEnvJSON :: forall a. (Typeable a, Aeson.FromJSON a, HasCallStack) => String -> IO a
|
|
|
|
getEnvJSON var =
|
|
|
|
withFrozenCallStack $ do
|
|
|
|
accountString <- getEnv var
|
|
|
|
onLeft
|
|
|
|
(Aeson.eitherDecode' (fromString accountString))
|
|
|
|
( \err ->
|
|
|
|
let expectedType = show (typeRep (Proxy :: Proxy a))
|
|
|
|
in error (unlines ["Failure parsing '" <> var <> "' to type '" <> expectedType <> "':", show err])
|
|
|
|
)
|
|
|
|
|
|
|
|
-- * Helpers
|
|
|
|
|
|
|
|
getEnv :: HasCallStack => String -> IO String
|
|
|
|
getEnv var = do
|
|
|
|
value <- lookupEnv var
|
|
|
|
onNothing value (error $ "getEnv: " <> var <> " does not exist (no environment variable)")
|
|
|
|
|
|
|
|
-- | Read a variable to a specific type.
|
|
|
|
readVarValue :: forall a. (Read a, Typeable a, HasCallStack) => String -> String -> IO a
|
|
|
|
readVarValue var value =
|
|
|
|
onNothing
|
|
|
|
(readMaybe value)
|
|
|
|
let expectedType = show (typeRep (Proxy :: Proxy a))
|
|
|
|
in error
|
|
|
|
( unwords
|
|
|
|
[ "Failure parsing '" <> var <> "'",
|
|
|
|
"to type '" <> expectedType <> "';",
|
|
|
|
"containing value '" <> show value <> "'."
|
|
|
|
]
|
|
|
|
)
|