Safe Haskell | None |
---|---|
Language | Haskell2010 |
A module for reading configuration options from environment variables.
Applications have configuration options. The Twelve-Factor App recommends applications read these from environment variables. This requires us to decode environment variables, which are strings, into the different types the app's configuration options might have. This module helps with that.
Here's what sets this package apart from other environment parsers:
- Very small API, supporting just one way to do environment parsing.
- Comes with parsers for common configuration option types, such as URIs. Not using type classes for these parsers means we don't have to write a bunch of orphan instances.
- Mandatory documentation of each environment variable we want to decode.
- The decoders keep track of all the environment variables they depend on. That way the decoder for an application can tell us all the environment variables an application depends on and what they are used for.
Synopsis
- data Parser a
- text :: Parser Text
- int :: Integral a => Parser a
- float :: Parser Float
- boolean :: Parser Bool
- uri :: Parser URI
- filePath :: Parser FilePath
- networkURI :: Parser URI
- secret :: Parser a -> Parser (Secret a)
- custom :: Parser a -> (a -> Result Text b) -> Parser b
- data Decoder config
- consumes :: Decoder config -> [Variable]
- data Variable = Variable {
- name :: Text
- description :: Text
- defaultValue :: Text
- variable :: Variable -> Parser a -> Decoder a
- either :: Decoder a -> Decoder a -> Decoder a
- decode :: Decoder a -> IO a
- decodeDefaults :: Decoder a -> Result Text a
- decodePairs :: Decoder a -> Dict Text Text -> Result Text a
- decodeVariables :: Decoder a -> IO [DecodedVariable]
- decodeVariablePairs :: Decoder a -> Dict Text Text -> [DecodedVariable]
- data DecodedVariable = DecodedVariable {}
Parsers
A function that can read values of a type from text. For example, a
Parser Int
knows how to read a string like "412" and extract from that the
number 412
.
Parsing functions can fail when they read a text that they do not understand.
For example, the Parser Int
parser will fail if ran against the string
"Not a number in the slightest!".
int :: Integral a => Parser a Source #
Parse an integer from an environment variable.
Works for any integer type (Integer
, Int
, Int
, ...).
networkURI :: Parser URI Source #
There's two URI
types that are in vogue in the Haskell ecosystem. We would
like to standardized on the Text.URI
package, since it's the more modern
Text
based version (no Strings
for us!), but most libraries require the
other type. This function helps convert.
secret :: Parser a -> Parser (Secret a) Source #
Parse a secret value from an environment variable.
Check the documentation for the Log
module of nri-prelude
to learn more
about secrets.
custom :: Parser a -> (a -> Result Text b) -> Parser b Source #
Create a parser for custom types. Build on the back of one of the primitve parsers from this module.
data Environment = Development | Production environment :: Parser Environment environment = custom text <| \str -> case str of "development" -> Ok Development "production" -> Ok Production _ -> Err ("Unknown environment: " ++ str)
Decoders
An environment decoder knows how to read an app's configuration from
environment variables. Check out the variable
function to see how you can
begin building decoders.
consumes :: Decoder config -> [Variable] Source #
The list of Variable
s that this decoder will read when ran.
An environment variable with a description of what it is used for.
Variable | |
|
variable :: Variable -> Parser a -> Decoder a Source #
Produce a configuration from a single environment veriable. Usually you
will combine these with mapN
functions to build larger configurations.
Data Settings = Settings { amountOfHats :: Int , furLined :: Bool } map2 Settings (variable (Variable "HATS" "Amount of hats" "2") int) (variable (Variable "FUR_LINED" "Do hats have fur lining?" "False") boolean)
decode :: Decoder a -> IO a Source #
Attempt to decode a configuration by reading environment variables. This will fail if one or more environment variables fail to parse.
It will not fail if certain environment variables are absent. Defaults will be used for those missing values.
decodeDefaults :: Decoder a -> Result Text a Source #
Build a configuration using only default values of environment variables.
Similar to decode
, except this version doesn't read any environment
variables.
This is sometimes useful for tests, where you might not care about the exact values of settings.
decodePairs :: Decoder a -> Dict Text Text -> Result Text a Source #
Same as decode
, but takes the environment to decode as a dictionary.
Decoders for just the variables
decodeVariables :: Decoder a -> IO [DecodedVariable] Source #
Run a decoder. Instead of returnin the decoded value return metadata about each variable that was decoded.
This can be helpful when generating a --help
command, for listing all the
variables that the application supports and what they are currently set to.
decodeVariablePairs :: Decoder a -> Dict Text Text -> [DecodedVariable] Source #
Same as decodeVariables
, but takes the environment to decode as a
dictionary.
data DecodedVariable Source #
Describe a decoded variable for informational purposes.
Instances
Show DecodedVariable Source # | |
Defined in Environment showsPrec :: Int -> DecodedVariable -> ShowS # show :: DecodedVariable -> String # showList :: [DecodedVariable] -> ShowS # |