mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2026-05-17 00:26:36 +03:00
Previously, context cancellation was ignored when reading user response for the prompt. That leads to ignoring of "Ctrl+C" and other termination signals to vmctl until user finishes the input. Fix that by properly propagating the context and respecting the cancellation of the context. Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/terminal"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/vm"
|
|
)
|
|
|
|
const barTpl = `{{ blue "%s:" }} {{ counters . }} {{ bar . "[" "█" (cycle . "█") "▒" "]" }} {{ percent . }}`
|
|
|
|
// isSilent should be inited in main
|
|
var isSilent bool
|
|
|
|
func prompt(ctx context.Context, question string) bool {
|
|
if isSilent {
|
|
return true
|
|
}
|
|
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
|
|
if !isTerminal {
|
|
return true
|
|
}
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Print(question, " [Y/n] ")
|
|
|
|
answerCh := make(chan string, 1)
|
|
errCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
answer, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
errCh <- err
|
|
return
|
|
}
|
|
answerCh <- answer
|
|
}()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
fmt.Println("\nCanceled.")
|
|
return false
|
|
case err := <-errCh:
|
|
panic(err)
|
|
case answer := <-answerCh:
|
|
answer = strings.TrimSpace(strings.ToLower(answer))
|
|
if answer == "" || answer == "yes" || answer == "y" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
func wrapErr(vmErr *vm.ImportError, verbose bool) error {
|
|
var errTS string
|
|
var maxTS, minTS int64
|
|
for _, ts := range vmErr.Batch {
|
|
if minTS < ts.Timestamps[0] || minTS == 0 {
|
|
minTS = ts.Timestamps[0]
|
|
}
|
|
if maxTS < ts.Timestamps[len(ts.Timestamps)-1] {
|
|
maxTS = ts.Timestamps[len(ts.Timestamps)-1]
|
|
}
|
|
if verbose {
|
|
errTS += fmt.Sprintf("%s for timestamps range %d - %d\n",
|
|
ts.String(), ts.Timestamps[0], ts.Timestamps[len(ts.Timestamps)-1])
|
|
}
|
|
}
|
|
var verboseMsg string
|
|
if !verbose {
|
|
verboseMsg = "(enable `--verbose` output to get more details)"
|
|
}
|
|
if vmErr.Err == nil {
|
|
return fmt.Errorf("%s\n\tLatest delivered batch for timestamps range %d - %d %s\n%s",
|
|
vmErr.Err, minTS, maxTS, verboseMsg, errTS)
|
|
}
|
|
return fmt.Errorf("%s\n\tImporting batch failed for timestamps range %d - %d %s\n%s",
|
|
vmErr.Err, minTS, maxTS, verboseMsg, errTS)
|
|
}
|