-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
executable file
·323 lines (281 loc) · 7.52 KB
/
app.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
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/BurntSushi/toml"
ui "github.com/gizak/termui/v3"
"github.com/go-redis/redis/v8"
"github.com/milonoir/rv/common"
"github.com/milonoir/rv/logger"
r "github.com/milonoir/rv/redis"
"github.com/milonoir/rv/scanner"
)
const (
scannerUsage = ` [<Up>](fg:yellow)/[<Down>](fg:yellow) move selection up/down [<Enter>](fg:yellow) select [<m>](fg:yellow) view messages
[<PgUp>](fg:yellow)/[<PgDown>](fg:yellow) scroll up/down [<e>](fg:yellow) enable scanner
[<Home>](fg:yellow)/[<End>](fg:yellow) move to top/bottom [<d>](fg:yellow) disable scanner [<q>](fg:yellow) quit`
selectorUsage = ` [<Up>](fg:yellow)/[<Down>](fg:yellow) move selection up/down [<Enter>](fg:yellow) select
[<PgUp>](fg:yellow)/[<PgDown>](fg:yellow) scroll up/down [<Esc>](fg:yellow) go back
[<Home>](fg:yellow)/[<End>](fg:yellow) move to top/bottom [<q>](fg:yellow) quit`
viewerUsage = ` [<Up>](fg:yellow)/[<Down>](fg:yellow) move selection up/down
[<PgUp>](fg:yellow)/[<PgDown>](fg:yellow) scroll up/down [<Esc>](fg:yellow) go back
[<Home>](fg:yellow)/[<End>](fg:yellow) move to top/bottom [<q>](fg:yellow) quit`
messagesUsage = `[<Esc>](fg:yellow) go back
[<q>](fg:yellow) quit`
)
var (
updateInterval = 100 * time.Millisecond
viewerTimeout = 3 * time.Second
)
// config represents the application configuration.
type config struct {
Redis *r.Config
Scans map[string]*scanner.Config
}
// app represents the main application.
type app struct {
cfg *config
rc *redis.Client
scanner scanner.Scanner
selector scanner.Selector
viewer scanner.Viewer
helper common.TextBox
messages common.TextBox
logger logger.Logger
messagesVisible bool
selectorVisible bool
viewerVisible bool
msgCh chan string
}
// newApp creates and configures a new app.
func newApp(cfgFile string) (*app, error) {
f, err := common.LoadFile(cfgFile)
if err != nil {
return nil, fmt.Errorf("load config file: %w", err)
}
cfg := &config{}
if _, err = toml.Decode(string(f), cfg); err != nil {
return nil, fmt.Errorf("parse toml config: %w", err)
}
return &app{
cfg: cfg,
}, nil
}
// setup configures and initializes the components of the app.
func (a *app) setup() error {
if err := a.setupRedis(); err != nil {
return fmt.Errorf("setup Redis: %w", err)
}
if err := a.initUI(); err != nil {
return fmt.Errorf("init termui: %w", err)
}
return nil
}
// setupRedis configures the Redis client and tests its connection to the Redis server.
func (a *app) setupRedis() error {
a.rc = redis.NewClient(&redis.Options{
Addr: a.cfg.Redis.Server,
Password: a.cfg.Redis.Password,
DB: a.cfg.Redis.DB,
DialTimeout: a.cfg.Redis.DialTimeout.Duration,
IdleTimeout: a.cfg.Redis.IdleTimeout.Duration,
ReadTimeout: a.cfg.Redis.ReadTimeout.Duration,
WriteTimeout: a.cfg.Redis.WriteTimeout.Duration,
MaxRetries: a.cfg.Redis.MaxRetries,
})
// Test connection.
reply, err := a.rc.Do(context.Background(), "PING").Text()
if err != nil {
return fmt.Errorf("test Redis connection ping: %w", err)
}
if reply != "PONG" {
return fmt.Errorf("unexpected response from Redis: %s != PONG", reply)
}
return nil
}
// initUI initializes the termui.
func (a *app) initUI() error {
return ui.Init()
}
// initWidgets initializes the widgets.
func (a *app) initWidgets(ctx context.Context) {
a.msgCh = make(chan string, 1)
// Scanner widget
a.scanner = scanner.NewScanner(ctx, a.rc, a.cfg.Scans)
// Selector widget
a.selector = scanner.NewSelector()
// Viewer widget
a.viewer = scanner.NewViewer(a.rc)
// Helper widget
a.helper = common.NewTextBox(" Help ")
a.helper.SetText(scannerUsage)
// Logger widget
a.logger = logger.NewLogger(ctx, a.msgCh, a.scanner.Messages(), a.viewer.Messages())
// Messages widget
a.messages = common.NewTextBox(" Messages ")
a.resize(ui.TerminalDimensions())
}
// run is the main event loop of the application.
func (a *app) run() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
a.initWidgets(ctx)
t := time.NewTicker(updateInterval)
defer t.Stop()
uiEvents := ui.PollEvents()
for {
select {
case <-t.C:
a.update()
case e := <-uiEvents:
switch e.ID {
case "<Resize>":
payload := e.Payload.(ui.Resize)
a.resize(payload.Width, payload.Height)
case "q", "<C-c>":
a.handleQuit()
return
}
// Dispatching events to appropriate handlers.
switch {
case a.viewerVisible:
a.handleViewerEvents(e)
case a.selectorVisible:
a.handleSelectorEvents(ctx, e)
case a.messagesVisible:
a.handleMessagesEvents(e)
default:
a.handleScannerEvents(e)
}
}
}
}
func (a *app) handleScannerEvents(e ui.Event) {
switch e.ID {
case "<Up>":
a.scanner.ScrollUp()
case "<Down>":
a.scanner.ScrollDown()
case "<PageUp>":
a.scanner.ScrollPageUp()
case "<PageDown>":
a.scanner.ScrollPageDown()
case "<Home>":
a.scanner.ScrollTop()
case "<End>":
a.scanner.ScrollBottom()
case "<Enter>":
items, rt := a.scanner.Select()
switch {
case items == nil:
a.msgCh <- fmt.Sprintf("Error in selection")
case len(items) == 0:
a.msgCh <- fmt.Sprintf("No matching keys")
default:
a.selector.SetItems(items, rt)
a.helper.SetText(selectorUsage)
a.selectorVisible = true
}
case "e":
a.scanner.Enable()
case "d":
a.scanner.Disable()
case "m":
a.messages.SetText(strings.Join(a.logger.Messages(), "\n"))
a.helper.SetText(messagesUsage)
a.messagesVisible = true
}
}
func (a *app) handleSelectorEvents(ctx context.Context, e ui.Event) {
switch e.ID {
case "<Up>":
a.selector.ScrollUp()
case "<Down>":
a.selector.ScrollDown()
case "<PageUp>":
a.selector.ScrollPageUp()
case "<PageDown>":
a.selector.ScrollPageDown()
case "<Home>":
a.selector.ScrollTop()
case "<End>":
a.selector.ScrollBottom()
case "<Enter>":
c, cancel := context.WithTimeout(ctx, viewerTimeout)
defer cancel()
key, rt := a.selector.Select()
a.viewer.View(c, key, rt)
a.helper.SetText(viewerUsage)
a.selectorVisible = false
a.viewerVisible = true
case "<Escape>":
a.selectorVisible = false
a.helper.SetText(scannerUsage)
}
}
func (a *app) handleViewerEvents(e ui.Event) {
switch e.ID {
case "<Escape>":
a.viewerVisible = false
a.selectorVisible = true
a.helper.SetText(selectorUsage)
case "<Up>":
a.viewer.ScrollUp()
case "<Down>":
a.viewer.ScrollDown()
case "<PageUp>":
a.viewer.ScrollPageUp()
case "<PageDown>":
a.viewer.ScrollPageDown()
case "<Home>":
a.viewer.ScrollTop()
case "<End>":
a.viewer.ScrollBottom()
}
}
func (a *app) handleMessagesEvents(e ui.Event) {
switch e.ID {
case "<Escape>":
a.messagesVisible = false
a.helper.SetText(scannerUsage)
}
}
// update invokes the Update() method on each widget.
func (a *app) update() {
a.helper.Update()
a.logger.Update()
switch {
case a.viewerVisible:
a.viewer.Update()
case a.selectorVisible:
a.selector.Update()
case a.messagesVisible:
a.messages.Update()
default:
a.scanner.Update()
}
}
// resize resizes all widgets.
func (a *app) resize(w, h int) {
a.scanner.Resize(0, 0, w, h-5)
a.selector.Resize(0, 0, w, h-5)
a.viewer.Resize(0, 0, w, h-5)
a.messages.Resize(0, 0, w, h-5)
a.helper.Resize(0, h-5, w/2, h)
a.logger.Resize(w/2, h-5, w, h)
ui.Clear()
}
// handleQuit invokes the Close() method on each widget and closes termui.
func (a *app) handleQuit() {
close(a.msgCh)
a.messages.Close()
a.viewer.Close()
a.selector.Close()
a.scanner.Close()
a.helper.Close()
a.logger.Close()
ui.Clear()
ui.Close()
}