-
Notifications
You must be signed in to change notification settings - Fork 28
/
getall.go
63 lines (47 loc) · 1.32 KB
/
getall.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
package main
import (
firestore "cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
)
func getDocuments(client *firestore.Client,
collectionPath string,
ids []string,
) ([]*firestore.DocumentSnapshot, error) {
collectionRef := client.Collection(collectionPath)
var docRefs []*firestore.DocumentRef
for _, elem := range ids {
docRefs = append(docRefs, collectionRef.Doc(elem))
}
return client.GetAll(context.Background(), docRefs)
}
func getAllCommandAction(c *cli.Context) error {
argsLength := len(c.Args())
if argsLength < 2 {
return cli.NewExitError("Wrong number of arguments", 82)
}
extendedJson := c.Bool("extendedjson")
var collectionPath string
var ids []string
collectionPath = c.Args().First()
ids = c.Args()[1:]
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
data, err := getDocuments(client, collectionPath, ids)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error fetching documents. \n%v", err), 86)
}
displayItemWriter := newDisplayItemWriter(&c.App.Writer)
defer displayItemWriter.Close()
for _, doc := range data {
err = displayItemWriter.Write(doc, extendedJson)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error while writing output. \n%v", err), 86)
}
}
defer client.Close()
return nil
}