-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocalenv
executable file
·43 lines (37 loc) · 1.13 KB
/
localenv
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
#!/bin/sh
# Copyright (c) 2024 Benjamin Holt -- MIT License
set -eo pipefail
# set -x # Uncomment for debugging
# Why doesn't /usr/bin/env have a -f option??
if [ -z "$1" -o "$1" == "-h" -o "$1" == "--help" ]; then
cat <<USAGE
Usage: localenv [-f FILE] <command>
This will run load environment variables from a file and run the command.
FILE defaults to 'local.env' or '.env' whichever is found first.
Warning: there may be issues with quoting and escaping of the command or
its arguments.
USAGE
exit 0
fi
if [ "$1" == "-f" ]; then
DotEnv="$2"
shift; shift
else
for f in "local.env" ".env"; do
if [ -f "$f" ]; then
DotEnv="$f"
break
fi
done
fi
if [ -z "$DotEnv" ]; then
echo "No environment file found; specify one with -f or run with -h for help" >&2
exit 1
fi
if [ ! -f "$DotEnv" ]; then
echo "Environment file '$DotEnv' not found" >&2
exit 1
fi
# Strip #-style comments and blank lines, then run the remaining lines with 'export'
`cat "$DotEnv" | sed -e 's/ *#.*//' -e '/^#.*/d' -e '/^ *$/d' -e 's/^/export /'`
$@ # Run remaining args as a command