Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdlib: direnv_load can now handle stdout outputs #551

Merged
merged 1 commit into from
Jan 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions cmd_dump.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
package main

import "fmt"
import (
"fmt"
"os"
)

// `direnv dump`
// CmdDump is `direnv dump`
var CmdDump = &Cmd{
Name: "dump",
Desc: "Used to export the inner bash state at the end of execution",
Args: []string{"[SHELL]"},
Args: []string{"[SHELL]", "[FILE]"},
Private: true,
Action: actionSimple(func(env Env, args []string) (err error) {
target := "gzenv"
w := os.Stdout

if len(args) > 1 {
target = args[1]
}

var filePath string
if len(args) > 2 {
filePath = args[2]
} else {
filePath = os.Getenv("DIRENV_DUMP_FILE_PATH")
}

if filePath != "" {
w, err = os.OpenFile(filePath, os.O_WRONLY, 0666)
if err != nil {
return err
}
}

shell := DetectShell(target)
if shell == nil {
return fmt.Errorf("unknown target shell '%s'", target)
}

fmt.Println(shell.Dump(env))
_, err = fmt.Fprintln(w, shell.Dump(env))

return
}),
Expand Down
40 changes: 35 additions & 5 deletions stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,18 +260,48 @@ const STDLIB = "#!/usr/bin/env bash\n" +
"# the results with direnv_load.\n" +
"#\n" +
"direnv_load() {\n" +
" local exports\n" +
" # backup and restore watches in case of nix-shell --pure\n" +
" local __watches=$DIRENV_WATCHES\n" +
" # Backup watches in case of `nix-shell --pure`\n" +
" local prev_watches=$DIRENV_WATCHES\n" +
" local prev_dump_file_path=${DIRENV_DUMP_FILE_PATH:-}\n" +
"\n" +
" # Create pipe\n" +
" DIRENV_DUMP_FILE_PATH=$(mktemp -u)\n" +
" export DIRENV_DUMP_FILE_PATH\n" +
" mkfifo \"$DIRENV_DUMP_FILE_PATH\"\n" +
"\n" +
" # Run program in the background\n" +
" (\"$@\")&\n" +
"\n" +
" exports=$(\"$direnv\" apply_dump <(\"$@\"))\n" +
" # Apply the output of the dump\n" +
" local exports\n" +
" exports=$(\"$direnv\" apply_dump \"$DIRENV_DUMP_FILE_PATH\")\n" +
" local es=$?\n" +
"\n" +
" # Regroup\n" +
" rm \"$DIRENV_DUMP_FILE_PATH\"\n" +
" wait # wait on the child process to exit\n" +
" local es2=$?\n" +
"\n" +
" if [[ $es -ne 0 ]]; then\n" +
" return $es\n" +
" fi\n" +
"\n" +
" if [[ $es2 -ne 0 ]]; then\n" +
" return $es2\n" +
" fi\n" +
"\n" +
" eval \"$exports\"\n" +
"\n" +
" export DIRENV_WATCHES=$__watches\n" +
" # Restore watches if the dump wiped them\n" +
" if [[ -z \"$DIRENV_WATCHES\" ]]; then\n" +
" export DIRENV_WATCHES=$prev_watches\n" +
" fi\n" +
" # Allow nesting\n" +
" if [[ -n \"$prev_dump_file_path\" ]]; then\n" +
" export DIRENV_DUMP_FILE_PATH=$prev_dump_file_path\n" +
" else\n" +
" unset DIRENV_DUMP_FILE_PATH\n" +
" fi\n" +
"}\n" +
"\n" +
"# Usage: PATH_add <path> [<path> ...]\n" +
Expand Down
40 changes: 35 additions & 5 deletions stdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,48 @@ source_up() {
# the results with direnv_load.
#
direnv_load() {
local exports
# backup and restore watches in case of nix-shell --pure
local __watches=$DIRENV_WATCHES
# Backup watches in case of `nix-shell --pure`
local prev_watches=$DIRENV_WATCHES
local prev_dump_file_path=${DIRENV_DUMP_FILE_PATH:-}

# Create pipe
DIRENV_DUMP_FILE_PATH=$(mktemp -u)
export DIRENV_DUMP_FILE_PATH
mkfifo "$DIRENV_DUMP_FILE_PATH"

# Run program in the background
("$@")&

exports=$("$direnv" apply_dump <("$@"))
# Apply the output of the dump
local exports
exports=$("$direnv" apply_dump "$DIRENV_DUMP_FILE_PATH")
local es=$?

# Regroup
rm "$DIRENV_DUMP_FILE_PATH"
wait # wait on the child process to exit
local es2=$?

if [[ $es -ne 0 ]]; then
return $es
fi

if [[ $es2 -ne 0 ]]; then
return $es2
fi

eval "$exports"

export DIRENV_WATCHES=$__watches
# Restore watches if the dump wiped them
if [[ -z "$DIRENV_WATCHES" ]]; then
export DIRENV_WATCHES=$prev_watches
fi
# Allow nesting
if [[ -n "$prev_dump_file_path" ]]; then
export DIRENV_DUMP_FILE_PATH=$prev_dump_file_path
else
unset DIRENV_DUMP_FILE_PATH
fi
}

# Usage: PATH_add <path> [<path> ...]
Expand Down
2 changes: 1 addition & 1 deletion test/scenarios/dump/.envrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ direnv_load env \
LS_COLORS='*.ogg=38;5;45:*.wav=38;5;45' \
LESSOPEN='||/usr/bin/lesspipe.sh %s' \
THREE_BACKSLASHES='\\\' \
bash -c "direnv dump"
bash -c "echo to stdout && echo to stderr >&2 && direnv dump"