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

System.Environment.Guard.Lifted

Description

Synopsis

High level combinators

data ExpectEnv Source #

The expectation for an environment variable lookup.

Since: 0.1.1

Constructors

ExpectEnvSet

Expect that the environment variable is set (i.e. contents can be anything).

Since: 0.1.1

ExpectEnvEquals String

Expect that the environment variable is set and the contents equals the string. This is case-insensitive.

Since: 0.1.1

ExpectEnvPredicate (String -> Bool)

Expect that the environment variable is set and its contents satisfies the predicate.

Since: 0.1.1

Instances

Instances details
Show ExpectEnv Source #

Since: 0.1.1

Instance details

Defined in System.Environment.Guard.Lifted

withGuard :: MonadIO m => String -> ExpectEnv -> m a -> m (Maybe a) Source #

Guards an action behind an environment variable according to the given expectation.

Examples

Expand
>>> setEnv "FOO" "bar"
>>> withGuard "FOO" (ExpectEnvEquals "baz") (putStrLn "succeeded")
Nothing
>>> withGuard "FOO" ExpectEnvSet (putStrLn "succeeded")
succeeded
Just ()

Since: 0.1.1

withGuard_ :: MonadIO m => String -> ExpectEnv -> m a -> m () Source #

Variant of withGuard that ignores the return value.

Since: 0.1.1

guardOrElse Source #

Arguments

:: MonadIO m 
=> String

The environment variable.

-> ExpectEnv

The expectation.

-> m a

The action to run if the expectation succeeds.

-> m e

The action to run if the expectation fails.

-> m (Either e a)

The result.

guardOrElse var expect m1 m2 is equivalent to withGuard var expect m1 except that it runs m2 if m1 is not run.

Examples

Expand
>>> setEnv "FOO" "bar"
>>> guardOrElse "FOO" ExpectEnvSet (pure True) (pure "not found")
Right True
>>> guardOrElse "BAR" ExpectEnvSet (pure True) (pure "not found")
Left "not found"

Since: 0.1.1

guardOrElse' Source #

Arguments

:: MonadIO m 
=> String

The environment variable.

-> ExpectEnv

The expectation.

-> m a

The action to run if the expectation succeeds.

-> m a

The action to run if the expectation fails.

-> m a

The result.

guardOrElse specialized to the same type so that we always return an a. This can also be used to ignore the return value i.e.

guardOrElse' var expect (void m1) m2

Examples

Expand
>>> setEnv "FOO" "bar"
>>> guardOrElse' "FOO" ExpectEnvSet (pure True) (pure False)
True
>>> guardOrElse' "BAR" ExpectEnvSet (pure True) (pure False)
False
>>> guardOrElse' "BAR" ExpectEnvSet (void $ pure True) (putStrLn "not found")
not found

Since: 0.1.1

Low level functions

Checking environment variable is set

guardSet :: MonadIO m => String -> m a -> m (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_ :: MonadIO m => String -> m a -> m () Source #

Variant of guardSet that ignores the return value.

Since: 0.1

Checking environment variable match

guardEquals :: MonadIO m => String -> String -> m a -> m (Maybe a) Source #

guardEquals var expected io runs io iff

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

Examples

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

Since: 0.2

guardEquals_ :: MonadIO m => String -> String -> m a -> m () Source #

Variant of guardEquals_ that ignores the return value.

Since: 0.2

Checking environment variable predicate

guardPredicate :: MonadIO m => String -> (String -> Bool) -> m a -> m (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_ :: MonadIO m => String -> (String -> Bool) -> m a -> m () Source #

Variant of guardPredicate that ignores the return value.

Since: 0.1