Skip to content

Commit ea00ccb

Browse files
committed
dataconnect: emulator.sh: add fancy command-line argument parsing for easier using, especially when using a custom data connect emulator binary.
1 parent 496779d commit ea00ccb

File tree

1 file changed

+115
-12
lines changed

1 file changed

+115
-12
lines changed

firebase-dataconnect/emulator/emulator.sh

Lines changed: 115 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,121 @@
1616

1717
set -euo pipefail
1818

19-
# Uncomment the line below to use a custom Data Connect Emulator binary
20-
#export DATACONNECT_EMULATOR_BINARY_PATH='...'
19+
readonly SCRIPT_DIR="$(dirname "$0")"
20+
readonly SELF_EXECUTABLE="$0"
21+
readonly LOG_PREFIX="[$0] "
22+
readonly DEFAULT_POSTGRESQL_STRING='postgresql://postgres:postgres@localhost:5432?sslmode=disable'
2123

22-
export FIREBASE_DATACONNECT_POSTGRESQL_STRING='postgresql://postgres:postgres@localhost:5432?sslmode=disable'
23-
echo "[$0] export FIREBASE_DATACONNECT_POSTGRESQL_STRING='$FIREBASE_DATACONNECT_POSTGRESQL_STRING'"
24+
function main {
25+
parse_args "$@"
2426

25-
readonly FIREBASE_ARGS=(
26-
firebase
27-
--debug
28-
emulators:start
29-
--only auth,dataconnect
30-
)
27+
log "FIREBASE_DATACONNECT_POSTGRESQL_STRING=${FIREBASE_DATACONNECT_POSTGRESQL_STRING:-<undefined>}"
28+
log "DATACONNECT_EMULATOR_BINARY_PATH=${DATACONNECT_EMULATOR_BINARY_PATH:-<undefined>}"
3129

32-
echo "[$0] Running command: ${FIREBASE_ARGS[*]}"
33-
exec "${FIREBASE_ARGS[@]}"
30+
readonly FIREBASE_ARGS=(
31+
firebase
32+
--debug
33+
emulators:start
34+
--only auth,dataconnect
35+
)
36+
37+
log "Running command: ${FIREBASE_ARGS[*]}"
38+
exec "${FIREBASE_ARGS[@]}"
39+
}
40+
41+
function parse_args {
42+
local emulator_binary=''
43+
local postgresql_string="${DEFAULT_POSTGRESQL_STRING}"
44+
local wipe_and_restart_postgres_pod=0
45+
46+
local OPTIND=1
47+
local OPTERR=0
48+
while getopts ":c:p:hw" arg ; do
49+
case "$arg" in
50+
c) emulator_binary="${OPTARG}" ;;
51+
p) postgresql_string="${OPTARG}" ;;
52+
w) wipe_and_restart_postgres_pod=1 ;;
53+
h)
54+
print_help
55+
exit 0
56+
;;
57+
:)
58+
echo "ERROR: missing value after option: -${OPTARG}" >&2
59+
echo "Run with -h for help" >&2
60+
exit 2
61+
;;
62+
?)
63+
echo "ERROR: unrecognized option: -${OPTARG}" >&2
64+
echo "Run with -h for help" >&2
65+
exit 2
66+
;;
67+
*)
68+
echo "INTERNAL ERROR: unknown argument: $arg" >&2
69+
exit 1
70+
;;
71+
esac
72+
done
73+
74+
if [[ ! -z $emulator_binary ]] ; then
75+
export DATACONNECT_EMULATOR_BINARY_PATH="${emulator_binary}"
76+
fi
77+
78+
if [[ ! -z $postgresql_string ]] ; then
79+
export FIREBASE_DATACONNECT_POSTGRESQL_STRING="${postgresql_string}"
80+
fi
81+
82+
if [[ $wipe_and_restart_postgres_pod == "1" ]] ; then
83+
local wipe_args="${SCRIPT_DIR}/wipe_postgres_db.sh"
84+
log "Running command: ${wipe_args[*]}"
85+
"${wipe_args[@]}"
86+
87+
local start_args="${SCRIPT_DIR}/start_postgres_pod.sh"
88+
log "Running command: ${start_args[*]}"
89+
"${start_args[@]}"
90+
fi
91+
}
92+
93+
function print_help {
94+
echo "Firebase Data Connect Emulator Launcher Helper"
95+
echo
96+
echo "This script provides a convenient way to launch the Firebase Data Connect"
97+
echo "and Firebase Authentication emulators in a way that is amenable for running"
98+
echo "the integration tests."
99+
echo
100+
echo "Syntax: ${SELF_EXECUTABLE} [options]"
101+
echo
102+
echo "Options:"
103+
echo " -c <data_connect_emulator_binary_path>"
104+
echo " Uses the Data Connect Emulator binary at the given path. If not specified, "
105+
echo " or if specified as the empty string, then the emulator binary is downloaded."
106+
echo
107+
echo " -p <postgresql_connection_string>"
108+
echo " Uses the given string to connect to the PostgreSQL server. If not specified "
109+
echo " the the default value of \"${DEFAULT_POSTGRESQL_STRING}\" is used."
110+
echo " If specified as the empty string then an ephemeral PGLite server is used."
111+
echo
112+
echo " -w"
113+
echo " If specified, then a local PostgreSQL container is wiped and restarted"
114+
echo " before launching the emulators. This is accomplished by running the scripts"
115+
echo " ./wipe_postgres_db.sh followed by ./start_postgres_pod.sh."
116+
echo
117+
echo " -h"
118+
echo " Print this help screen and exit, as if successful."
119+
echo
120+
echo "Environment Variables:"
121+
echo " DATACONNECT_EMULATOR_BINARY_PATH"
122+
echo " This variable will be set to the value of the -c argument prior to launching"
123+
echo " the emulators. If the -c argument is not given then the value of this environment"
124+
echo " variable will be used as if it had been specified to the -c argument."
125+
echo
126+
echo " FIREBASE_DATACONNECT_POSTGRESQL_STRING"
127+
echo " This variable will be set to the value of the -p argument prior to launching"
128+
echo " the emulators. If the -p argument is not given then the value of this environment"
129+
echo " variable will be set to \"${DEFAULT_POSTGRESQL_STRING}\"."
130+
}
131+
132+
function log {
133+
echo "${LOG_PREFIX}$*"
134+
}
135+
136+
main "$@"

0 commit comments

Comments
 (0)