-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirestore.go
95 lines (78 loc) · 2.67 KB
/
firestore.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
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
package firestore
import (
"context"
"log"
"cloud.google.com/go/firestore"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type (
// Client wraps a Google Firestore client.
Client struct {
projectID string
credentialsFile string
context *context.Context
client *firestore.Client
}
)
const firestoreCollection = "github-auditor"
// NewClient instantiates a new Firestore client for the passed GCP project.
func NewClient(projectID string) *Client {
ctx := context.Background()
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to instantiate Firestore client in project %s: %v", projectID, err)
}
return &Client{
projectID: projectID,
context: &ctx,
client: client,
}
}
// NewClientWithCredentials instantiates a new Firestore client for the passed GCP project using the passed path to a JSON service account key file.
func NewClientWithCredentials(projectID, credentialsFile string) *Client {
ctx := context.Background()
client, err := firestore.NewClient(ctx, projectID, option.WithCredentialsFile(credentialsFile))
if err != nil {
log.Fatalf("Failed to instantiate Firestore client in project %s using credentials file %s: %v", projectID, credentialsFile, err)
}
return &Client{
projectID: projectID,
credentialsFile: credentialsFile,
context: &ctx,
client: client,
}
}
// DocExists returns whether the Firestore document with the passed ID containing the passed timestamp and action exists.
func (c Client) DocExists(id, timestamp, action string) bool {
snapshot, err := c.client.Collection(firestoreCollection).Doc(id).Get(*c.context)
if status.Code(err) == codes.NotFound {
return false
}
if snapshot == nil {
return false
}
data := snapshot.Data()
if data == nil {
return false
}
ts, err := snapshot.DataAt("timestamp")
if err != nil && status.Code(err) != codes.NotFound {
log.Fatalf("Failed to retrieve timestamp value from Firestore document %s/%s: %v", firestoreCollection, id, err)
}
a, err := snapshot.DataAt("action")
if err != nil && status.Code(err) != codes.NotFound {
log.Fatalf("Failed to retrieve action value from Firestore document %s/%s: %v", firestoreCollection, id, err)
}
return timestamp == ts && action == a
}
// SaveDoc creates or updates the Firestore document with the passed ID, setting its contents to the passed timestamp and action.
func (c Client) SaveDoc(id, timestamp, action string) error {
doc := c.client.Collection(firestoreCollection).Doc(id)
_, err := doc.Set(*c.context, map[string]interface{}{
"timestamp": timestamp,
"action": action,
})
return err
}