/
cmd_db.go
153 lines (143 loc) · 3.17 KB
/
cmd_db.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package service
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
func dbCommands(client *Client) []cli.Command {
return []cli.Command{
{
Name: "db",
Usage: "DB",
Hidden: true,
Subcommands: []cli.Command{
{
Name: "collections",
Usage: "List collections",
Flags: []cli.Flag{
cli.StringFlag{Name: "db", Value: "service", Usage: "db"},
},
ArgsUsage: "",
Hidden: true,
Action: func(c *cli.Context) error {
if c.NArg() > 1 {
return errors.Errorf("too many arguments")
}
path := c.Args().First()
req := &CollectionsRequest{
Parent: path,
DB: c.String("db"),
}
resp, err := client.RPCClient().Collections(context.TODO(), req)
if err != nil {
return err
}
fmtCollections(resp.Collections)
return nil
},
},
{
Name: "documents",
Usage: "List documents",
Flags: []cli.Flag{
cli.StringFlag{Name: "db", Value: "service", Usage: "db"},
},
ArgsUsage: "collection",
Hidden: true,
Subcommands: []cli.Command{
documentDeleteCommand(client),
},
Action: func(c *cli.Context) error {
if c.NArg() > 1 {
return errors.Errorf("too many arguments")
}
path := strings.TrimSpace(c.Args().First())
if path == "" {
resp, err := client.RPCClient().Collections(context.TODO(), &CollectionsRequest{
DB: c.String("db"),
})
if err != nil {
return err
}
fmtCollections(resp.Collections)
return nil
}
req := &DocumentsRequest{
Prefix: path,
DB: c.String("db"),
}
resp, err := client.RPCClient().Documents(context.TODO(), req)
if err != nil {
return err
}
fmtDocuments(resp.Documents)
return nil
},
},
},
},
}
}
func documentDeleteCommand(client *Client) cli.Command {
return cli.Command{
Name: "rm",
Usage: "Delete document",
Flags: []cli.Flag{
cli.StringFlag{Name: "db", Value: "service", Usage: "db"},
},
Hidden: true,
Action: func(c *cli.Context) error {
_, err := client.RPCClient().DocumentDelete(context.TODO(), &DocumentDeleteRequest{
Path: c.Args().First(),
DB: c.String("db"),
})
if err != nil {
return err
}
return nil
},
}
}
func fmtCollections(cols []*Collection) {
out := &bytes.Buffer{}
w := new(tabwriter.Writer)
w.Init(out, 0, 8, 1, ' ', 0)
for _, col := range cols {
fmtCollection(w, col)
}
if err := w.Flush(); err != nil {
panic(err)
}
fmt.Print(out.String())
}
func fmtCollection(w io.Writer, col *Collection) {
if col == nil {
fmt.Fprintf(w, "∅\n")
return
}
fmt.Fprintf(w, "%s\n", col.Path)
}
func fmtDocuments(docs []*Document) {
out := &bytes.Buffer{}
w := new(tabwriter.Writer)
w.Init(out, 0, 8, 1, ' ', 0)
for _, doc := range docs {
fmtDocument(w, doc)
}
if err := w.Flush(); err != nil {
panic(err)
}
fmt.Print(out.String())
}
func fmtDocument(w io.Writer, doc *Document) {
if doc == nil {
fmt.Fprintf(w, "∅\n")
return
}
fmt.Fprintf(w, "%s\t%s\n", doc.Path, doc.Value)
}