Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Add --env-file #236

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ name = "cargo-watch"
camino = "1.1.2"
cargo_metadata = "0.15.2"
clap = "2.33.1"
dotenvy = "0.15.6"
log = "0.4.17"
notify-rust = "4.7.0"
shell-escape = "0.1.5"
Expand Down
17 changes: 14 additions & 3 deletions cargo-watch.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ cargo-watch(1) -- watches over your Cargo project's source

## SYNOPSIS

cargo watch [`--exec` | `-x` <cargo subcommand>]... [`--shell` | `-s` <command>]... [`--ignore` | `-i` <pattern>]... [`--watch` | `-w` <path>]... [`--features` <list>] [`--no-restart`] [`--watch-when-idle`] [`--clear` | `-c`] [`--use-shell` <program>] [`--workdir` | `-C` <path>] [`--postpone`] [`--poll` <interval>] [`--delay` | `-d` <seconds>] [`--no-vcs-ignores`] [`--no-dot-ignores`] [`--ignore-nothing`] [`--debug` ] [`--why`] [`--quiet`] [`--version` | `-V`] [-- <command>...]
cargo watch [options] [`--exec` | `-x` <cargo subcommand>]... [`--shell` | `-s` <command>]... [-- <command>...]
cargo watch [`--version` | `-V`]
cargo watch [`--help` | `-h`]

## DESCRIPTION

Expand Down Expand Up @@ -67,11 +69,20 @@ Ignore nothing, not even target/ and .git/.
* `-c`, `--clear`:
Clear the screen before each run.

* `-E`, `--env-var` <key>=<value>:
Inject an environment variable.

* `--env-file` <path>:
Inject environment variables from a dotenv-style file.

* `-B` <value>:
Inject `RUST_BACKTRACE=<value>` into the environment.

This is useful when panics are handled and you want a backtrace to be printed: usually you’d have to add `RUST_BACKTRACE=1` yourself, this shortens it to `-B1`!

* `-L` <value>:
Inject `RUST_LOG=<value>` into the environment.

* `-C`, `--workdir` <path>:
Change the working directory to the <path> given.

Expand Down Expand Up @@ -163,9 +174,9 @@ If you want more verbose output, try running with the `--debug` flag. Note that

### KNOWN BUGS

In 7.8.0, the `--workdir` option changes the directory before any other options are processed, so e.g. `--watch` paths may not work as expected. This has to be fixed upstream, see [watchexec#188].
In 7.8.0, the `--workdir` option changes the directory before any other options are processed, so e.g. `--watch` paths may not work as expected. This is fixed upstream in Watchexec library 2.

[watchexec#188]: https://github.com/watchexec/watchexec/issues/188#issuecomment-829138116
In 8.x, the `-B`, `-L`, `-E`, `--env-var`, `--env-file` options inject environment variables into the main cargo-watch process, which may be unsound in some situations and may also affect cargo-watch itself. This is fixed upstream in Watchexec library 2.

## TROUBLESHOOTING

Expand Down
3 changes: 3 additions & 0 deletions completions/zsh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ args=(
'(-i --ignore)'{-i+,--ignore=}'[Ignore changes to paths matching the pattern]:pattern'
'(-w --watch)'{-w+,--watch=}'[Watch a specific directory]:path:_path_files -/'
'(-C --workdir)'{-C+,--workdir=}'[Change working directory of command]:path:_path_files -/'
'(-E --env-var)'{-E+,--env-var=}'[Add an environment variable to the command]:pattern'
'-B=[Inject RUST_BACKTRACE=value into the environment]:value'
'-L=[Inject RUST_LOG=value into the environment]:value'
'--no-dot-ignores[Skip .ignore files]'
'--no-vcs-ignores[Skip .gitignore files]'
'--ignore-nothing[Ignore nothing, not even target/ and .git/]'
Expand All @@ -31,6 +33,7 @@ args=(
'--delay=[File updates debounce delay]:seconds'
'--poll=[Forces polling mode]:interval'
'--features=[List of features passed to cargo invocations]:features'
'--env-file=[Inject environment variables from a file]:path:_path_files -/'
'(-)1:command: _command_names -e'
'*::arguments:{ _comp_priv_prefix=( $cmd -n ${(kv)opt_args[-u]} ) ; _normal }'
)
Expand Down
10 changes: 10 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ pub fn parse() -> ArgMatches<'static> {
.min_values(1)
.number_of_values(1)
)
.arg(
Arg::with_name("env-files")
.help("Set environment variables from a .env file")
.long("env-file")
.takes_value(true)
.multiple(true)
.empty_values(false)
.min_values(1)
.number_of_values(1)
)
.arg(
Arg::with_name("rust-backtrace")
.help("Inject RUST_BACKTRACE=VALUE (generally you want to set it to 1) into the environment")
Expand Down
24 changes: 24 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use camino::Utf8PathBuf;
use clap::values_t;
use stderrlog::Timestamp;
Expand Down Expand Up @@ -51,6 +53,28 @@ fn main() -> Result<()> {
std::env::set_var("RUST_LOG", l);
}

if matches.is_present("env-files") {
for file in values_t!(matches, "env-files", PathBuf).unwrap_or_else(|e| e.exit()) {
for item in dotenvy::from_path_iter(&file).unwrap_or_else(|e| {
clap::Error::with_description(
&format!("Failed to read .env file {file:?}: {e}"),
clap::ErrorKind::ValueValidation,
)
.exit()
}) {
let (key, var) = item.unwrap_or_else(|e| {
clap::Error::with_description(
&format!("Malformed pair in .env file {file:?}: {e}"),
clap::ErrorKind::ValueValidation,
)
.exit()
});
// Soundness: not great, it'll get better with watchexec 2
std::env::set_var(key, var);
}
}
}

if matches.is_present("env-vars") {
for pair in values_t!(matches, "env-vars", String).unwrap_or_else(|e| e.exit()) {
if let Some((key, var)) = pair.split_once('=') {
Expand Down