-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
420 lines (358 loc) · 10.9 KB
/
content.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package component
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/kopecmaciej/mongui/config"
"github.com/kopecmaciej/mongui/mongo"
"github.com/kopecmaciej/tview"
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const (
ContentComponent = "Content"
JsonViewComponent = "JsonView"
)
// Content is a component that displays documents in a table
type Content struct {
*Component
*tview.Flex
Table *tview.Table
View *tview.TextView
style *config.ContentStyle
queryBar *InputBar
jsonPeeker *DocPeeker
deleteModal *DeleteModal
docModifier *DocModifier
state mongo.CollectionState
autocompleteKeys []string
}
// NewContent creates a new Content component
// It also initializes all subcomponents
func NewContent() *Content {
state := mongo.CollectionState{
Page: 0,
Limit: 50,
}
c := &Content{
Component: NewComponent("Content"),
Table: tview.NewTable(),
Flex: tview.NewFlex(),
View: tview.NewTextView(),
queryBar: NewInputBar("Query"),
jsonPeeker: NewDocPeeker(),
deleteModal: NewDeleteModal(),
docModifier: NewDocModifier(),
state: state,
}
c.SetAfterInitFunc(c.init)
return c
}
func (c *Content) init() error {
ctx := context.Background()
c.setStyle()
c.setKeybindings(ctx)
if err := c.jsonPeeker.Init(c.app); err != nil {
return err
}
if err := c.deleteModal.Init(c.app); err != nil {
return err
}
if err := c.queryBar.Init(c.app); err != nil {
return err
}
c.queryBar.EnableAutocomplete()
c.queryBar.EnableHistory()
c.queryBar.SetDefaultText("{ <$0> }")
if err := c.docModifier.Init(c.app); err != nil {
return err
}
c.render(false)
c.queryBarListener(ctx)
return nil
}
func (c *Content) setStyle() {
c.style = &c.app.Styles.Content
c.Table.SetBorder(true)
c.Table.SetTitle(" Content ")
c.Table.SetTitleAlign(tview.AlignLeft)
c.Table.SetBorderPadding(0, 0, 1, 1)
c.Table.SetFixed(1, 1)
c.Table.SetSelectable(true, false)
c.Table.SetBackgroundColor(c.style.BackgroundColor.Color())
c.Table.SetBorderColor(c.style.BorderColor.Color())
c.Flex.SetDirection(tview.FlexRow)
}
// SetKeybindings sets keybindings for the component
func (c *Content) setKeybindings(ctx context.Context) {
k := c.app.Keys
c.Table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch {
case k.Contains(k.Root.Content.PeekDocument, event.Name()):
err := c.jsonPeeker.Peek(ctx, c.state.Db, c.state.Coll, c.Table.GetCell(c.Table.GetSelection()).Text)
if err != nil {
ShowErrorModal(c.app.Root, "Error peeking document", err)
return nil
}
return nil
case k.Contains(k.Root.Content.ViewDocument, event.Name()):
err := c.viewJson(c.Table.GetCell(c.Table.GetSelection()).Text)
if err != nil {
ShowErrorModal(c.app.Root, "Error viewing document", err)
return nil
}
return nil
case k.Contains(k.Root.Content.AddDocument, event.Name()):
ID, err := c.docModifier.Insert(ctx, c.state.Db, c.state.Coll)
if err != nil {
ShowErrorModal(c.app.Root, "Error adding document", err)
return nil
}
insertedDoc, err := c.dao.GetDocument(ctx, c.state.Db, c.state.Coll, ID)
if err != nil {
ShowErrorModal(c.app.Root, "Error getting inserted document", err)
return nil
}
strDoc, err := mongo.StringifyDocument(insertedDoc)
if err != nil {
ShowErrorModal(c.app.Root, "Error stringifying document", err)
return nil
}
c.addCell(strDoc)
return nil
case k.Contains(k.Root.Content.EditDocument, event.Name()):
updated, err := c.docModifier.Edit(ctx, c.state.Db, c.state.Coll, c.Table.GetCell(c.Table.GetSelection()).Text)
if err != nil {
defer ShowErrorModalAndFocus(c.app.Root, "Error editing document", err, func() {
c.app.SetFocus(c.Table)
})
return nil
}
c.refreshCell(updated)
return nil
case k.Contains(k.Root.Content.DuplicateDocument, event.Name()):
ID, err := c.docModifier.Duplicate(ctx, c.state.Db, c.state.Coll, c.Table.GetCell(c.Table.GetSelection()).Text)
if err != nil {
defer ShowErrorModal(c.app.Root, "Error duplicating document", err)
}
duplicatedDoc, err := c.dao.GetDocument(ctx, c.state.Db, c.state.Coll, ID)
if err != nil {
defer ShowErrorModal(c.app.Root, "Error getting inserted document", err)
}
strDoc, err := mongo.StringifyDocument(duplicatedDoc)
if err != nil {
defer ShowErrorModal(c.app.Root, "Error stringifying document", err)
}
c.addCell(strDoc)
return nil
case k.Contains(k.Root.Content.ToggleQuery, event.Name()):
c.queryBar.Toggle()
c.render(true)
return nil
case k.Contains(k.Root.Content.DeleteDocument, event.Name()):
err := c.deleteDocument(ctx, c.Table.GetCell(c.Table.GetSelection()).Text)
if err != nil {
defer ShowErrorModal(c.app.Root, "Error deleting document", err)
}
return nil
case k.Contains(k.Root.Content.Refresh, event.Name()):
err := c.refresh(ctx)
if err != nil {
defer ShowErrorModal(c.app.Root, "Error refreshing documents", err)
}
return nil
case k.Contains(k.Root.Content.NextPage, event.Name()):
c.goToNextMongoPage(ctx)
return nil
case k.Contains(k.Root.Content.PreviousPage, event.Name()):
c.goToPrevMongoPage(ctx)
return nil
}
return event
})
}
func (c *Content) render(setFocus bool) {
c.Flex.Clear()
var focusPrimitive tview.Primitive
focusPrimitive = c
if c.queryBar.IsEnabled() {
c.Flex.AddItem(c.queryBar, 3, 0, false)
focusPrimitive = c.queryBar
}
c.Flex.AddItem(c.Table, 0, 1, true)
_, _, _, height := c.Flex.GetInnerRect()
c.state.Limit = int64(height) - 4
if setFocus {
c.app.SetFocus(focusPrimitive)
}
}
func (c *Content) queryBarListener(ctx context.Context) {
accceptFunc := func(text string) {
c.Flex.RemoveItem(c.queryBar)
filter, err := mongo.ParseStringQuery(text)
if err != nil {
defer ShowErrorModal(c.app.Root, "Error parsing query", err)
}
c.RenderContent(ctx, c.state.Db, c.state.Coll, filter)
c.Table.Select(2, 0)
}
rejectFunc := func() {
c.render(true)
}
c.queryBar.DoneFuncHandler(accceptFunc, rejectFunc)
}
func (c *Content) listDocuments(ctx context.Context, db, coll string, filters map[string]interface{}) ([]string, int64, error) {
c.state.Db = db
c.state.Coll = coll
documents, count, err := c.dao.ListDocuments(ctx, db, coll, filters, c.state.Page, c.state.Limit)
if err != nil {
return nil, 0, err
}
if len(documents) == 0 {
return nil, 0, nil
}
c.state.Count = count
c.loadAutocompleteKeys(documents)
docsWithOid, err := mongo.ConvertIdsToOids(documents)
if err != nil {
return nil, 0, err
}
return docsWithOid, count, nil
}
func (c *Content) loadAutocompleteKeys(documents []primitive.M) {
uniqueKeys := make(map[string]bool)
var addKeys func(string, interface{})
addKeys = func(prefix string, value interface{}) {
switch v := value.(type) {
case map[string]interface{}:
for key, val := range v {
fullKey := key
if prefix != "" {
fullKey = prefix + "." + key
}
addKeys(fullKey, val)
}
default:
uniqueKeys[prefix] = true
}
}
for _, doc := range documents {
for key, value := range doc {
if obj, ok := value.(primitive.M); ok {
addKeys(key, obj)
for k, v := range obj {
fullKey := key + "." + k
addKeys(fullKey, v)
}
} else {
addKeys(key, value)
}
}
}
autocompleteKeys := make([]string, 0, len(uniqueKeys))
for key := range uniqueKeys {
autocompleteKeys = append(autocompleteKeys, key)
}
c.queryBar.LoadNewKeys(autocompleteKeys)
}
func (c *Content) RenderContent(ctx context.Context, db, coll string, filter map[string]interface{}) error {
c.Table.Clear()
c.app.SetFocus(c.Table)
documents, count, err := c.listDocuments(ctx, db, coll, filter)
if err != nil {
log.Error().Err(err).Msg("Error listing documents")
return err
}
headerInfo := fmt.Sprintf("Documents: %d, Page: %d, Limit: %d", count, c.state.Page, c.state.Limit)
if filter != nil {
prettyFilter, err := json.Marshal(filter)
if err != nil {
log.Error().Err(err).Msg("Error marshaling filter")
return err
}
headerInfo += fmt.Sprintf(", Filter: %v", string(prettyFilter))
}
headerCell := tview.NewTableCell(headerInfo).
SetAlign(tview.AlignLeft).
SetSelectable(false)
c.Table.SetCell(0, 0, headerCell)
if count == 0 {
// TODO: find why if selectable is set to false, program crashes
c.Table.SetCell(2, 0, tview.NewTableCell("No documents found"))
}
for i, d := range documents {
dataCell := tview.NewTableCell(d)
dataCell.SetAlign(tview.AlignLeft)
c.Table.SetCell(i+2, 0, dataCell)
c.Table.Select(2, 0)
}
// c.Table.ScrollToBeginning()
return nil
}
func (c *Content) refresh(ctx context.Context) error {
return c.RenderContent(ctx, c.state.Db, c.state.Coll, nil)
}
// addCell adds a new cell to the table
func (c *Content) addCell(content string) {
maxRow := c.Table.GetRowCount()
c.Table.SetCell(maxRow, 0, tview.NewTableCell(content).SetAlign(tview.AlignLeft))
}
// refreshCell refreshes the cell with the new content
func (c *Content) refreshCell(content string) {
// Trim the content, as in table we don't want to see new lines and spaces
content = strings.ReplaceAll(content, "\n", "")
content = strings.ReplaceAll(content, " ", "")
row, col := c.Table.GetSelection()
c.Table.SetCell(row, col, tview.NewTableCell(content).SetAlign(tview.AlignLeft))
}
func (c *Content) goToNextMongoPage(ctx context.Context) {
if c.state.Page+c.state.Limit >= c.state.Count {
return
}
c.state.Page += c.state.Limit
c.RenderContent(ctx, c.state.Db, c.state.Coll, nil)
}
func (c *Content) goToPrevMongoPage(ctx context.Context) {
if c.state.Page == 0 {
return
}
c.state.Page -= c.state.Limit
c.RenderContent(ctx, c.state.Db, c.state.Coll, nil)
}
func (c *Content) viewJson(jsonString string) error {
c.View.Clear()
c.app.Root.AddPage(JsonViewComponent, c.View, true, true)
indentedJson, err := mongo.IndientJSON(jsonString)
if err != nil {
return err
}
c.View.SetText(string(indentedJson.Bytes()))
c.View.ScrollToBeginning()
c.View.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEsc:
c.app.Root.RemovePage(JsonViewComponent)
c.app.SetFocus(c.Table)
}
return event
})
return nil
}
func (c *Content) deleteDocument(ctx context.Context, jsonString string) error {
objectID, err := mongo.GetIDFromJSON(jsonString)
c.deleteModal.SetText("Are you sure you want to delete document of ID: [blue]" + objectID.Hex())
c.deleteModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonIndex == 0 {
err = c.dao.DeleteDocument(ctx, c.state.Db, c.state.Coll, objectID)
if err != nil {
defer ShowErrorModal(c.app.Root, "Error deleting document", err)
}
}
c.app.Root.RemovePage(c.deleteModal.GetIdentifier())
c.RenderContent(ctx, c.state.Db, c.state.Coll, nil)
})
c.app.Root.AddPage(c.deleteModal.GetIdentifier(), c.deleteModal, true, true)
return nil
}