From ac5543a9a4a80f55be8176054ff1d6e7f9416349 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Fri, 20 Oct 2023 10:53:01 +0200 Subject: [PATCH] fix: empty string when new newline at the end of stdin (#96) The code reads the token from `stdin` until newline. If there is no newline, it counts the content as empty string. We use `bufio.NewReader` with `reader.ReadString('\n')` which "reads until the first occurrence of delim in the input". When it can't find a newline character before the stream ends we return with an error and an empty string. Fix: As `reader.ReadString` returns with the content before the error in all cases, we can return with the content even if we meet an error. Note: We should check if the error was EOF on caller side everywhere. Fixes #95 References: * https://github.com/yitsushi/totp-cli/issues/95 Signed-off-by: Efertone --- internal/terminal/terminal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/terminal/terminal.go b/internal/terminal/terminal.go index 3046e9e..ccf4751 100644 --- a/internal/terminal/terminal.go +++ b/internal/terminal/terminal.go @@ -36,7 +36,7 @@ func (t Terminal) Read(prompt string) (string, error) { text, readErr := reader.ReadString('\n') if readErr != nil { - return "", fmt.Errorf("error reading from input: %w", readErr) + return text, fmt.Errorf("error reading from input: %w", readErr) } text = strings.TrimSpace(text)