env-guard-0.1: Conditionally running IO actions based on environment variables.
Safe HaskellSafe-Inferred
LanguageHaskell2010

System.Environment.Guard

Description

Functions for conditionally running IO actions based on an environment variable.

Since: 0.1

Synopsis

Checking environment variable is set

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

guardSet var io runs io iff

  1. The environment variable var is set.
guardSet var === guardPredicate var (const True)

Examples

Expand
>>> guardSet "NOT_SET" (putStrLn "ran io" $> True)
Nothing
>>> setEnv "SET" "foo"
>>> guardSet "SET" (putStrLn "ran io" $> True)
ran io
Just True

Since: 0.1

guardSet_ :: String -> IO a -> IO () Source #

Variant of guardSet that ignores the return value.

Since: 0.1

Checking environment variable match

guardExpected :: String -> String -> IO a -> IO (Maybe a) Source #

guardExpected var expected io runs io iff

  1. The environment variable var is set.
  2. var's value equals expected. This is case-insensitive.
guardExpected var expected === guardPredicate var (\a b -> fmap toLower a == fmap toLower b)

Examples

Expand
>>> guardExpected "NOT_SET" "val" (putStrLn "ran io" $> True)
Nothing
>>> setEnv "WRONG_VAL" "good_val"
>>> guardExpected "WRONG_VAL" "bad_val" (putStrLn "ran io" $> True)
Nothing
>>> setEnv "WILL_RUN" "val"
>>> guardExpected "WILL_RUN" "VAL" (putStrLn "ran io" $> True)
ran io
Just True

Since: 0.1

guardExpected_ :: String -> String -> IO a -> IO () Source #

Variant of guardExpected_ that ignores the return value.

Since: 0.1

Checking environment variable predicate

guardPredicate :: String -> (String -> Bool) -> IO a -> IO (Maybe a) Source #

This is the most general way to check an environment variable. guardPredicate var p io runs io iff

  1. The environment variable var is set.
  2. var's value satisfies predicate p.

Examples

Expand
>>> guardPredicate "NOT_SET" (const True) (putStrLn "ran io" $> True)
Nothing
>>> setEnv "CASE_WRONG" "VAL"
>>> guardPredicate "CASE_WRONG" (== "val") (putStrLn "ran io" $> True)
Nothing
>>> setEnv "WILL_RUN" "VAL"
>>> guardPredicate "WILL_RUN" (== "VAL") (putStrLn "ran io" $> True)
ran io
Just True

Since: 0.1

guardPredicate_ :: String -> (String -> Bool) -> IO a -> IO () Source #

Variant of guardPredicate that ignores the return value.

Since: 0.1