frostfs-node/cmd/frostfs-lens/internal/meta/tui.go
Aleksey Savchuk ff28b7e592
[#1223] lens/tui: Add help for tui command in lens CLI
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2024-08-16 11:33:31 +03:00

98 lines
2.1 KiB
Go

package meta
import (
"context"
"fmt"
common "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-lens/internal"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-lens/internal/tuiutil"
"github.com/rivo/tview"
"github.com/spf13/cobra"
"go.etcd.io/bbolt"
)
var tuiCMD = &cobra.Command{
Use: "tui",
Short: "Metabase TUI",
Long: "Launch a TUI to browse metabase and search for data",
Run: tuiFunc,
}
var (
initialFilter string
needListFilters bool
)
func init() {
common.AddComponentPathFlag(tuiCMD, &vPath)
tuiCMD.Flags().StringVar(
&initialFilter,
"filter",
"",
"Initial filter to start with, format <tag>:<args...>",
)
tuiCMD.Flags().BoolVar(
&needListFilters,
"list-filters",
false,
"List all available filters",
)
}
func tuiFunc(cmd *cobra.Command, _ []string) {
common.ExitOnErr(cmd, runTUI(cmd))
}
func runTUI(cmd *cobra.Command) error {
// tview.Styles.PrimaryTextColor = tcell.ColorBlack
// tview.Styles.PrimitiveBackgroundColor = tcell.ColorWhite
// tview.Styles.GraphicsColor = tcell.ColorBlack
if needListFilters {
cmd.Println("tag\targs")
cmd.Println("---\t----")
cmd.Println("cid\t<CID>")
cmd.Println("oid\t<OID>")
cmd.Println("addr\t<CID>/<OID>")
cmd.Println("attr\t<key> or <key>/<value>")
return nil
}
db, err := openDB(false)
common.ExitOnErr(cmd, err)
defer db.Close()
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()
app := tview.NewApplication()
ui := tuiutil.NewUI(ctx, app, db)
_ = ui.AddFilter("cid", cidParser, "<CID>")
_ = ui.AddFilter("oid", oidParser, "<OID>")
_ = ui.AddCompositeFilter("addr", addrParser, "<CID>/<OID>")
_ = ui.AddCompositeFilter("attr", attributeParser, "<key> or <key>/<value>")
err = ui.WithPrompt(initialFilter)
if err != nil {
return fmt.Errorf("invalid filter prompt: %w", err)
}
app.SetRoot(ui, true).SetFocus(ui)
return app.Run()
}
func openDB(writable bool) (*bbolt.DB, error) {
db, err := bbolt.Open(vPath, 0o600, &bbolt.Options{
ReadOnly: !writable,
})
if err != nil {
return nil, fmt.Errorf("couldn't open database: %w", err)
}
return db, nil
}