-- |Config utility module, uses System.Envy to read ENV values and builds a data
module Chakra.Config where

import qualified System.Envy as Envy
import System.IO (hPutStrLn, stderr)

-- | Read from ENVIRONMENT Variables and return your target `t` data
-- `t` should have a instance of FromEnv
-- Example: 
-- @ 
--    instance FromEnv MyAppSettings
-- @
withAppSettingsFromEnv :: Envy.FromEnv t => (t -> IO ()) -> IO ()
withAppSettingsFromEnv :: (t -> IO ()) -> IO ()
withAppSettingsFromEnv t -> IO ()
f = IO (Either String t)
forall a. FromEnv a => IO (Either String a)
Envy.decodeEnv IO (Either String t) -> (Either String t -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either String t -> IO ()
callF
  where
    callF :: Either String t -> IO ()
callF Either String t
x =
      case Either String t
x of
        Left String
e -> Handle -> String -> IO ()
hPutStrLn Handle
stderr (String
"Error reading env: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
e)
        Right t
c -> t -> IO ()
f t
c