frostfs-node/cmd/frostfs-lens/internal/tuiutil/app.go
Aleksey Savchuk ec66b4cee2
[#1223] lens/tui: Add search by CID and OID
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2024-08-16 11:33:31 +03:00

152 lines
3.2 KiB
Go

package tuiutil
import (
"context"
"fmt"
handlers "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-lens/schema"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
"github.com/davecgh/go-spew/spew"
"github.com/gdamore/tcell/v2"
"github.com/mr-tron/base58"
"github.com/rivo/tview"
"go.etcd.io/bbolt"
)
// 29NkY3QtH6r8Hd9RBejwtUuju1ViAQ6jZPR7pjuTnkoH
// 1QVJo3LCACFnbeqJ7uHz97PyoPrkZr4BnZ9cVYAhRPx
// 6E6b24qy32p3L3wjRSUEGNFHMxkUsaqj7udwWVKLzkU
// 1GcnNjFnPof2YbPi2RBi3Sjy3qeR2cymc9BR6i9U2Kt
type Bucket struct {
name []byte
path [][]byte
nextHandler handlers.Handler
result *handlers.KV
}
type Record struct {
key []byte
value []byte
path [][]byte
nextHandler handlers.Handler
result *handlers.KV
}
type Application struct {
app *tview.Application
nav *Navigation
db *bbolt.DB
}
func NewApplication(ctx context.Context, db *bbolt.DB) (*Application, error) {
spew.Config.DisableMethods = true
a := &Application{
db: db,
}
a.app = tview.NewApplication()
a.nav = NewNavigation(a.app)
a.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEsc {
a.app.Stop()
return nil
}
return event
})
err := a.nav.Push(ctx, a.newIntoView)
if err != nil {
return nil, err
}
return a, nil
}
func (a *Application) Run() error {
return a.app.Run()
}
func (a *Application) stopOnErr(err error) {
if err != nil {
a.app.QueueEvent(tcell.NewEventError(err))
}
}
func (a *Application) newIntoView(ctx context.Context) (tview.Primitive, error) {
form := tview.NewForm()
unsetErrMsg := func() {
if form.GetFormItemCount() >= 4 {
form.RemoveFormItem(3)
}
}
setErrMsg := func(msg string) {
form.AddTextView("", fmt.Sprintf("[red]%s[white]", msg), 45, 1, true, false)
}
var cidText, oidText string
form.
AddInputField("CID", "", 45, nil, func(text string) { cidText = text }).
AddInputField("OID", "", 45, nil, func(text string) { oidText = text }).
AddButton("Search", func() {
unsetErrMsg()
cidVal := &cid.ID{}
data, err := base58.Decode(cidText)
if err != nil {
setErrMsg("not a valid CID")
return
}
err = cidVal.Decode(data)
if err != nil {
setErrMsg("not a valid CID")
return
}
oidVal := &oid.ID{}
data, err = base58.Decode(oidText)
if err != nil {
setErrMsg("not a valid OID")
return
}
err = oidVal.Decode(data)
if err != nil {
setErrMsg("not a valid OID")
return
}
err = a.nav.Push(ctx, func(ctx context.Context) (tview.Primitive, error) {
return a.newBucketsView(ctx, cidVal, oidVal)
})
a.stopOnErr(err)
}).
AddButton("Continue w/o search", func() {
err := a.nav.Push(ctx, func(ctx context.Context) (tview.Primitive, error) {
return a.newBucketsView(ctx, nil, nil)
})
a.stopOnErr(err)
})
form.SetBorder(true)
view := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(tview.NewBox(), 0, 1, false).
AddItem(
tview.NewFlex().
AddItem(tview.NewBox(), 0, 1, false).
AddItem(form, 54, 0, true).
AddItem(tview.NewBox(), 0, 1, false),
13, 0, true,
).
AddItem(tview.NewBox(), 0, 1, false)
return view, nil
}