package dotenv
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=7d7cb7dd9b428ac3db8f2950add30ff7864eb2bbe0b218d33fec75f00160ab2e
sha512=3effccc7feec286354d1f942bfbdf8c854ba76bb1bb1ac5e6f060c4a127d0fc87c0f998e389a729c380b11ab969c19256f0fe2ac418077bec6170f05b0eeff6c
Description
This tool allows for the user to keep a file (default .env
, hence the name) which contains environment variables to be exported when running locally. When deployed, the file will not be available and the variables will be read from the environment, as always.
This is a port of JavaScript's Dotenv (https://github.com/motdotla/dotenv).
Published: 04 Nov 2019
README
Dotenv
Small lib to allow storing config separate from code.
This tool allows for the user to keep a file (default .env
, hence the name) which contains environment variables to be exported when running locally. When deployed, the file will not be available and the variables will be read from the environment, as always.
This is a port of JavaScript's Dotenv (https://github.com/motdotla/dotenv).
I have adapted it a little, but the result is the same.
Install
With opam:
opam install dotenv
Usage
Dotenv should be the first thing to be run on you program's entry function.
Ex:
let _ =
Dotenv.export () |> ignore
let whatever_secret = Sys.get_env_opt "secret" in
rest of the app...
This will read from a .env
file at the base of your app and populate the environment with the variables that it find in export format (VAR=value
).
Methods
Dotenv has two methods: parse
and export
.
export
will run through the env file and add the valid variables to the environment. They are then accessible via Sys.get_env
exactly as if they were on the deployment (or development) environment when the program started.
parse
will run through the env file and get all the variables onto an association list, so that it can then be used withouth having the variables on the environment. This is probably not used in most cases. Can be useful for loading files that aren't secret or for some types of debugging. Don't forget the debug option, there's a lot of good info there.
Arguments to the functions:
export
and parse
have three possible arguments, all optional:
debug
:bool (defaults to [false]) -> If true, will log the operations that dotenv is executing. It's useful for debug if there's something unexpected in read variables.path
:string (defaults to [".env"]) -> Points to where the.env
file is. This option allows you to use env files in different dirs or to simulate different environments easily.encoding
: [< Uutf.decoder_encoding ] -> Used to read files in a different encoding. We're usingUutf
for this. Accepted values:[ `UTF_16 | `UTF_16BE | `UTF_16LE | `UTF_8 | `US_ASCII | `ISO_8859_1]
Parse Rules
The parse rules used are pretty much the same as dotenv for JS - from which I partly used the text below, with some edits -, with some small differences that are presented next. The parsing will be the same for parse
or export
methods, but the latter will add VAR
to the application environment with value VALUE
:
VAR=value
becomes("VAR", "value")
spaces are respected:
VAR=some value
becomes("VAR", "some value")
empty lines are skipped
lines beginning with
#
or that don't respect the export format are treated as commentsempty values become empty strings:
VAR=
becomes("VAR", "")
inner quotes are maintained (think JSON):
JSON={"foo": "bar"}
becomes("JSON", "{\"foo\": \"bar\"}")
whitespace is removed from both ends of unquoted values:
VAR= some value
becomes("VAR", "some value")
single quoted values become strings:
SINGLE_QUOTE='quoted'
becomes(SINGLE_QUOTE, "quoted")
single and double quoted values maintain whitespace from both ends:
VAR=" some value "
becomes("VAR", " some value ")