forked from sgarciac/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
62 lines (47 loc) · 1.23 KB
/
get.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
package main
import (
firestore "cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
)
func getData(
client *firestore.Client,
collectionPath string,
documentPath string,
id string,
) (*firestore.DocumentSnapshot, error) {
var documentRef *firestore.DocumentRef
if collectionPath != "" {
collectionRef := client.Collection(collectionPath)
documentRef = collectionRef.Doc(id)
} else {
documentRef = client.Doc(documentPath)
}
return documentRef.Get(context.Background())
}
func getCommandAction(c *cli.Context) error {
argsLength := len(c.Args())
if argsLength < 1 || argsLength > 2 {
return cli.NewExitError("Wrong number of arguments", 82)
}
extendedJson := c.Bool("extendedjson")
var collectionPath, documentPath, id string
if argsLength == 2 {
collectionPath = c.Args().First()
id = c.Args().Get(1)
} else {
documentPath = c.Args().First()
}
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
defer client.Close()
docsnap, err := getData(client, collectionPath, documentPath, id)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to get data. \n%v", err), 82)
}
writeSnapshot(c.App.Writer, docsnap, extendedJson)
return nil
}