Skip to content

Commit

Permalink
stdlib: add direnv_apply_dump <file> (direnv#587)
Browse files Browse the repository at this point in the history
After the changes to `direnv_load`, this function is needed by the users
that want to consume a `direnv dump` output asynchonously. For example
when the evaluation gets cached.
  • Loading branch information
zimbatm authored Feb 7, 2020
1 parent 987ddb3 commit 930e526
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions man/direnv-stdlib.1
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ Loads another \fB\fC\&.envrc\fR if found when searching from the parent director
.PP
NOTE: the other \fB\fC\&.envrc\fR is not checked by the security framework.

.SS \fB\fCdirenv\_apply\_dump <file>\fR
.PP
Loads the output of \fB\fCdirenv dump\fR that was stored in a file.

.SS \fB\fCdirenv\_load [<command\-generating\-dump\-output>]\fR
.PP
Applies the environment generated by running \fIargv\fP as a command. This is useful for adopting the environment of a child process \- cause that process to run "direnv dump" and then wrap the results with direnv\_load.
Expand Down
4 changes: 4 additions & 0 deletions man/direnv-stdlib.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ Loads another `.envrc` if found when searching from the parent directory up to /

NOTE: the other `.envrc` is not checked by the security framework.

### `direnv_apply_dump <file>`

Loads the output of `direnv dump` that was stored in a file.

### `direnv_load [<command-generating-dump-output>]`

Applies the environment generated by running *argv* as a command. This is useful for adopting the environment of a child process - cause that process to run "direnv dump" and then wrap the results with direnv_load.
Expand Down
8 changes: 8 additions & 0 deletions stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ const StdLib = "#!/usr/bin/env bash\n" +
" return ${exit_code:-0}\n" +
"}\n" +
"\n" +
"# Usage: direnv_apply_dump <file>\n" +
"#\n" +
"# Loads the output of `direnv dump` that was stored in a file.\n" +
"direnv_apply_dump() {\n" +
" local path=$1\n" +
" eval \"$(\"$direnv\" apply_dump \"$path\")\"\n" +
"}\n" +
"\n" +
"# Usage: PATH_add <path> [<path> ...]\n" +
"#\n" +
"# Prepends the expanded <path> to the PATH environment variable, in order.\n" +
Expand Down
8 changes: 8 additions & 0 deletions stdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ direnv_load() {
return ${exit_code:-0}
}

# Usage: direnv_apply_dump <file>
#
# Loads the output of `direnv dump` that was stored in a file.
direnv_apply_dump() {
local path=$1
eval "$("$direnv" apply_dump "$path")"
}

# Usage: PATH_add <path> [<path> ...]
#
# Prepends the expanded <path> to the PATH environment variable, in order.
Expand Down
22 changes: 20 additions & 2 deletions test/stdlib.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,34 @@ assert_eq() {
fi
}

# test find_up
test_name() {
echo "--- $*"
}

test_name find_up
(
load_stdlib
path=$(find_up "README.md")
assert_eq "$path" "$root/README.md"
)

# test source_up
test_name source_up
(
load_stdlib
cd scenarios/inherited
source_up
)

test_name direnv_apply_dump
(
tmpfile=$(mktemp)
cleanup() { rm "$tmpfile"; }
trap cleanup EXIT

load_stdlib
FOO=bar direnv dump > "$tmpfile"
direnv_apply_dump "$tmpfile"
assert_eq "$FOO" bar
)

echo OK

0 comments on commit 930e526

Please sign in to comment.