-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·63 lines (55 loc) · 1.52 KB
/
entrypoint.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
#!/bin/sh
set -eu
ENV_FILE_PATH=".env"
CONFIG_ROOT="ENV_CONFIG"
OUTPUT_FILE="./public/env-config.js"
generateOutput() {
echo "Generating JS configuration output to: $OUTPUT_FILE"
echo "window.$CONFIG_ROOT = {" >"$OUTPUT_FILE"
for line in $1; do
if beginswith REACT_APP_ "$line"; then
key=${line%%=*}
value=${line#*=}
printf " - Found '%s'" "$key"
printf "\t%s: '%s',\n" "$key" "$value" >>"$OUTPUT_FILE"
fi
done
echo "}" >>"$OUTPUT_FILE"
}
beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
usage() {
printf
printf "Arguments:"
printf "\t-e\t Sets the .env file to use (default: .env)"
printf "\t-o\t Sets the output filename (default: ./public/env-config.js)"
printf "\t-c\t Sets the JS configuration key (default: ENV_CONFIG)"
printf
printf "Example:"
printf "\tbash entrypoint.sh -e .env -o env-config.js"
}
while getopts "e:o:c:" opt; do
case $opt in
e) ENV_FILE_PATH=$OPTARG ;;
o) OUTPUT_FILE=$OPTARG ;;
c) CONFIG_ROOT=$OPTARG ;;
:)
echo "Error: -${OPTARG} requires a value"
exit 1
;;
*)
usage
exit 1
;;
esac
done
# Load .env file if supplied
ENV_FILE=""
if [ -f "$ENV_FILE_PATH" ]; then
echo "Loading environment file from '$ENV_FILE_PATH'"
ENV_FILE="$(cat "$ENV_FILE_PATH")"
fi
# Load system environment variables
ENV_VARS=$(printenv)
# Merge .env file with env variables
ALL_VARS="$ENV_FILE\n$ENV_VARS"
generateOutput "$ALL_VARS"