EVP: Environment Variable Parser

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

See README.md


[Skip to Readme]

Properties

Versions 0, 0.1, 0.1
Change log CHANGELOG.md
Dependencies base (>=4.16), containers, data-default-class, text, yaml [details]
License BSD-3-Clause
Copyright Copyright (c) 2023 Fumiaki Kinoshita
Author Fumiaki Kinoshita
Maintainer fumiexcel@gmail.com
Category System
Home page https://github.com/fumieval/EVP
Source repo head: git clone https://github.com/fumieval/EVP.git
Uploaded by FumiakiKinoshita at 2023-11-30T05:15:32Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for EVP-0.1

[back to package description]

EVP: Environment Variable Parser

EVP is a simple environment parser which focues on these three aspects:

Example

The following code is a complete example demonstrating how to use EVP:

{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE OverloadedStrings #-}

import EVP qualified

main :: IO ()
main = EVP.scan parser

-- ApplicativeDo is important here because Scan is not a monad.
parser :: EVP.Scan ()
parser = do
    -- @secret@ masks the parsed value
    _token <- EVP.secret $ EVP.string "API_TOKEN"
    -- parse the environment variable as a YAML value
    _port <- EVP.yaml "HTTP_PORT"
    -- obtain the environment variable as is
    _foo <- EVP.string "FOO"
    -- you can also provide a default value
    _debug <- EVP.yaml $ "DEBUG_MODE" `EVP.defaultsTo` False
    pure ()

Running this code produces the following output.

[EVP Info] API_TOKEN: <REDACTED>
[EVP Info] HTTP_PORT: 8080
[EVP Info] FOO: foo
[EVP Info] DEBUG_MODE: False (default)

Revealing unused variables

EVP has a mechanism to detect unused variables.

If your application's environment variables has a common prefix MYAPP_, you can set assumePrefix as a unusedLogger.

EVP.scanWith EVP.def
    { EVP.unusedLogger = EVP.assumePrefix "MYAPP_" }
    parser

If an environment variable prefixed by MYAPP_ is set but not referred to, EVP prints the following warning. This is useful for detecting typos too.

[EVP Warn] MYAPP_OBSOLETE_FLAG is set but not used

Alternatively, you can also name unwanted variables individually:

EVP.scanWith EVP.def
    { EVP.unusedLogger = EVP.obsolete ["OBSOLETE_VAR"] }

These can be combined using the Semigroup instance.

EVP.scanWith EVP.def
    { EVP.unusedLogger = EVP.assumePrefix "MYAPP_" <> EVP.obsolete ["OBSOLETE_VAR"] }
    parser

Parser group

You can provide an additional context to a parser by applying group "group name". Group names appear in the log messages:

[EVP Info] DEBUG_MODE: False (default)
[EVP Info/MySQL] MYSQL_HOST: localhost
[EVP Info/MySQL] MYSQL_PASSWORD: <REDACTED>
[EVP Error/MySQL] Failed to parse MYSQL_PORT=blah: Aeson exception:
[EVP Error/MySQL] Error in $: parsing Int failed, expected Number, but encountered String

Design context