-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
50 lines (43 loc) · 1021 Bytes
/
read.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
package cmd
import (
"fmt"
fc "github.com/caliagaa/ripfire/cmd/firebase_client"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
"log"
)
// readCmd represents the read command
var readCmd = &cobra.Command{
Use: "read",
Short: "Read Firestore collection",
Long: `Read Firestore collection`,
Run: func(cmd *cobra.Command, args []string) {
readCollection(collectionName)
},
}
func init() {
rootCmd.AddCommand(readCmd)
readCmd.Flags().StringVarP(&collectionName, "collection", "c", "", "Collection to read")
}
func readCollection(collectionName string) {
ctx := context.Background()
client := fc.GetClient(ctx)
defer client.Close()
iter := client.Collection(collectionName).Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
print(doc.Data())
}
}
func print(m map[string]interface{}) {
for key, element := range m {
fmt.Println(key, ":",element)
}
}