Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meowgorithm/mr-env
A simple way to read environment variables in Haskell
https://github.com/meowgorithm/mr-env
environment-variables haskell
Last synced: 2 months ago
JSON representation
A simple way to read environment variables in Haskell
- Host: GitHub
- URL: https://github.com/meowgorithm/mr-env
- Owner: meowgorithm
- License: mit
- Created: 2020-03-15T01:29:31.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-06T01:21:13.000Z (about 2 years ago)
- Last Synced: 2024-09-29T17:41:41.077Z (3 months ago)
- Topics: environment-variables, haskell
- Language: Haskell
- Homepage: https://hackage.haskell.org/package/mr-env-0.1.0.0
- Size: 64.5 KB
- Stars: 2
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE
Awesome Lists containing this project
README
# Mr. Env
![build][action-badge] [![Hackage][hackage-shield]][hackage]
[action-badge]: https://github.com/meowgorithm/mr-env/workflows/build/badge.svg
[hackage]: http://hackage.haskell.org/package/mr-env
[hackage-shield]: https://img.shields.io/hackage/v/mr-env.svg?style=flat&color=bluevioletA simple way to read environment variables in Haskell.
```haskell
-- Read environment variables, with defaults
host <- envAsString "HOST" "localhost"
port <- envAsInt "PORT" 8000
```## Simple Example
Read environment variables with `do` notation:
```haskell
import System.Environment.MrEnv ( envAsBool, envAsInt, envAsInteger, envAsString )main :: IO ()
main = do-- Get a string, with a fallback value if nothing is set.
host <- envAsString "HOST" "localhost"-- Get an int. If you need an integer instead you could also use envAsInteger.
port <- envAsInt "PORT" 8000-- Get a boolean. Here we're expecting the environment variable to read
-- something along the lines of "true", "TRUE", "True", "truE" and so on.
debug <- envAsBool "DEBUG" FalseputStrLn $
"Let's connect to "
++ host
++ " on port "
++ show port
++ ". Debug mode is "
++ if debug then "on" else "off"
++ "."
```## Fancy Example
Read environment variables into a record:
```haskell
import System.Environment.MrEnv ( envAsBool, envAsInt, envAsInteger, envAsString )data Config =
Config { host :: String
, port :: Int
, debug :: Bool
}getConfig :: IO Config
getConfig = Config
<$> envAsString "HOST" "localhost"
<*> envAsInt "PORT" 8000
<*> envAsBool "DEBUG" Falsemain :: IO ()
main =
getConfig >>= \conf ->
putStrLn $
"Let's connect to "
++ host conf
++ " on port "
++ show $ port conf
++ ". Debug mode is "
++ if debug conf then "on" else "off"
++ "."
```We suggest pronouncing `<*>` _[brackety-splat][1]_ (as
opposed to _ap_). In that vein, `<$>` is _brackety-cash._[1]: https://www.reddit.com/r/haskell/comments/241jcm/how_do_you_say/
## Authors
* [Christian Rocha](https://github.com/meowgorithm)
* [Sam Raker](https://github.com/swizzard)## License
MIT