-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbash_firebase
More file actions
129 lines (104 loc) · 3.58 KB
/
bash_firebase
File metadata and controls
129 lines (104 loc) · 3.58 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# Most of the below utilites depends on the "jq" tool for parsing the
# See https://stedolan.github.io/jq/ for installing jq tool.
# Configure the below Firebase project related settings.
# Web API Key of the Firebase project
export FIREBASE_APP_KEY=
# Firebase project id
export FIREBASE_PROJECT_ID=
# Firebase project user credentials
export FIREBASE_APP_EMAIL=
export FIREBASE_APP_PASSWORD=
export FIREBASE_AUTH_URL="https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword"
export FIREBASE_AUTH_TOKEN_DB="/tmp/.firebase_get_auth_token.db"
export FIRESTORE_DEFAULT_DATABASE="(default)"
export FIRESTORE_API_URL="https://firestore.googleapis.com"
firestore_get_fields_from_collection() {
local OPTIND OPTARG choice collection_id select_field select_type where_field where_value where_type database reponse
while getopts 'd:c:s:w:v:t:h' choice
do
case $choice in
d) database=$OPTARG ;;
c) collection_id=$OPTARG ;;
s) select_field=$OPTARG ;;
g) select_type=$OPTARG ;;
w) where_field=$OPTARG ;;
v) where_value=$OPTARG ;;
t) where_type=$OPTARG ;;
h) echo "Usage: firestore_get_fields_from_collection [ -d database ] -c collection_id -s select_field -g select_type -w where_field -v where_value [-t where_type] [-h]"; return
esac
done
if [[ -z "$collection_id" || -z "$select_field" || -z "$where_field" || -z "$where_value" ]]; then
firestore_get_fields_from_collection -h
return
fi
database=${database:-$FIRESTORE_DEFAULT_DATABASE}
where_type=${where_type:-"stringValue"}
select_type=${select_type:-"stringValue"}
if ! [ -e $FIREBASE_AUTH_TOKEN_DB ]; then
_firebase_get_auth_token > $FIREBASE_AUTH_TOKEN_DB
fi
token=`cat $FIREBASE_AUTH_TOKEN_DB`
response=`_firestore_run_query $collection_id $select_field $where_field $where_value $token $where_type $database`
response_code=`echo "$response" | awk 'NR==1{print $2}'`
if [ $response_code == "401" ]; then #Unautohrized
_firebase_get_auth_token > $FIREBASE_AUTH_TOKEN_DB
token=`cat $FIREBASE_AUTH_TOKEN_DB`
response=`_firestore_run_query $collection_id $select_field $where_field $where_value $token $where_type $database`
response_code=`echo "$response" | awk 'NR==1{print $2}'`
fi
if [ $response_code == "200" ]; then
echo "$response" | tr -d '\r' | awk -v RS= 'END{print $0}' | jq -r ".[].document.fields.$select_field.$select_type"
else
echo "Something went wrong, reponse = $response"
fi
}
_firestore_run_query() {
local collection_id select_field where_field where_value where_type
collection_id=$1
select_field=$2
where_field=$3
where_value=$4
token=$5
where_type=$6
database=$7
curl -s -i -X POST "$FIRESTORE_API_URL/v1/projects/$FIREBASE_PROJECT_ID/databases/$database/documents:runQuery" \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "
{
\"structuredQuery\": {
\"select\": {
\"fields\": [
{
\"fieldPath\": \"$select_field\"
}
]
},
\"from\": [
{
\"collectionId\": \"$collection_id\",
\"allDescendants\": false
}
],
\"where\": {
\"fieldFilter\": {
\"field\": {
\"fieldPath\": \"$where_field\"
},
\"op\": \"EQUAL\",
\"value\": {
\"$where_type\": \"$where_value\"
}
}
}
}
}"
}
_firebase_get_auth_token() {
curl -s -X POST "$FIREBASE_AUTH_URL?key=$FIREBASE_APP_KEY" -H 'Content-Type: application/json' \
-d "{
\"email\": \"$FIREBASE_APP_EMAIL\",
\"password\": \"$FIREBASE_APP_PASSWORD\",
\"returnSecureToken\": true}" | jq -r '.idToken'
}