{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Data.Yaml.Config
(
loadYamlSettings
, loadYamlSettingsArgs
, EnvUsage
, ignoreEnv
, useEnv
, requireEnv
, useCustomEnv
, requireCustomEnv
, applyCurrentEnv
, getCurrentEnv
, applyEnvValue
) where
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative ((<$>))
import Data.Monoid
#endif
import Data.Semigroup
import Data.List.NonEmpty (nonEmpty)
import Data.Aeson
import qualified Data.HashMap.Strict as H
import Data.Text (Text, pack)
import System.Environment (getArgs, getEnvironment)
import Control.Arrow ((***))
import Control.Monad (forM)
import Control.Exception (throwIO)
import Data.Text.Encoding (encodeUtf8)
import qualified Data.Yaml as Y
import qualified Data.Yaml.Include as YI
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
newtype MergedValue = MergedValue { getMergedValue :: Value }
instance Semigroup MergedValue where
MergedValue x <> MergedValue y = MergedValue $ mergeValues x y
mergeValues :: Value -> Value -> Value
mergeValues (Object x) (Object y) = Object $ H.unionWith mergeValues x y
mergeValues x _ = x
applyEnvValue :: Bool
-> H.HashMap Text Text -> Value -> Value
applyEnvValue requireEnv' env =
goV
where
goV (Object o) = Object $ goV <$> o
goV (Array a) = Array (goV <$> a)
goV (String t1) = fromMaybe (String t1) $ do
t2 <- T.stripPrefix "_env:" t1
let (name, t3) = T.break (== ':') t2
mdef = fmap parseValue $ T.stripPrefix ":" t3
Just $ case H.lookup name env of
Just val ->
case mdef of
Just (String _) -> String val
_ -> parseValue val
Nothing ->
case mdef of
Just val | not requireEnv' -> val
_ -> Null
goV v = v
parseValue val = either
(const (String val))
id
(Y.decodeThrow $ encodeUtf8 val)
getCurrentEnv :: IO (H.HashMap Text Text)
getCurrentEnv = fmap (H.fromList . map (pack *** pack)) getEnvironment
applyCurrentEnv :: Bool
-> Value -> IO Value
applyCurrentEnv requireEnv' orig = flip (applyEnvValue requireEnv') orig <$> getCurrentEnv
data EnvUsage = IgnoreEnv
| UseEnv
| RequireEnv
| UseCustomEnv (H.HashMap Text Text)
| RequireCustomEnv (H.HashMap Text Text)
ignoreEnv :: EnvUsage
ignoreEnv = IgnoreEnv
useEnv :: EnvUsage
useEnv = UseEnv
requireEnv :: EnvUsage
requireEnv = RequireEnv
useCustomEnv :: H.HashMap Text Text -> EnvUsage
useCustomEnv = UseCustomEnv
requireCustomEnv :: H.HashMap Text Text -> EnvUsage
requireCustomEnv = RequireCustomEnv
loadYamlSettings
:: FromJSON settings
=> [FilePath]
-> [Value]
-> EnvUsage
-> IO settings
loadYamlSettings runTimeFiles compileValues envUsage = do
runValues <- forM runTimeFiles $ \fp -> do
eres <- YI.decodeFileEither fp
case eres of
Left e -> throwIO (Y.LoadSettingsException fp e)
Right value -> return value
value' <-
case nonEmpty $ map MergedValue $ runValues ++ compileValues of
Nothing -> error "loadYamlSettings: No configuration provided"
Just ne -> return $ getMergedValue $ sconcat ne
value <-
case envUsage of
IgnoreEnv -> return $ applyEnvValue False mempty value'
UseEnv -> applyCurrentEnv False value'
RequireEnv -> applyCurrentEnv True value'
UseCustomEnv env -> return $ applyEnvValue False env value'
RequireCustomEnv env -> return $ applyEnvValue True env value'
case Y.parseEither parseJSON value of
Left s -> error $ "Could not convert to expected type: " ++ s
Right settings -> return settings
loadYamlSettingsArgs
:: FromJSON settings
=> [Value]
-> EnvUsage
-> IO settings
loadYamlSettingsArgs values env = do
args <- getArgs
loadYamlSettings args values env