Skip to content

Add 'get_dotenv/3' and 'fetch_dotenv!/2'#33

Open
arcanemachine wants to merge 1 commit into
fireproofsocks:mainfrom
arcanemachine:arcanemachine-patch-1
Open

Add 'get_dotenv/3' and 'fetch_dotenv!/2'#33
arcanemachine wants to merge 1 commit into
fireproofsocks:mainfrom
arcanemachine:arcanemachine-patch-1

Conversation

@arcanemachine
Copy link
Copy Markdown
Contributor

@arcanemachine arcanemachine commented Apr 27, 2026

These wrapper functions provide idiomatic naming behavior that mirrors that of System.get_env/2,3 and System.fetch_env!/2.

I added these because I was often getting confused between the different Dotenvy.env! functions (which one to use, and when), and harmonizing them against the style used by System get_env/fetch_env! built-ins made a lot of that confusion go away from me.

Names were chosen to avoid collision and confusion with the built-in System function names, since Dotenvy is conventionally imported in config/runtime.exs instead of being aliased. This allows a new reader to know that the imported function is a Dotenvy env getter (e.g. get_dotenv/3) at a glance, not the built-in System one (e.g. System.get_env/3).

This pull request does not replace or remove any existing functionality, but instead extends it for the sake of improved (in my opinion, at least) ergonomics.

The changes are summarized by the addition made to the cheatsheet:

# `get_dotenv/2,3` (alias of `env!/3`, but with fallback to nil; default type is string)
config :my_app,
  username: get_dotenv("PG_USERNAME"), # string type, fallback to nil
  database: get_dotenv("PG_DATABASE", "my_app_db"), # string type, fallback to "my_app_db"
  port: get_dotenv("PG_PORT", :integer), # fallback to nil
  pool_size: get_dotenv("PG_POOL_SIZE", :integer, 10) # fallback to 10

# `fetch_dotenv!/2` (alias of `env!/2` - raises if value not found; default type is string)
config :my_app,
  secret_key: fetch_dotenv!("SECRET_KEY"),
  secret_number: fetch_dotenv!("SECRET_NUMBER", :integer!)

Thanks!

These wrapper functions provide idiomatic naming that mirror the
behavior of `System.get_env/2,3` and `System.fetch_env!/2`.

Names were chosen to avoid collision with the `System` function names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant