-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdDb.go
91 lines (72 loc) · 1.53 KB
/
cmdDb.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
package firebasecli
import (
"context"
"encoding/json"
"strings"
docopt "github.com/docopt/docopt-go"
)
// Db manipulates Cloud Firestore.
func (c *Cmd) Db(args ...string) error {
opts, err := docopt.ParseArgs(`
Manipulate Cloud Firestore.
Usage:
firebasecli db list
firebasecli db export COLLECTIONS... [-f]
Options:
list Show the all collection paths.
Options:
export COLLECTIONS Print the all documents in given collections as JSON.
-f, --format Output as formatted JSON.
`, args, "")
if err != nil {
// TODO add an error explanation.
return err
}
var arg struct {
Db bool
List bool
Export bool
Collections []string
Format bool
}
if err := opts.Bind(&arg); err != nil {
// TODO add an error explanation.
return err
}
switch {
case arg.List:
return c.dbList()
case arg.Export:
return c.dbExport(arg.Collections, arg.Format)
default:
return ErrUnknownCommand
}
}
func (c *Cmd) dbList() error {
ctx := context.Background()
collections, err := c.App.DbList(ctx)
if err != nil {
return err
}
c.Println(strings.Join(collections, "\n"))
return nil
}
func (c *Cmd) dbExport(collections []string, format bool) error {
ctx := context.Background()
data, err := c.App.DbExport(ctx, collections)
if err != nil {
return err
}
var jsonData []byte
if format {
jsonData, err = json.MarshalIndent(data, "", " ")
} else {
jsonData, err = json.Marshal(data)
}
if err != nil {
// TODO add an error explanation.
return err
}
c.Println(string(jsonData))
return nil
}