-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.go
47 lines (38 loc) · 1.12 KB
/
client.go
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
package main
import (
"context"
"os"
firestore "cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
// create client or fails.
//
// NOTE: If FIRESTORE_EMULATOR_HOST is set, it will set a
// default projectid if none has been set.
func createClient(credentials string) (*firestore.Client, error) {
return createClientWithProjectId(credentials, projectId)
}
func createClientWithProjectId(credentials string, projectId string) (*firestore.Client, error) {
if os.Getenv("FIRESTORE_EMULATOR_HOST") != "" {
if projectId == "" {
projectId = "default"
}
return firestore.NewClient(context.Background(), projectId)
}
if database == "" {
database = firestore.DefaultDatabaseID
}
options := make([]option.ClientOption, 0)
if credentials != "" {
options = append(options, option.WithCredentialsFile(credentials))
}
return firestore.NewClientWithDatabase(context.Background(), projectId, database, options...)
}
func getConfigWithProjectId(projectId string) *firebase.Config {
config := firebase.Config{}
if projectId != "" {
config.ProjectID = projectId
}
return &config
}