Skip to content

Commit

Permalink
Add dotenv integration (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Mar 5, 2018
1 parent ec56336 commit 70234f6
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 81 deletions.
135 changes: 124 additions & 11 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ homepage = "https://github.com/casey/just"
readme = "crates-io-readme.md"

[dependencies]
ansi_term = "0.10"
ansi_term = "0.11"
assert_matches = "1.1.0"
atty = "0.2.1"
brev = "0.1.6"
clap = "2.0.0"
dotenv = "0.11.0"
edit-distance = "2.0.0"
itertools = "0.7"
lazy_static = "1.0.0"
Expand Down
28 changes: 28 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,34 @@ This is an x86_64 machine
- `env_var_or_default(key, default)` – Retrieves the environment variable with name `key`, returning `default` if it is not present.
==== Dotenv Integration
`just` will load environment variables from a file named `.env`. This file can be located in the same directory as your justfile or in a parent directory. These variables are environment variables, not `just` variables, and so must be accessed using `$VARIABLE_NAME` in recipes and backticks.
For example, if your `.env` file contains:
```
# a comment, will be ignored
DATABASE_ADDRESS=localhost:6379
SERVER_PORT=1337
```
And your justfile contains:
```make
serve:
@echo "Starting server with database $DATABASE_ADDRESS on port $SERVER_PORT..."
./server --database $DATABASE_ADDRESS --port $SERVER_PORT
```
`just serve` will output:
```sh
$ just serve
Starting server with database localhost:6379 on port 1337...
./server --database $DATABASE_ADDRESS --port $SERVER_PORT
```
=== Command Evaluation Using Backticks
Backticks can be used to store the result of commands:
Expand Down
26 changes: 15 additions & 11 deletions src/assignment_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@ use brev;

pub struct AssignmentEvaluator<'a: 'b, 'b> {
pub assignments: &'b Map<&'a str, Expression<'a>>,
pub dotenv: &'b Map<String, String>,
pub dry_run: bool,
pub evaluated: Map<&'a str, String>,
pub exports: &'b Set<&'a str>,
pub overrides: &'b Map<&'b str, &'b str>,
pub quiet: bool,
pub scope: &'b Map<&'a str, String>,
pub shell: &'b str,
pub dry_run: bool,
}

impl<'a, 'b> AssignmentEvaluator<'a, 'b> {
pub fn evaluate_assignments(
assignments: &Map<&'a str, Expression<'a>>,
dotenv: &'b Map<String, String>,
overrides: &Map<&str, &str>,
quiet: bool,
shell: &'a str,
dry_run: bool,
) -> RunResult<'a, Map<&'a str, String>> {
let mut evaluator = AssignmentEvaluator {
assignments: assignments,
evaluated: empty(),
exports: &empty(),
overrides: overrides,
quiet: quiet,
scope: &empty(),
shell: shell,
dry_run: dry_run,
evaluated: empty(),
exports: &empty(),
scope: &empty(),
assignments,
dotenv,
dry_run,
overrides,
quiet,
shell,
};

for name in assignments.keys() {
Expand Down Expand Up @@ -110,7 +113,7 @@ impl<'a, 'b> AssignmentEvaluator<'a, 'b> {
if self.dry_run {
Ok(format!("`{}`", raw))
} else {
Ok(self.run_backtick(raw, token)?)
Ok(self.run_backtick(self.dotenv, raw, token)?)
}
}
Expression::Concatination{ref lhs, ref rhs} => {
Expand All @@ -125,12 +128,13 @@ impl<'a, 'b> AssignmentEvaluator<'a, 'b> {

fn run_backtick(
&self,
dotenv: &Map<String, String>,
raw: &str,
token: &Token<'a>,
) -> RunResult<'a, String> {
let mut cmd = Command::new(self.shell);

cmd.export_environment_variables(self.scope, self.exports)?;
cmd.export_environment_variables(self.scope, dotenv, self.exports)?;

cmd.arg("-cu")
.arg(raw);
Expand Down
Loading

0 comments on commit 70234f6

Please sign in to comment.