| Copyright | (c) David Johnson 2015 |
|---|---|
| Maintainer | djohnson.m@gmail.com |
| Stability | experimental |
| Portability | POSIX |
| Safe Haskell | None |
| Language | Haskell2010 |
System.Envy
Description
{-# LANGUAGE DeriveGeneric #-}
module Main ( main ) where
import System.Envy
import GHC.Generics
data PGConfig = PGConfig {
pgHost :: String -- "PG_HOST"
, pgPort :: Int -- "PG_PORT"
} deriving (Generic, Show)
-- Default instance used if environment variable doesn't exist
instance DefConfig PGConfig where
defConfig = PGConfig "localhost" 5432
instance FromEnv PGConfig
-- Generically produces the following body (no implementation needed if using Generics):
-- fromEnv = PGConfig <$> envMaybe "PG_HOST" .!= "localhost"
-- <*> envMaybe "PG_PORT" .!= 5432
main :: IO ()
main =
print =<< do decodeEnv :: IO (Either String PGConfig)
-- PGConfig { pgHost = "custom-pg-url", pgPort = 5432 }- class FromEnv a where
- class ToEnv a where
- class Typeable a => Var a where
- data EnvList a
- newtype Parser a = Parser {}
- decodeEnv :: FromEnv a => IO (Either String a)
- decode :: FromEnv a => IO (Maybe a)
- showEnv :: IO ()
- setEnvironment :: EnvList a -> IO (Either String ())
- setEnvironment' :: ToEnv a => a -> IO (Either String ())
- unsetEnvironment :: EnvList a -> IO (Either String ())
- unsetEnvironment' :: ToEnv a => a -> IO (Either String ())
- makeEnv :: [EnvVar] -> EnvList a
- env :: Var a => String -> Parser a
- envMaybe :: Var a => String -> Parser (Maybe a)
- (.=) :: Var a => String -> a -> EnvVar
- (.!=) :: Parser (Maybe a) -> a -> Parser a
- class DefConfig a where
- data Option = Option {}
- runEnv :: Parser a -> IO (Either String a)
- gFromEnvCustom :: forall a. (DefConfig a, Generic a, GFromEnv (Rep a)) => Option -> Parser a
Classes
FromEnv Typeclass w/ Generic default implementation
Type class for objects which can be converted to a set of environment variable settings.
Minimal complete definition
class Typeable a => Var a where #
Class for converting to / from an environment variable
List of environment variables. Captures a "phantom type" which allows the type checker to detect the proper implementation of toEnv to use.
Parser Monad for environment variable retrieval
Functions
setEnvironment' :: ToEnv a => a -> IO (Either String ()) #
Set environment directly using a value of class ToEnv
unsetEnvironment :: EnvList a -> IO (Either String ()) #
Unset Environment from a ToEnv constrained type
unsetEnvironment' :: ToEnv a => a -> IO (Either String ()) #
Unset Environment using a value of class ToEnv
Environment variable getter. Fails if the variable is not set or fails to parse.
Environment variable getter returning Maybe
Arguments
| :: Var a | |
| => String | The variable name to set. |
| -> a | Object to set in the environment. |
| -> EnvVar | Mapping of Variable to Value. |
Infix environment variable setter
Smart constructor for producing types of EnvVar
Arguments
| :: Parser (Maybe a) | Parser that might fail. |
| -> a | Value to return if the parser fails. |
| -> Parser a | Parser that returns the default on failure. |
For use with envMaybe for providing default arguments.
Generics
Type class for objects which have a default configuration.
Minimal complete definition
For customizing environment variable generation
Constructors
| Option | |
Fields
| |