From 4ec7341e76bf7ad5dc91e21653f010ad4f07d368 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 10:48:56 +0000 Subject: [PATCH] fix(cli): surface eof for empty prompts Co-Authored-By: Virgil --- pkg/cli/prompt.go | 17 ++++++++++++----- pkg/cli/prompt_test.go | 9 +++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/cli/prompt.go b/pkg/cli/prompt.go index f95bec1..af01afd 100644 --- a/pkg/cli/prompt.go +++ b/pkg/cli/prompt.go @@ -42,11 +42,18 @@ func Prompt(label, defaultVal string) (string, error) { r := newReader() input, err := r.ReadString('\n') - if err != nil && !errors.Is(err, io.EOF) { - return "", err - } - input = strings.TrimSpace(input) + if err != nil { + if !errors.Is(err, io.EOF) { + return "", err + } + if input == "" { + if defaultVal != "" { + return defaultVal, nil + } + return "", err + } + } if input == "" { return defaultVal, nil } @@ -68,7 +75,7 @@ func Select(label string, options []string) (string, error) { r := newReader() input, err := r.ReadString('\n') if err != nil && strings.TrimSpace(input) == "" { - return "", err + return "", fmt.Errorf("selection cancelled: %w", err) } trimmed := strings.TrimSpace(input) diff --git a/pkg/cli/prompt_test.go b/pkg/cli/prompt_test.go index e72ac5e..c7cf643 100644 --- a/pkg/cli/prompt_test.go +++ b/pkg/cli/prompt_test.go @@ -36,6 +36,15 @@ func TestPrompt_Bad_EOFUsesDefault(t *testing.T) { assert.Equal(t, "world", val) } +func TestPrompt_Bad_EOFWithoutDefaultReturnsError(t *testing.T) { + SetStdin(strings.NewReader("")) + defer SetStdin(nil) + + val, err := Prompt("Name", "") + assert.ErrorIs(t, err, io.EOF) + assert.Empty(t, val) +} + func TestSelect_Good(t *testing.T) { SetStdin(strings.NewReader("2\n")) defer SetStdin(nil) -- 2.45.3