Files
archie/internal/infrastructure/configuration/setup/prompter.go
T
sam 6ee86f2593 feat(setup): Prompter interface and a TTY-gated terminal implementation
archie-core-via. internal/infrastructure/configuration/setup defines
Prompter -- Select, ReadLine, ReadSecret, Confirm, all context-aware --
which is all the setup step logic (to follow) needs from a prompt
surface. No terminal dependency, so steps are testable without a TTY.

internal/infrastructure/terminalprompt.Terminal is the real
implementation, using golang.org/x/term. New refuses construction when
its input isn't a real terminal, with a message naming the problem --
enforcing "a non-TTY invocation fails with a clear message rather than
writing a partial config" at the earliest possible point, rather than
falling back to a plain read that would silently accept piped/scripted
input (ssh, `archied setup < answers.txt`, CI). Terminal satisfies
Prompter structurally; this package does not import that one, so
there's no infrastructure -> infrastructure edge for what Go's type
system already gives for free.

Each blocking read runs in a goroutine raced against ctx.Done(), so
Ctrl+C returns promptly instead of leaving the operator stuck; the
losing goroutine is abandoned on cancellation; term.ReadPassword's own
raw-mode restoration is not interruptible, so an abandoned secret read
can leave the terminal in raw mode until it unblocks on its own --
documented on Terminal.readSecret rather than worked around, since a
cancelled setup command is expected to exit shortly after via its own
signal handling anyway.

Tested by constructing a Terminal directly (bypassing New, same
package) over a plain pipe with an injected readSecret seam -- New's
TTY gate itself is exercised separately against a real os.Pipe(). No
weakening of the production constructor for testability.

Refs archie-core-via
2026-08-05 12:47:20 +10:00

29 lines
1.4 KiB
Go

// Package setup builds a config.Config and the TOML edits to persist it,
// driven by archied's interactive first-run configuration flow. It has no
// terminal dependency of its own: everything it needs from a user-facing
// prompt surface is the [Prompter] interface below, so the step logic is
// testable end to end without a TTY.
package setup
import "context"
// Prompter is what setup's step logic needs from a prompt surface. The real
// implementation talks to a terminal (internal/infrastructure/
// terminalprompt); tests drive steps through a fake.
//
// Every method takes a context so a step can be cancelled mid-prompt (e.g.
// Ctrl+C) rather than leaving the operator stuck at a prompt an outer
// timeout or signal has already decided to abandon.
type Prompter interface {
// Select presents options and returns the chosen index. Returns -1 and
// ctx.Err() if ctx is done before an answer is given.
Select(ctx context.Context, prompt string, options []string) (int, error)
// ReadLine reads one line of visible input. An empty answer returns
// defaultValue.
ReadLine(ctx context.Context, prompt, defaultValue string) (string, error)
// ReadSecret reads one line of input without echoing it.
ReadSecret(ctx context.Context, prompt string) (string, error)
// Confirm asks a yes/no question. An empty answer returns defaultYes.
Confirm(ctx context.Context, prompt string, defaultYes bool) (bool, error)
}