env-guard: Conditionally running IO actions based on environment variables.

[ environment, library, mit, system ] [ Propose Tags ] [ Report a vulnerability ]

This package provides functions for validating presence / contents of environment variables, and conditionally running IO actions based on the result.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1, 0.1.1, 0.2, 0.2.1
Change log CHANGELOG.md
Dependencies base (>=4.14.0.0 && <4.22) [details]
Tested with ghc ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.6 || ==9.8.4 || ==9.10.2 || ==9.12.2
License MIT
Copyright 2022-2025 Thomas Bidne
Author Thomas Bidne
Maintainer tbidne@protonmail.com
Category System, Environment
Home page https://github.com/tbidne/env-guard/
Bug tracker https://github.com/tbidne/env-guard/issues
Source repo head: git clone https://github.com/tbidne/env-guard
Uploaded by tbidne at 2025-07-16T23:41:18Z
Distributions NixOS:0.2
Downloads 621 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for env-guard-0.2.1

[back to package description]

env-guard

Hackage ci MIT

Introduction

This package is used to guard IO actions behind an environment variable.

Examples

Simple

guardSet :: String -> IO a -> IO (Maybe a)

-- Run function if env var AppEnv is set (to anything).
guardSet "AppEnv" runProd
guardEquals :: String -> String -> IO a -> IO (Maybe a)

-- Run function if env var AppEnv is set to "prod" (case-insensitive).
guardEquals "AppEnv" "prod" runProd
guardPredicate :: String -> (String -> Bool) -> IO a -> IO (Maybe a)

-- Run function if env var AppEnv's value satisfies the predicate.
guardPredicate "AppEnv" (\s -> s == "staging" || s == "dev") runTests

Higher-Level Combinators

-- withGuard uses the 'ExpectEnv' type to determine which of the above three
-- functions to call.
withGuard :: String -> ExpectEnv -> IO a -> IO (Maybe a)

-- equivalent to 'guardSet "BUILD_DOCS" buildDocs'.
withGuard "BUILD_DOCS" ExpectEnvSet buildDocs

-- equivalent to 'guardEquals "BUILD_DOCS" "true" buildDocs'.
withGuard "BUILD_DOCS" (ExpectEnvEquals "true") buildDocs
-- guardOrElse runs one of two IO actions, depending on the success of the env
-- variable lookup.
guardOrElse :: String -> ExpectEnv -> IO a -> IO e -> IO (Either e a)

-- Runs runA if DO_A is set; otherwise runs runB.
guardOrElse "DO_A" ExpectEnvSet runA runB

-- guardOrElse' specialized to the same type.
guardOrElse' :: String -> ExpectEnv -> IO a -> IO a -> IO a

-- Runs runTests if RUN_TESTS is set; otherwise prints a message.
guardOrElse' "RUN_TESTS" ExpectEnvSet runTests (putStrLn "*** Tests Disabled ***")