-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuserloadenv.sh
66 lines (57 loc) · 1.58 KB
/
userloadenv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
ENV_FILES="" # list of env files (empty by default)
VERBOSE=0
show_help() {
cat <<EOF
Usage: $(basename "$0") [--env-file=FILE ...] [--verbose] [--help] [VAR=val ...] COMMAND [ARGS...]
Options:
--env-file=FILE Specify an env file to load (can be used multiple times)
--verbose Print each env file loaded
--help Show this help message
Load .env file as environment variables and execute a command
If no --env-file is given, defaults to loading ./.env
EOF
}
# Parse --env-file=... options
while [ $# -gt 0 ]; do
case "$1" in
--env-file=*)
FILE="${1#--env-file=}"
case "$FILE" in
/*) ;; # absolute path, use as is
./*) ;; # relative path, use as is
../*) ;; # relative path, use as is
\~/*) FILE="${HOME}/${FILE#\~\/}" ;; # relative to home path
*) FILE="./$FILE" ;; # make relative
esac
ENV_FILES="$ENV_FILES $FILE"
shift
;;
--verbose)
VERBOSE=1
shift
;;
--help)
show_help
exit 0
;;
--) shift; break ;; # stop parsing options
*) break ;; # first non-option argument
esac
done
# Fallback to .env only if no --env-file was specified
if [ -z "$ENV_FILES" ]; then
ENV_FILES="./.env"
fi
set -a
# Source all env files
for f in $ENV_FILES; do
if [ -f "$f" ]; then
[ "$VERBOSE" -eq 1 ] && echo "Loading env from $f" >&2
. "$f"
else
[ "$VERBOSE" -eq 1 ] && echo "Skipping missing env file: $f" >&2
fi
done
# Execute command with optional inline env vars
exec env "$@"