From ecde8c38a5234d126bff974af1204b355a1a79f4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 22 Mar 2025 14:10:52 +0100 Subject: [PATCH] internal/prompt: skip fmt.Printf and use writer directly Signed-off-by: Sebastiaan van Stijn --- internal/prompt/prompt.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index cc054dfd84..4d47a10e6b 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -5,7 +5,6 @@ package prompt import ( "bufio" "context" - "fmt" "io" "os" "runtime" @@ -50,7 +49,7 @@ func DisableInputEcho(ins *streams.In) (restore func() error, _ error) { // and propagate the error up the stack to prevent the background goroutine // from blocking indefinitely. func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string) (string, error) { - _, _ = fmt.Fprint(out, message) + _, _ = out.Write([]byte(message)) result := make(chan string) go func() { @@ -62,7 +61,7 @@ func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string) select { case <-ctx.Done(): - _, _ = fmt.Fprintln(out, "") + _, _ = out.Write([]byte("\n")) return "", ErrTerminated case r := <-result: return r, nil @@ -80,24 +79,24 @@ func ReadInput(ctx context.Context, in io.Reader, out io.Writer, message string) // returns an error, the caller should close the [io.Reader] used for the prompt // and propagate the error up the stack to prevent the background goroutine // from blocking indefinitely. -func Confirm(ctx context.Context, ins io.Reader, outs io.Writer, message string) (bool, error) { +func Confirm(ctx context.Context, in io.Reader, out io.Writer, message string) (bool, error) { if message == "" { message = "Are you sure you want to proceed?" } message += " [y/N] " - _, _ = fmt.Fprint(outs, message) + _, _ = out.Write([]byte(message)) // On Windows, force the use of the regular OS stdin stream. if runtime.GOOS == "windows" { - ins = streams.NewIn(os.Stdin) + in = streams.NewIn(os.Stdin) } result := make(chan bool) go func() { var res bool - scanner := bufio.NewScanner(ins) + scanner := bufio.NewScanner(in) if scanner.Scan() { answer := strings.TrimSpace(scanner.Text()) if strings.EqualFold(answer, "y") { @@ -109,7 +108,7 @@ func Confirm(ctx context.Context, ins io.Reader, outs io.Writer, message string) select { case <-ctx.Done(): - _, _ = fmt.Fprintln(outs, "") + _, _ = out.Write([]byte("\n")) return false, ErrTerminated case r := <-result: return r, nil