allow terminal control back after bubbletea UI (#4237)

terminal control was not relinquished since ui.Start()
must not be run in a go-routine as it is expected
to be a blocking call, and must be serialized with when
an application exits.

refactor the 'scanner info/status' to ensure we have
correct behavior.

This PR also renames 'info-> status', keeps the older
one as alias for now.
This commit is contained in:
Harshavardhana
2022-09-08 08:18:07 -07:00
committed by GitHub
parent f73e036b80
commit 30a9bb8b05
3 changed files with 32 additions and 34 deletions
@@ -62,7 +62,8 @@ var adminScannerInfoFlags = []cli.Flag{
}
var adminScannerInfo = cli.Command{
Name: "info",
Name: "status",
Aliases: []string{"info"},
Usage: "summarize scanner events on MinIO server in real-time",
Action: mainAdminScannerInfo,
OnUsageError: onUsageError,
@@ -87,7 +88,7 @@ EXAMPLES:
// checkAdminTopAPISyntax - validate all the passed arguments
func checkAdminScannerInfoSyntax(ctx *cli.Context) {
if len(ctx.Args()) == 0 || len(ctx.Args()) > 1 {
cli.ShowCommandHelpAndExit(ctx, "info", 1) // last argument is exit code
cli.ShowCommandHelpAndExit(ctx, ctx.Command.Name, 1) // last argument is exit code
}
}
@@ -111,26 +112,25 @@ func mainAdminScannerInfo(ctx *cli.Context) error {
ByHost: false,
}
ui := tea.NewProgram(initScannerMetricsUI(ctx.Int("max-paths")))
if !globalJSON {
go func() {
if e := ui.Start(); e != nil {
cancel()
os.Exit(1)
go func() {
e := client.Metrics(ctxt, opts, func(metrics madmin.RealtimeMetrics) {
if globalJSON {
printMsg(metricsMessage{RealtimeMetrics: metrics})
return
}
os.Exit(0)
}()
}
e := client.Metrics(ctxt, opts, func(metrics madmin.RealtimeMetrics) {
if globalJSON {
printMsg(metricsMessage{RealtimeMetrics: metrics})
return
}
ui.Send(metrics)
})
ui.Send(metrics)
})
if e != nil && !errors.Is(e, context.Canceled) {
fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to fetch scanner metrics")
return nil
if e != nil && !errors.Is(e, context.Canceled) {
fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to fetch scanner metrics")
}
}()
if !globalJSON {
if e := ui.Start(); e != nil {
cancel()
os.Exit(1)
}
}
return nil
@@ -184,10 +184,13 @@ func (m *scannerMetricsUI) Init() tea.Cmd {
}
func (m *scannerMetricsUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.quitting {
return m, tea.Quit
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
default:
@@ -200,14 +203,11 @@ func (m *scannerMetricsUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
return m, nil
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
default:
return m, nil
}
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
func (m *scannerMetricsUI) View() string {
+2 -2
View File
@@ -333,8 +333,8 @@ var completeCmds = map[string]complete.Predictor{
"/admin/top/locks": aliasCompleter,
"/admin/top/api": aliasCompleter,
"/admin/scanner/info": aliasCompleter,
"/admin/scanner/trace": aliasCompleter,
"/admin/scanner/status": aliasCompleter,
"/admin/scanner/trace": aliasCompleter,
"/admin/service/stop": aliasCompleter,
"/admin/service/restart": aliasCompleter,
+2 -4
View File
@@ -86,7 +86,7 @@ func (m *speedTestUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
default:
@@ -99,12 +99,10 @@ func (m *speedTestUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
return m, nil
case spinner.TickMsg:
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
default:
return m, nil
}
}