Module

Env

Package
purescript-env
Repository
m-rinaldi/purescript-env

Configuration from the process environment, as one typed record: the variables a program needs are named and typed once, read in one go, and every missing or malformed one is reported together. A .env file in the working directory is read first, for development; a variable the environment already has keeps its value.

type Variables = ( "PORT" :: Maybe Int, "DB_PATH" :: String )

main = Env.load (Proxy :: _ (Record Variables)) >>= case _ of
  Left errors -> throw (Env.printErrors errors)
  Right vars -> start vars

The two halves are modules of their own and are re-exported from here: Env.Dot folds the file into the environment, Env.Typed reads the record. load is the one call that does both.

Meant to be imported qualified: Env.load, Env.read, Env.printErrors.

#load Source

load :: forall r rl. RowToList r rl => ReadRecord rl r => Proxy (Record r) -> Effect (Either Errors (Record r))

The .env file in the working directory, if there is one, folded into the process environment, and the variables the record names read from it: Env.Dot.environment and Env.Typed.read in one.

Re-exports from Env.Dot

#environmentFrom Source

environmentFrom :: String -> Effect (Object String)

Like environment, with the file named. A file that is not there is no error; one that cannot be read (a directory, no permission) is thrown.

#environment Source

environment :: Effect (Object String)

The process environment after the .env file in the working directory was folded into it.

Re-exports from Env.Typed

#Errors Source

type Errors = NonEmptyList EnvError

What stopped a record from being read: for each variable, that it is missing (EnvLookupError) or does not parse (EnvParseError), by name. At least one, or the record would have been read.

#EnvError Source

data EnvError

What went wrong with one variable: it is not set, or its text does not parse, with the name and then the instance's reason.

Constructors

Instances

#ParseValue Source

class ParseValue ty  where

How a variable's text becomes a value of the field's type. Write an instance for a type of your own. A Left is the reason the text does not parse, and completes the sentence The variable "NAME" …: "is not an integer", "is not one of console, resend", "is empty".

Members

Instances

#ReadRecord Source

class ReadRecord :: RowList Type -> Row Type -> Constraintclass ReadRecord (rl :: RowList Type) (rout :: Row Type) | rl -> rout where

Builds the record field by field from the row's list, collecting the errors of every field instead of stopping at the first.

Members

Instances

#ReadVariable Source

class ReadVariable ty  where

Looks a variable up and parses it: a Maybe field may be missing, any other has to be there. Exported because the compiler solves it where read is called; there is nothing to write an instance of it for.

Members

Instances

#read Source

read :: forall r rl. RowToList r rl => ReadRecord rl r => Proxy (Record r) -> Object String -> Either Errors (Record r)

The variables the record names, from an environment. Every field is a variable: "PORT" :: Int has to be there and parse, "PORT" :: Maybe Int may be missing but has to parse if it is there. A field of a type of your own needs a ParseValue instance. All problems are reported together.

#printErrors Source

printErrors :: Errors -> String

One message naming every variable in question, for a log or a stderr line before exiting.