who_need_help/scripts/import-firebase-android-config.sh

69 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
set -eu
ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
if [ "$#" -ne 2 ]; then
echo "Usage: $0 ENV_FILE GOOGLE_SERVICES_JSON" >&2
exit 1
fi
env_file=$1
client_file=$2
if [ ! -f "$client_file" ]; then
echo "Firebase Android configuration does not exist: $client_file" >&2
exit 1
fi
package_name=$(
awk -F= '
$1 == "ANDROID_APP_LINKS_PACKAGE_NAME" {
print substr($0, index($0, "=") + 1)
exit
}
' "$env_file"
)
if [ -z "$package_name" ]; then
echo "ANDROID_APP_LINKS_PACKAGE_NAME is missing from the selected environment." >&2
exit 1
fi
if ! jq --exit-status --arg package "$package_name" '
(.project_info.project_number) as $project_number
|
[
.client[]?
| select(.client_info.android_client_info.package_name == $package)
] as $clients
| ($clients | length == 1)
and (.project_info.project_id | type == "string" and length > 0)
and (.project_info.project_number | type == "string" and length > 0)
and ($clients[0].client_info.mobilesdk_app_id | type == "string" and length > 0)
and ($clients[0].client_info.mobilesdk_app_id
| startswith("1:" + $project_number + ":android:"))
and ($clients[0].api_key[0].current_key | type == "string" and length > 0)
' "$client_file" >/dev/null; then
echo "Firebase Android configuration does not contain exactly one complete client for package $package_name." >&2
exit 1
fi
values_file=$(mktemp "${TMPDIR:-/tmp}/wnh-firebase-android-values.XXXXXX")
trap 'rm -f "$values_file"' EXIT HUP INT TERM
chmod 600 "$values_file"
jq --raw-output --arg package "$package_name" '
(
.client[]
| select(.client_info.android_client_info.package_name == $package)
) as $client
| "WNH_FIREBASE_APPLICATION_ID=\($client.client_info.mobilesdk_app_id)",
"WNH_FIREBASE_API_KEY=\($client.api_key[0].current_key)",
"WNH_FIREBASE_PROJECT_ID=\(.project_info.project_id)",
"WNH_FIREBASE_GCM_SENDER_ID=\(.project_info.project_number)"
' "$client_file" >"$values_file"
"$ROOT/scripts/set-env-values.sh" "$env_file" "$values_file" >/dev/null
echo "Public Firebase Android values imported for package $package_name."