Skip to content

Commit

Permalink
Release of dotenvy 0.15.1
Browse files Browse the repository at this point in the history
The following is also duplicated in the changelog.

- `dotenv` crate forked as `dotenvy`
- `dotenv_codegen` forked as `dotenvy_codgen`
- `dotenv_codegen_implementation` forked as `dotenvy_codegen_impl`
- Crate description for dotenvy_codegen
- Crate description for dotenvy_codgen_impl
- New language in README
- MIT license badge in README
- Generate helpful errors from dotenv! macro (full merge of [dotenv-rs/dotenv #58](https://github.com/dotenv-rs/dotenv/pull/57/files#))

- replaced deprecated `std::env_home:dir()` with `dirs:home_dir`
- Better handling of `home_dir` (merge of [dotenv-rs/dotenv #62](https://github.com/dotenv-rs/dotenv/pull/62/files#))
- assertions dealing with `Result` (based on [dotenv-rs/dotenv #57](https://github.com/dotenv-rs/dotenv/pull/57/files#))
- upgraded clap in `dotenvy` bin from v2 to v3.1 (covers [dotenv-rs/dotenv #76](https://github.com/dotenv-rs/dotenv/pull/76/files))

- example folder. The simple example has been moved to the README.
- `extern`
- unnecessary `use` statements in doc examples
  • Loading branch information
allan2 committed Feb 28, 2022
1 parent 3c1a77b commit ea1572f
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 284 deletions.
42 changes: 35 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.15.1] - 2022-02-28

### Added

- `dotenv` crate forked as `dotenvy`
- `dotenv_codegen` forked as `dotenvy_codgen`
- `dotenv_codegen_implementation` forked as `dotenvy_codegen_impl`
- Crate description for dotenvy_codegen
- Crate description for dotenvy_codgen_impl
- New language in README
- MIT license badge in README
- Generate helpful errors from dotenv! macro (full merge of [dotenv-rs/dotenv #58](https://github.com/dotenv-rs/dotenv/pull/57/files#))

### Changed

- replaced deprecated `std::env_home:dir()` with `dirs:home_dir`
- Better handling of `home_dir` (merge of [dotenv-rs/dotenv #62](https://github.com/dotenv-rs/dotenv/pull/62/files#))
- assertions dealing with `Result` (based on [dotenv-rs/dotenv #57](https://github.com/dotenv-rs/dotenv/pull/57/files#))
- upgraded clap in `dotenvy` bin from v2 to v3.1 (covers [dotenv-rs/dotenv #76](https://github.com/dotenv-rs/dotenv/pull/76/files))

## [Unreleased]
### Removed

- example folder. The simple example has been moved to the README.
- `extern`
- unnecessary `use` statements in doc examples

## [0.15.0] - 2019-10-21

### Changed

- Undeprecate `iter` methods
- Library no longer has any dependencies

### Added

- Support for variables with a `.` in their name
- Support `\n` in double-quoted lines
- Support for variable substitution

## [0.14.1] - 2019-05-14

### Changed

- Deprecate `iter` methods.

## [0.14.0] - 2019-05-07

### Changed

- Switched repo to use cargo workspaces.
- Renamed dotenv_codegen_impl to dotenv_codegen_implementation since we no longer own the original crate.
- Update code to 2018 edition



[Unreleased]: https://github.com/dotenv-rs/dotenv/compare/v0.15.0...HEAD
[unreleased]: https://github.com/dotenv-rs/dotenv/compare/v0.15.0...HEAD
[0.15.0]: https://github.com/dotenv-rs/dotenv/compare/v0.14.1...v0.15.0
[0.14.1]: https://github.com/dotenv-rs/dotenv/compare/v0.14.0...v0.14.1
[0.14.0]: https://github.com/dotenv-rs/dotenv/compare/v0.13.0...v0.14.0
[0.14.0]: https://github.com/dotenv-rs/dotenv/compare/v0.13.0...v0.14.0
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[workspace]

members = [
"dotenv",
"dotenv_codegen",
"dotenv_codegen_implementation",
]
members = ["dotenv", "dotenv_codegen", "dotenv_codegen_impl"]
2 changes: 1 addition & 1 deletion LICENSE.md → LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
130 changes: 31 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,129 +1,61 @@
# rust-dotenv
# dotenvy

![CI](https://github.com/dotenv-rs/dotenv/workflows/CI/badge.svg)
[![codecov](https://codecov.io/gh/dotenv-rs/dotenv/branch/master/graph/badge.svg)](https://codecov.io/gh/dotenv-rs/dotenv)
[![Crates.io](https://img.shields.io/crates/v/dotenv.svg)](https://crates.io/crates/dotenv)
[![crates.io](https://img.shields.io/crates/v/dotenvy.svg)](https://crates.io/crates/dotenv)
![CI](https://github.com/allan2/dotenvy/workflows/CI/badge.svg)
[![Released API docs](https://docs.rs/dotenvy/badge.svg)](https://docs.rs/dotenvy)
[![codecov](https://codecov.io/gh/allan2/dotenvy/branch/master/graph/badge.svg)](https://codecov.io/gh/allan2/dotenvy)

**Achtung!** This is a v0.\* version! Expect bugs and issues all around.
Submitting pull requests and issues is highly encouraged!
A well-maintained fork of the [dotenv](https://github.com/dotenv-rs/dotenv) crate.

Quoting [bkeepers/dotenv][dotenv]:
This library loads environment variables from a _.env_ file. This is convenient for dev environments.

> Storing [configuration in the environment](http://www.12factor.net/config)
> is one of the tenets of a [twelve-factor app](http://www.12factor.net/).
> Anything that is likely to change between deployment environments–such as
> resource handles for databases or credentials for external services–should
> be extracted from the code into environment variables.
## Components

This library is meant to be used on development or testing environments in
which setting environment variables is not practical. It loads environment
variables from a `.env` file, if available, and mashes those with the actual
environment variables provided by the operative system.
1. `dotenvy` crate - A well-maintained fork of the `dotenv` crate.
2. `dotenvy_codegen` crate - A macro for compile time dotenv inspection.
3. `dotenvy_codgen_impl` crate - Internal implementation for dotenvy_codegen.
4. `dotenvy` CLI tool for running a command using the environment from a _.env_ file (currently Unix only)

Usage
----
## Usage

The easiest and most common usage consists on calling `dotenv::dotenv` when the
application starts, which will load environment variables from a file named
`.env` in the current directory or any of its parents; after that, you can just call
the environment-related method you need as provided by `std::os`.
### Loading at runtime

If you need finer control about the name of the file or its location, you can
use the `from_filename` and `from_path` methods provided by the crate.

`dotenv_codegen` provides the `dotenv!` macro, which
behaves identically to `env!`, but first tries to load a `.env` file at compile
time.

Examples
----

A `.env` file looks like this:

```sh
# a comment, will be ignored
REDIS_ADDRESS=localhost:6379
MEANING_OF_LIFE=42
```

You can optionally prefix each line with the word `export`, which will
conveniently allow you to source the whole file on your shell.

A sample project using Dotenv would look like this:

```rust
extern crate dotenv;

use dotenv::dotenv;
```rs
use dotenvy::dotenv;
use std::env;

fn main() {
dotenv().ok();

for (key, value) in env::vars() {
println!("{}: {}", key, value);
println!("{key}: {value}");
}
}
```

Variable substitution
----
### Loading at compile time

It's possible to reuse variables in the `.env` file using `$VARIABLE` syntax.
The syntax and rules are similar to bash ones, here's the example:
The `dotenv!` macro provided by `dotenvy_codegen` crate can be used.

Warning: there is an outstanding issue with rust-analyzer ([rust-analyzer #9606](https://github.com/rust-analyzer/rust-analyzer/issues/9606)) related to the `dotenv!` macro

```sh
## Why does this fork exist?

VAR=one
VAR_2=two

This comment has been minimized.

Copy link
@pksunkara

pksunkara Mar 7, 2022

Why were all this example code removed? I think they are helpful.

The original dotenv crate has not been updated since June 26, 2020. Attempts to reach the authors and present maintainer were not successful ([dotenv-rs/dotenv #74](https://github.com/dotenv-rs/dotenv/issues/74)).

# Non-existing values are replaced with an empty string
RESULT=$NOPE #value: '' (empty string)
This fork is intended to serve as the development home for the dotenv implementation in Rust.

# All the letters after $ symbol are treated as the variable name to replace
RESULT=$VAR #value: 'one'
## What are the differences from the original?

# Double quotes do not affect the substitution
RESULT="$VAR" #value: 'one'
This repo fixes:

# Different syntax, same result
RESULT=${VAR} #value: 'one'
- home directory works correctly (no longer using the deprecated `std::env::home_dir`)
- more helpful errors for `dotenv!` ([dotenv-rs/dotenv #57](https://github.com/dotenv-rs/dotenv/pull/57/files#))

# Curly braces are useful in cases when we need to use a variable with non-alphanumeric name
RESULT=$VAR_2 #value: 'one_2' since $ with no curly braces stops after first non-alphanumeric symbol
RESULT=${VAR_2} #value: 'two'
For a full list of changes, read the [changelog](./CHANGELOG.md).

# The replacement can be escaped with either single quotes or a backslash:
RESULT='$VAR' #value: '$VAR'
RESULT=\$VAR #value: '$VAR'

# Environment variables are used in the substutution and always override the local variables
RESULT=$PATH #value: the contents of the $PATH environment variable
PATH="My local variable value"
RESULT=$PATH #value: the contents of the $PATH environment variable, even though the local variable is defined
```
## Are you a usurper of the dotenv legacy?

Dotenv will parse the file, substituting the variables the way it's described in the comments.


Using the `dotenv!` macro
------------------------------------

Add `dotenv_codegen` to your dependencies, and add the following to the top of
your crate:

```rust
#[macro_use]
extern crate dotenv_codegen;
```

Then, in your crate:

```rust
fn main() {
println!("{}", dotenv!("MEANING_OF_LIFE"));
}
```
Legend has it that the Lost Maintainer will return, merging changes from `dotenvy` into `dotenv` with such thrust that all `Cargo.toml`s will lose one keystroke. Only then shall the Rust dotenv crateverse be united in true harmony.

[dotenv]: https://github.com/bkeepers/dotenv
Until then, this repo dutifully carries on the dotenv torch. It is actively maintained. Contributions and PRs are very welcome!
20 changes: 11 additions & 9 deletions dotenv/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dotenv"
version = "0.15.0"
name = "dotenvy"
version = "0.15.1"
authors = [
"Noemi Lapresta <noemi.lapresta@gmail.com>",
"Craig Hills <chills@gmail.com>",
Expand All @@ -9,24 +9,26 @@ authors = [
"Sean Griffin <sean@seantheprogrammer.com>",
"Adam Sharp <adam@sharplet.me>",
"Arpad Borsos <arpad.borsos@googlemail.com>",
"Allan Zhang <al@ayz.ai>",
]
description = "A `dotenv` implementation for Rust"
homepage = "https://github.com/dotenv-rs/dotenv"
description = "A well-maintained fork of the `dotenv` crate"
homepage = "https://github.com/allan2/dotenvy"
readme = "../README.md"
keywords = ["environment", "env", "dotenv", "settings", "config"]
keywords = ["dotenv", "env", "environment", "settings", "config"]
license = "MIT"
repository = "https://github.com/dotenv-rs/dotenv"
repository = "https://github.com/allan2/dotenvy"
edition = "2018"

[[bin]]
name = "dotenv"
name = "dotenvy"
required-features = ["cli"]

[dependencies]
clap = { version = "2", optional = true }
clap = { version = "3.1", optional = true }
dirs = "4.0"

[dev-dependencies]
tempfile = "3.0.0"
tempfile = "3.3.0"

[features]
cli = ["clap"]
10 changes: 0 additions & 10 deletions dotenv/examples/simple.rs

This file was deleted.

38 changes: 17 additions & 21 deletions dotenv/src/bin/dotenv.rs → dotenv/src/bin/dotenvy.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
extern crate clap;
extern crate dotenv;

use clap::{App, AppSettings, Arg};
use clap::Arg;
use std::os::unix::process::CommandExt;
use std::process::{exit, Command};
use std::process;

macro_rules! die {
($fmt:expr) => ({
eprintln!($fmt);
exit(1);
process::exit(1);
});
($fmt:expr, $($arg:tt)*) => ({
eprintln!($fmt, $($arg)*);
exit(1);
process::exit(1);
});
}

fn make_command(name: &str, args: Vec<&str>) -> Command {
let mut command = Command::new(name);
fn make_command(name: &str, args: Vec<&str>) -> process::Command {
let mut command = process::Command::new(name);

for arg in args {
command.arg(arg);
Expand All @@ -27,42 +24,41 @@ fn make_command(name: &str, args: Vec<&str>) -> Command {
}

fn main() {
let matches = App::new("dotenv")
let matches = clap::Command::new("dotenvy")
.about("Run a command using the environment in a .env file")
.usage("dotenv <COMMAND> [ARGS]...")
.setting(AppSettings::AllowExternalSubcommands)
.setting(AppSettings::ArgRequiredElseHelp)
.setting(AppSettings::UnifiedHelpMessage)
.override_usage("dotenvy <COMMAND> [ARGS]...")
.allow_external_subcommands(true)
.arg_required_else_help(true)
.arg(
Arg::with_name("FILE")
.short("f")
Arg::new("FILE")
.short('f')
.long("file")
.takes_value(true)
.help("Use a specific .env file (defaults to .env)"),
)
.get_matches();

match matches.value_of("FILE") {
None => dotenv::dotenv(),
Some(file) => dotenv::from_filename(file),
None => dotenvy::dotenv(),
Some(file) => dotenvy::from_filename(file),
}
.unwrap_or_else(|e| die!("error: failed to load environment: {}", e));

let mut command = match matches.subcommand() {
(name, Some(matches)) => {
Some((name, matches)) => {
let args = matches
.values_of("")
.map(|v| v.collect())
.unwrap_or(Vec::new());

make_command(name, args)
}
_ => die!("error: missing required argument <COMMAND>"),
None => die!("error: missing required argument <COMMAND>"),
};

if cfg!(target_os = "windows") {
match command.spawn().and_then(|mut child| child.wait()) {
Ok(status) => exit(status.code().unwrap_or(1)),
Ok(status) => process::exit(status.code().unwrap_or(1)),
Err(error) => die!("fatal: {}", error),
};
} else {
Expand Down
Loading

0 comments on commit ea1572f

Please sign in to comment.