forked from sgarciac/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
54 lines (49 loc) · 1.45 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
48
49
50
51
52
53
54
package main
import (
firestore "cloud.google.com/go/firestore"
"context"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
"os"
)
// 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) {
var err error
var firebaseApp *firebase.App
if os.Getenv("FIRESTORE_EMULATOR_HOST") != "" {
if projectId == "" {
projectId = "default"
}
client, err := firestore.NewClient(context.Background(), projectId)
return client, err
} else if credentials != "" {
sa := option.WithCredentialsFile(credentials)
config := getConfigWithProjectId(projectId)
firebaseApp, err = firebase.NewApp(context.Background(), config, sa)
if err != nil {
return nil, err
}
return firebaseApp.Firestore(context.Background())
} else {
// This will use GOOGLE_APPLICATION_CREDENTIALS
config := getConfigWithProjectId(projectId)
firebaseApp, err = firebase.NewApp(context.Background(), config)
if err != nil {
return nil, err
}
return firebaseApp.Firestore(context.Background())
}
}
func getConfigWithProjectId(projectId string) *firebase.Config {
config := firebase.Config{}
if projectId != "" {
config.ProjectID = projectId
}
return &config
}