Skip to content

Commit

Permalink
Merge pull request #44 from kianmeng/misc-doc-changes
Browse files Browse the repository at this point in the history
Misc doc changes
  • Loading branch information
iloveitaly authored Jun 10, 2021
2 parents a2083bc + 5b931fb commit d6fd3f3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{lib,test}/**/*.{ex,exs}"],
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
32 changes: 27 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
/ebin
/deps
/doc
/mix.lock
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez
_build

# Ignore package tarball (built via "mix hex.build").
dotenv-*.tar

# Temporary files for e.g. tests.
/tmp/

# Misc.
mix.lock
File renamed without changes.
51 changes: 34 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
# Dotenv for Elixir [![Hex pm](http://img.shields.io/hexpm/v/dotenv.svg?style=flat)](https://hex.pm/packages/dotenv) ![.github/workflows/main.yaml](https://github.com/iloveitaly/dotenv_elixir/workflows/.github/workflows/main.yaml/badge.svg)
# Dotenv for Elixir

[![Build](https://github.com/avdi/dotenv_elixir/actions/workflows/main.yaml/badge.svg)](https://github.com/avdi/dotenv_elixir/actions/workflows/main.yaml)
[![Module Version](https://img.shields.io/hexpm/v/dotenv.svg)](https://hex.pm/packages/dotenv)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/dotenv/)
[![Total Download](https://img.shields.io/hexpm/dt/dotenv.svg)](https://hex.pm/packages/dotenv)
[![License](https://img.shields.io/hexpm/l/dotenv.svg)](https://github.com/avdi/dotenv_elixir/blob/master/LICENSE.md)
[![Last Updated](https://img.shields.io/github/last-commit/avdi/dotenv_elixir.svg)](https://github.com/avdi/dotenv_elixir/commits/master)

This is a port of @bkeepers' [dotenv](https://github.com/bkeepers/dotenv) project to Elixir. You can read more about dotenv on that project's page. The short version is that it simplifies developing projects where configuration is stored in environment variables (e.g. projects intended to be deployed to Heroku).

## WARNING: Not compatible with Elixir releases
> WARNING: Not compatible with Elixir releases
Elixir has an excellent configuration system and this dotenv implementation has
a serious limitation in that it isn't available at compile time. It fits very
Elixir has an excellent configuration system and this dotenv implementation
has a serious limitation in that it isn't available at compile time. It fits very
poorly into a deployment setup using Elixir releases, distillery, or similar.

Configuration management should be built around Elixir's existing configuration system. A good example is [Phoenix](http://www.phoenixframework.org/) which generates a
project where the production config imports the "secrets" from a file stored
outside of version control. Even if you're using this for development, the same
approach could be taken.
Configuration management should be built around Elixir's existing configuration
system. A good example is [Phoenix](http://www.phoenixframework.org/) which
generates a project where the production config imports the "secrets" from a
file stored outside of version control. Even if you're using this for
development, the same approach could be taken.

However, if you are using Heroku, Dokku, or another deployment process that does *not* use releases, read on!

### Quick Start
## Quick Start

The simplest way to use Dotenv is with the included OTP application. This will automatically load variables from a `.env` file in the root of your project directory into the process environment when started.

First add `dotenv` to your dependencies.
First add `:dotenv` to your dependencies.

For the latest release:

```elixir
{:dotenv, "~> 3.0.0"}
defp deps do
[
{:dotenv, "~> 3.0.0"}
]
end
```

Most likely, if you are deploying in a Heroku-like environment, you'll want to only load the package in a non-production environment:
Expand All @@ -44,7 +55,7 @@ Fetch your dependencies with `mix deps.get`.

Now, when you load your app in a console with `iex -S mix`, your environment variables will be set automatically.

#### Using Environment Variables in Configuration
## Using Environment Variables in Configuration

[Mix loads configuration before loading any application code.](https://github.com/elixir-lang/elixir/blob/52141f2a3fa69906397017883242948dd93d91b5/lib/mix/lib/mix/tasks/run.ex#L123) If you want to use `.env` variables in your application configuration, you'll need to load dotenv manually on application start and reload your application config:

Expand All @@ -63,7 +74,7 @@ defmodule App.Application do
end
```

#### Elixir 1.9 and older
## Elixir 1.9 and older

If you are running an old version of Elixir, you'll need to add the `:dotenv` application to your applications list when running in the `:dev` environment:

Expand All @@ -81,13 +92,13 @@ defp app_list(_), do: app_list
defp app_list, do: [...]
```

#### Reloading the `.env` file
## Reloading the `.env` file

The `Dotenv.reload!/0` function will reload the variables defined in the `.env` file.

More examples of the server API usage can be found in [dotenv_app_test.exs](https://github.com/avdi/dotenv_elixir/blob/master/test/dotenv_app_test.exs).

### Serverless API
## Serverless API

If you would like finer-grained control over when variables are loaded, or would like to inspect them, Dotenv also provides a serverless API for interacting with `.env` files.

Expand All @@ -96,9 +107,15 @@ The `load!/1` function loads variables into the process environment, and can be
Alternately, `load/1` will return a data structure of the variables read from the `.env` file:

```elixir
iex(1)> Dotenv.load
iex> Dotenv.load
%Dotenv.Env{paths: ["/elixir/dotenv_elixir/.env"],
values: %{"APP_TEST_VAR" => "HELLO"}}
```

For further details, see the inline documentation. Usage examples can be found in [dotenv_test.exs](https://github.com/avdi/dotenv_elixir/blob/master/test/dotenv_test.exs).

## Copyright and License

Copyright (c) 2014 Avdi Grimm

This library is released under the MIT License. See the [LICENSE.md](./LICENSE.md) file
for further details.
9 changes: 4 additions & 5 deletions lib/dotenv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ defmodule Dotenv do
@moduledoc """
This module implements both an OTP application API and a "serverless" API.
Server API
==========
## Server API
Start the application with `start/2` On starting, it will automatically export
the environment variables in the default path (`.env`).
The environment can then be reloaded with `reload!/0` or a specific path
or list of paths can be provided to `reload!/1`.
Serverless API
==============
## Serverless API
To use the serverless API, you can either load the environment variables with
`load!` (again, optionally passing in a path or list of paths), or you
Expand Down Expand Up @@ -116,7 +114,8 @@ defmodule Dotenv do
end

@doc """
Reads the env files at the provided `env_path` path(s) and returns the values in a `Dotenv.Env` struct.
Reads the env files at the provided `env_path` path(s) and returns the values
in a `Dotenv.Env` struct.
"""
@spec load(String.t() | :automatic | [String.t()]) :: Env.t()
def load(env_path \\ :automatic)
Expand Down
64 changes: 43 additions & 21 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
defmodule DotenvElixir.Mixfile do
use Mix.Project

@source_url "https://github.com/avdi/dotenv_elixir"
@version "3.1.0"

def project do
[
app: :dotenv,
version: "3.1.0",
version: @version,
elixir: "~> 1.0",
deps: deps(),
package: [
maintainers: ["Jared Norman"],
contributors: [
"Avdi Grimm",
"David Rouchy",
"Jared Norman",
"Louis Simoneau",
"Michael Bianco"
],
links: %{github: "https://github.com/avdi/dotenv_elixir"},
licenses: ["MIT"]
],
description: "A port of dotenv to Elixir"
docs: docs(),
package: package()
]
end

# Configuration for the OTP application
def application do
[mod: {Dotenv, [:automatic]}]
[
mod: {Dotenv, [:automatic]}
]
end

# Returns the list of dependencies in the format:
# { :foobar, git: "https://github.com/elixir-lang/foobar.git", tag: "0.1" }
#
# To specify particular versions, regardless of the tag, do:
# { :barbat, "~> 0.1", github: "elixir-lang/barbat.git" }
defp deps do
[{:ex_doc, ">= 0.0.0", only: :dev}]
[
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
]
end

defp docs do
[
extras: [
"LICENSE.md": [title: "License"],
"README.md": [title: "Overview"]
],
main: "readme",
source_url: @source_url,
source_ref: "v#{@version}",
formatters: ["html"]
]
end

defp package do
[
description: "A port of dotenv to Elixir",
maintainers: ["Jared Norman"],
contributors: [
"Avdi Grimm",
"David Rouchy",
"Jared Norman",
"Louis Simoneau",
"Michael Bianco"
],
licenses: ["MIT"],
links: %{
GitHub: @source_url
}
]
end
end

0 comments on commit d6fd3f3

Please sign in to comment.