Files
w3cjandClaude Opus 5.5 d58b24affe refactor(ui): prompts in one place: each question declares its answer
Every question is a msg.Question that declares what answers it where
devvm can't ask: AnsweredBy("--force") names a flag or command, which the
refusal names (no_input), and RefusedWith(msg.X(...)) gives a question no
flag answers its own refusal. msg.AnswerFor and its table go.

The Reporter's Ask, Confirm and Answer, and the UI's Secret, take a
Question and return a *msg.Refusal where they can't ask: with --no-input,
without a terminal, or at the end of the input, one refusal for all three
causes. Commands return it through the failure path (showFailure ->
UI.Refused: the question, then the refusal); nothing dies on an unanswered
question any more. Questions with a usual answer fall back on it instead:
network policy on, restart no, history import later, the Include line
warned, an unenforced VM stopped, the first run's size kept.

Gone: confirmed() in cli (4 callers), force_needed and no_input_terminal,
vm.Env.CanAsk and ui.CanAsk, vm's interactive, setup's terminal, the
Reporter's StdinIsTerminal/StdoutIsTerminal and the Fake's StdoutTerminal,
and every caller's terminal check (18 in vm, netpolicy, build and
sshconfig; 3 CanAsk; 1 interactive; 2 in cli). The picker's check
(CanPick) stays in the picker, which keeps its choices with --no-input
too; setup and the first run's agents and the new-VM summary still read
CanPick, because what they save or say depends on whether the picker
showed.

A Question's About context (the network policy's, a secret's howto, the
restart's changes, the size's host) is said only when it's asked.
devvm update asks before its tools change, so a refusal leaves the VM as
it is.

The CLI tests answer through a terminal (ttyin) or, explicitly, with a
test build's DEVVM_PIPED_ANSWERS=1, a line of input each.

Tests: ui's TestConfirm, TestAsk, TestAnswer, TestPromptsOnlyAtATerminal,
TestSecret, TestNoInput, TestNoTerminal, TestNoAnswer, TestPipedAnswers,
TestRefusedInterrupted, TestPickNoInput; msg's TestQuestionRefused,
TestQuestionAbout; cli's agreement test TestQuestionsDeclareAnswers (every
AnsweredBy names devvm's flags and commands, every RefusedWith a message,
no Question made otherwise) and TestCheckAnswer; vm's
TestSizeChoiceRefused; testscript prompts.txtar (each cause, a refusal of
its own, piped answers).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 11:49:57 -07:00

62 lines
2.1 KiB
Go

package msg
// A Question is a message devvm asks, with what answers it when devvm
// can't ask: a flag or a command typed instead (AnsweredBy), or, for a
// question only the user can answer, the refusal that says what to do
// (RefusedWith). Every prompt of a Reporter takes one, so no question is
// asked without its answer declared. About adds what's said before it.
type Question struct {
m M
by string
refusal M
about []M
}
// AnsweredBy makes m a question that by answers without asking, a flag
// or a command (--force, devvm size -g), which its refusal names
// (no_input).
func (m M) AnsweredBy(by string) Question { return Question{m: m, by: by} }
// RefusedWith makes m a question no flag answers, whose refusal is r:
// what to do instead, such as run the command in a terminal.
func (m M) RefusedWith(r M) Question { return Question{m: m, refusal: r} }
// About returns q with these said before it, only when it's asked.
func (q Question) About(ms ...M) Question {
q.about = append(q.about[:len(q.about):len(q.about)], ms...)
return q
}
// M is what's asked.
func (q Question) M() M { return q.m }
// Context is what's said before q, when it's asked.
func (q Question) Context() []M { return q.about }
// Refused is q's refusal when devvm can't ask it: with --no-input,
// without a terminal, or at the end of the input. shown says the prompt
// was printed, so the refusal doesn't show the question again.
func (q Question) Refused(shown bool) *Refusal {
if q.refusal.ID() != "" {
return &Refusal{Msg: q.refusal}
}
r := &Refusal{Msg: NoInput(q.by)}
if !shown {
r.Question = q.m
}
return r
}
// A Refusal is the error a Reporter's prompt returns when it can't ask
// its question (Question.Refused): Msg says why and what answers it, and
// Question, when set, is shown before it. Commands return it as any
// failure; a caller with something to fall back on does that instead.
type Refusal struct {
Question, Msg M
}
func (r *Refusal) Error() string { return r.Msg.Error() }
// Unwrap is Msg, so a refusal is the message it says.
func (r *Refusal) Unwrap() error { return r.Msg }