Copyright | © Jonathan Lorimer 2023 |
---|---|
License | MIT |
Maintainer | jonathanlorimer@pm.me |
Stability | stable |
Safe Haskell | None |
Language | Haskell2010 |
This module contains all the functions for interacting with environment variables as a configuration source.
Since: 0.0.2.0
Synopsis
- envSourceSep :: MonadIO m => Text -> KeyTree Text Text -> m (KeyTree Text Text)
- envSource :: (MonadFail m, MonadIO m) => KeyTree Text Text -> m (KeyTree Text Text)
- getEnvConfigSep :: forall a m. (MonadFail m, MonadIO m, ConfigSource a, ConfigParser a) => Text -> m (Either ConfigParseError a)
- getEnvConfig :: forall a m. (MonadFail m, MonadIO m, ConfigSource a, ConfigParser a) => m (Either ConfigParseError a)
- printDotEnv' :: FilePath -> Text -> KeyTree Text Text -> IO ()
- printDotEnv :: forall {k} (a :: k). ConfigSource a => FilePath -> IO ()
Retrieval Functions
:: MonadIO m | |
=> Text | Separator |
-> KeyTree Text Text | Configuration source |
-> m (KeyTree Text Text) | Configuration tree with values filled in |
This function folds the tree from root to leaf accumulating the keys along the way. At the leaf we lookup the aggregated key in the environment, if there is a default then we use that for missing keys.
If you are looking at the source code this is what the functions in the
where
clause do:
valF
: Gets called onPure
values in the original tree passed in, these indicate defaulted values, so we use the default if looking the value up in the environment failed.accF
: This operates on the accumulated key, and is responsible for looking up the value in the environment when we hit theFree M.empty
case.stepF
: This is the step function for the fold and accumulates the keys as we traverse down the tree.mkKey
: This function is the same asgetEnvKey
except that it usesflip mappend
. The reason for this is that we insert keys into the accumulator as we traverse down so they end up in reversed order, then wefoldr
over them so we just need to make sure that we are placing the elements at the end of the list on the left hand side of the aggregate key.
Since: 0.0.1.0
envSource :: (MonadFail m, MonadIO m) => KeyTree Text Text -> m (KeyTree Text Text) Source #
This function is the same as envSourceSep
but with the separator specialized to "_".
>>>
import System.Environment
>>>
import Data.Map qualified as M
>>>
setEnv "A_B" "Functor"
>>>
setEnv "A_C" "Applicative"
>>>
setEnv "A_D" "Monad"
>>>
envSource $ Free $ M.fromList [("A", Free $ M.fromList [("B", Free M.empty), ("C", Free M.empty), ("D", Free M.empty)])]
Free (fromList [("A",Free (fromList [("B",Pure "Functor"),("C",Pure "Applicative"),("D",Pure "Monad")]))])
Since: 0.0.1.0
:: forall a m. (MonadFail m, MonadIO m, ConfigSource a, ConfigParser a) | |
=> Text | Separator |
-> m (Either ConfigParseError a) |
Requires a type annotation for your configuration type (with a
ConfigSource
and ConfigParser
instance), and a separator, and will go
out and fetch the values from environment variables then return your type
parsed from those values.
getEnvConfigSep @AppConfig "_"
Since: 0.0.1.0
getEnvConfig :: forall a m. (MonadFail m, MonadIO m, ConfigSource a, ConfigParser a) => m (Either ConfigParseError a) Source #
The same as getEnvConfigSep
but with the separator hard coded to "_"
Since: 0.0.1.0
Printing Functions
This function can be used to print a dotenv style file with all the aggregate keys, none of the values will be filled in.
Useful for testing what your expected environment variables should look like, and generating an env var file template.
Since: 0.0.1.0
printDotEnv :: forall {k} (a :: k). ConfigSource a => FilePath -> IO () Source #
The same as printDotEnv'
but with the separator hard coded to "_" and
it uses a type application to generate the configuration source tree
representation.
printDotEnv @AppConfig ".env"
Since: 0.0.1.0