forked from sgarciac/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
49 lines (40 loc) · 908 Bytes
/
add.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
package main
import (
firestore "cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
)
func addData(
client *firestore.Client,
collection string,
data string,
) (string, error) {
object, err := unmarshallData(data)
if err != nil {
return "", err
}
transformExtendedJsonMapToFirestoreMap(object, client)
doc, _, err := client.
Collection(collection).
Add(context.Background(), object)
if err != nil {
return "", err
}
return doc.ID, nil
}
func addCommandAction(c *cli.Context) error {
collectionPath := c.Args().First()
data := c.Args().Get(1)
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
id, err := addData(client, collectionPath, data)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to add data. \n%v", err), 81)
}
fmt.Fprintf(c.App.Writer, "%v\n", id)
defer client.Close()
return nil
}