module Configuration.Dotenv (load, loadFile) where
import System.Environment.Compat (lookupEnv, setEnv)
import Configuration.Dotenv.Parse (configParser)
import Text.Parsec (parse)
load ::
Bool
-> [(String, String)]
-> IO ()
load override = mapM_ (applySetting override)
loadFile ::
Bool
-> FilePath
-> IO ()
loadFile override f = do
contents <- readFile f
case parse configParser f contents of
Left e -> error $ "Failed to read file" ++ show e
Right options -> load override options
applySetting :: Bool -> (String, String) -> IO ()
applySetting override (key, value) =
if override then
setEnv key value
else do
res <- lookupEnv key
case res of
Nothing -> setEnv key value
Just _ -> return ()