Skip to content

Commit be6b51b

Browse files
committed
ENH: Support .env file
1 parent 99c916a commit be6b51b

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

sandbox-run

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,24 @@ paths='
4444
/usr
4545
'
4646

47-
split_args_by_lf () {
48-
lf='
47+
lf='
4948
'
50-
printf '%s' "$1" | case "$1" in *$lf*) cat ;; *) tr ' ' '\n' ;; esac
51-
}
52-
IFS='
53-
' # Split args only on newline
49+
split_args_by_lf () { printf '%s' "$1" | case "$1" in *$lf*) cat ;; *) tr ' ' '\n' ;; esac; }
50+
# Support BWRAP_ARGS passed to the process as well as via .env file
51+
prev_BWRAP_ARGS="${BWRAP_ARGS:-}"
52+
# Init env from dotenv file
53+
# shellcheck disable=SC2046
54+
[ ! -e "$cwd/.env" ] || { . "$cwd/.env"; export $(grep -Pzo '(?m)^\w*(?==)' "$cwd/.env" | tr '\0' '\n'); }
55+
IFS="$lf" # Split args only on newline
5456
# shellcheck disable=SC2046
55-
set -- $(split_args_by_lf "${BWRAP_ARGS:-}") "$bin" "$@"
57+
set -- $(split_args_by_lf "${BWRAP_ARGS:-}") $(split_args_by_lf "${prev_BWRAP_ARGS:-}") "$bin" "$@"
5658
unset IFS
5759

5860
home="$cwd/.sandbox-home"
5961
mkdir -p "$home/tmp"
6062

61-
# Set env vars
63+
# Pass our own redacted copy of env
64+
# Expose all vars passed exclusively to this process (i.e. not its parent)
6265
IFS=$(printf '\037')
6366
for var in $(env -0 |
6467
grep -Ez -e '^('\
@@ -72,6 +75,7 @@ for var in $(env -0 |
7275
paste -z -s -d '|'))$" |
7376
paste -z -s -d '|'))=" |
7477
grep -Ezv -e '^(_|LS_COLORS|PS1)=' |
78+
grep -Ezv -e '^(BWRAP_ARGS)=' |
7579
tr '\0' '\037'); do
7680
set -- --setenv "${var%%=*}" "${var#*=}" "$@"
7781
done

tests/test-bwrap-opts.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
. "${0%/*}/_init.sh"
5+
6+
BWRAP_ARGS='--ro-bind /etc/os-release /file' sandbox-run cat /file

tests/test-env-vars.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
. "${0%/*}/_init.sh"
5+
6+
7+
FOOBAR=1 sandbox-run sh -c 'set -u; echo $FOOBAR'
8+
echo "FOOBAR_DOTENV=1" > .env
9+
FOOBAR=1 sandbox-run sh -c 'set -u; echo $FOOBAR_DOTENV'
10+
# Sanity checks
11+
sandbox-run sh -c 'set -u; echo $PWD'
12+
! sandbox-run sh -c 'set -u; echo $NONEXISTENT' 2>/dev/null

0 commit comments

Comments
 (0)