From ea00ccb1e898aa660c00b2ea207ae07d72cc7cde Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Tue, 22 Jul 2025 18:26:19 +0000 Subject: [PATCH 1/2] dataconnect: emulator.sh: add fancy command-line argument parsing for easier using, especially when using a custom data connect emulator binary. --- firebase-dataconnect/emulator/emulator.sh | 127 ++++++++++++++++++++-- 1 file changed, 115 insertions(+), 12 deletions(-) diff --git a/firebase-dataconnect/emulator/emulator.sh b/firebase-dataconnect/emulator/emulator.sh index 6cd33d39a31..c31006f55c5 100755 --- a/firebase-dataconnect/emulator/emulator.sh +++ b/firebase-dataconnect/emulator/emulator.sh @@ -16,18 +16,121 @@ set -euo pipefail -# Uncomment the line below to use a custom Data Connect Emulator binary -#export DATACONNECT_EMULATOR_BINARY_PATH='...' +readonly SCRIPT_DIR="$(dirname "$0")" +readonly SELF_EXECUTABLE="$0" +readonly LOG_PREFIX="[$0] " +readonly DEFAULT_POSTGRESQL_STRING='postgresql://postgres:postgres@localhost:5432?sslmode=disable' -export FIREBASE_DATACONNECT_POSTGRESQL_STRING='postgresql://postgres:postgres@localhost:5432?sslmode=disable' -echo "[$0] export FIREBASE_DATACONNECT_POSTGRESQL_STRING='$FIREBASE_DATACONNECT_POSTGRESQL_STRING'" +function main { + parse_args "$@" -readonly FIREBASE_ARGS=( - firebase - --debug - emulators:start - --only auth,dataconnect -) + log "FIREBASE_DATACONNECT_POSTGRESQL_STRING=${FIREBASE_DATACONNECT_POSTGRESQL_STRING:-}" + log "DATACONNECT_EMULATOR_BINARY_PATH=${DATACONNECT_EMULATOR_BINARY_PATH:-}" -echo "[$0] Running command: ${FIREBASE_ARGS[*]}" -exec "${FIREBASE_ARGS[@]}" + readonly FIREBASE_ARGS=( + firebase + --debug + emulators:start + --only auth,dataconnect + ) + + log "Running command: ${FIREBASE_ARGS[*]}" + exec "${FIREBASE_ARGS[@]}" +} + +function parse_args { + local emulator_binary='' + local postgresql_string="${DEFAULT_POSTGRESQL_STRING}" + local wipe_and_restart_postgres_pod=0 + + local OPTIND=1 + local OPTERR=0 + while getopts ":c:p:hw" arg ; do + case "$arg" in + c) emulator_binary="${OPTARG}" ;; + p) postgresql_string="${OPTARG}" ;; + w) wipe_and_restart_postgres_pod=1 ;; + h) + print_help + exit 0 + ;; + :) + echo "ERROR: missing value after option: -${OPTARG}" >&2 + echo "Run with -h for help" >&2 + exit 2 + ;; + ?) + echo "ERROR: unrecognized option: -${OPTARG}" >&2 + echo "Run with -h for help" >&2 + exit 2 + ;; + *) + echo "INTERNAL ERROR: unknown argument: $arg" >&2 + exit 1 + ;; + esac + done + + if [[ ! -z $emulator_binary ]] ; then + export DATACONNECT_EMULATOR_BINARY_PATH="${emulator_binary}" + fi + + if [[ ! -z $postgresql_string ]] ; then + export FIREBASE_DATACONNECT_POSTGRESQL_STRING="${postgresql_string}" + fi + + if [[ $wipe_and_restart_postgres_pod == "1" ]] ; then + local wipe_args="${SCRIPT_DIR}/wipe_postgres_db.sh" + log "Running command: ${wipe_args[*]}" + "${wipe_args[@]}" + + local start_args="${SCRIPT_DIR}/start_postgres_pod.sh" + log "Running command: ${start_args[*]}" + "${start_args[@]}" + fi +} + +function print_help { + echo "Firebase Data Connect Emulator Launcher Helper" + echo + echo "This script provides a convenient way to launch the Firebase Data Connect" + echo "and Firebase Authentication emulators in a way that is amenable for running" + echo "the integration tests." + echo + echo "Syntax: ${SELF_EXECUTABLE} [options]" + echo + echo "Options:" + echo " -c " + echo " Uses the Data Connect Emulator binary at the given path. If not specified, " + echo " or if specified as the empty string, then the emulator binary is downloaded." + echo + echo " -p " + echo " Uses the given string to connect to the PostgreSQL server. If not specified " + echo " the the default value of \"${DEFAULT_POSTGRESQL_STRING}\" is used." + echo " If specified as the empty string then an ephemeral PGLite server is used." + echo + echo " -w" + echo " If specified, then a local PostgreSQL container is wiped and restarted" + echo " before launching the emulators. This is accomplished by running the scripts" + echo " ./wipe_postgres_db.sh followed by ./start_postgres_pod.sh." + echo + echo " -h" + echo " Print this help screen and exit, as if successful." + echo + echo "Environment Variables:" + echo " DATACONNECT_EMULATOR_BINARY_PATH" + echo " This variable will be set to the value of the -c argument prior to launching" + echo " the emulators. If the -c argument is not given then the value of this environment" + echo " variable will be used as if it had been specified to the -c argument." + echo + echo " FIREBASE_DATACONNECT_POSTGRESQL_STRING" + echo " This variable will be set to the value of the -p argument prior to launching" + echo " the emulators. If the -p argument is not given then the value of this environment" + echo " variable will be set to \"${DEFAULT_POSTGRESQL_STRING}\"." +} + +function log { + echo "${LOG_PREFIX}$*" +} + +main "$@" From aa367972a2b3ecb9a19f386680b6492f64f4b4c1 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Tue, 22 Jul 2025 18:52:23 +0000 Subject: [PATCH 2/2] address gemini code review feedback --- firebase-dataconnect/emulator/emulator.sh | 61 ++++++++--------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/firebase-dataconnect/emulator/emulator.sh b/firebase-dataconnect/emulator/emulator.sh index c31006f55c5..0074c175c0b 100755 --- a/firebase-dataconnect/emulator/emulator.sh +++ b/firebase-dataconnect/emulator/emulator.sh @@ -23,32 +23,25 @@ readonly DEFAULT_POSTGRESQL_STRING='postgresql://postgres:postgres@localhost:543 function main { parse_args "$@" - - log "FIREBASE_DATACONNECT_POSTGRESQL_STRING=${FIREBASE_DATACONNECT_POSTGRESQL_STRING:-}" - log "DATACONNECT_EMULATOR_BINARY_PATH=${DATACONNECT_EMULATOR_BINARY_PATH:-}" - - readonly FIREBASE_ARGS=( - firebase - --debug - emulators:start - --only auth,dataconnect - ) - - log "Running command: ${FIREBASE_ARGS[*]}" - exec "${FIREBASE_ARGS[@]}" + log "FIREBASE_DATACONNECT_POSTGRESQL_STRING=${FIREBASE_DATACONNECT_POSTGRESQL_STRING}" + log "DATACONNECT_EMULATOR_BINARY_PATH=${DATACONNECT_EMULATOR_BINARY_PATH}" + log "DATA_CONNECT_PREVIEW=${DATA_CONNECT_PREVIEW}" + run_command firebase --debug emulators:start --only auth,dataconnect } function parse_args { local emulator_binary='' local postgresql_string="${DEFAULT_POSTGRESQL_STRING}" + local preview_flags='' local wipe_and_restart_postgres_pod=0 local OPTIND=1 local OPTERR=0 - while getopts ":c:p:hw" arg ; do + while getopts ":c:p:v:hw" arg ; do case "$arg" in c) emulator_binary="${OPTARG}" ;; p) postgresql_string="${OPTARG}" ;; + v) preview_flags="${OPTARG}" ;; w) wipe_and_restart_postgres_pod=1 ;; h) print_help @@ -71,25 +64,21 @@ function parse_args { esac done - if [[ ! -z $emulator_binary ]] ; then - export DATACONNECT_EMULATOR_BINARY_PATH="${emulator_binary}" - fi - - if [[ ! -z $postgresql_string ]] ; then - export FIREBASE_DATACONNECT_POSTGRESQL_STRING="${postgresql_string}" - fi + export DATACONNECT_EMULATOR_BINARY_PATH="${emulator_binary}" + export FIREBASE_DATACONNECT_POSTGRESQL_STRING="${postgresql_string}" + export DATA_CONNECT_PREVIEW="${preview_flags}" if [[ $wipe_and_restart_postgres_pod == "1" ]] ; then - local wipe_args="${SCRIPT_DIR}/wipe_postgres_db.sh" - log "Running command: ${wipe_args[*]}" - "${wipe_args[@]}" - - local start_args="${SCRIPT_DIR}/start_postgres_pod.sh" - log "Running command: ${start_args[*]}" - "${start_args[@]}" + run_command "${SCRIPT_DIR}/wipe_postgres_db.sh" + run_command "${SCRIPT_DIR}/start_postgres_pod.sh" fi } +function run_command { + log "Running command: $*" + "$@" +} + function print_help { echo "Firebase Data Connect Emulator Launcher Helper" echo @@ -109,6 +98,11 @@ function print_help { echo " the the default value of \"${DEFAULT_POSTGRESQL_STRING}\" is used." echo " If specified as the empty string then an ephemeral PGLite server is used." echo + echo " -v " + echo " Uses the given Data Connect preview flags when launching the emulator." + echo " If not specified then an empty string is used, meaning that no preview flags" + echo " are in effect." + echo echo " -w" echo " If specified, then a local PostgreSQL container is wiped and restarted" echo " before launching the emulators. This is accomplished by running the scripts" @@ -116,17 +110,6 @@ function print_help { echo echo " -h" echo " Print this help screen and exit, as if successful." - echo - echo "Environment Variables:" - echo " DATACONNECT_EMULATOR_BINARY_PATH" - echo " This variable will be set to the value of the -c argument prior to launching" - echo " the emulators. If the -c argument is not given then the value of this environment" - echo " variable will be used as if it had been specified to the -c argument." - echo - echo " FIREBASE_DATACONNECT_POSTGRESQL_STRING" - echo " This variable will be set to the value of the -p argument prior to launching" - echo " the emulators. If the -p argument is not given then the value of this environment" - echo " variable will be set to \"${DEFAULT_POSTGRESQL_STRING}\"." } function log {