Documentation
¶
Overview ¶
Package prompt asks the user questions on an interactive terminal.
Prompts are written to stderr, never stdout: a command that asks a question must still be safe to pipe. Everything here is driven through an io.Reader so that the specs can feed it a buffer instead of a terminal.
Index ¶
- Variables
- func IsTerminal(v any) bool
- func ReadAll(r io.Reader) (string, error)
- type Option
- type Prompter
- func (p *Prompter) Confirm(label string) (bool, error)
- func (p *Prompter) Interactive() bool
- func (p *Prompter) Line(label, def string) (string, error)
- func (p *Prompter) Password(label string) (string, error)
- func (p *Prompter) Required(label string) (string, error)
- func (p *Prompter) Validated(label string, validate func(string) error) (string, error)
Constants ¶
This section is empty.
Variables ¶
var ErrNotInteractive = errors.New("no interactive terminal")
ErrNotInteractive is returned when a prompt is needed but there is no terminal to ask on — a CI job, or a command whose stdin is a pipe. Commands turn it into "pass --flag instead", which is the only useful thing to say.
Functions ¶
func IsTerminal ¶
IsTerminal reports whether v is an interactive terminal. It accepts anything carrying a file descriptor, so it answers for os.Stdin and os.Stdout alike; a bytes.Buffer has none, which is exactly why a spec's streams are never mistaken for a terminal.
Types ¶
type Option ¶
type Option func(*Prompter)
Option configures a Prompter.
func WithInteractive ¶
WithInteractive overrides whether in is treated as a terminal.
Nothing in fft passes it: production reads the answer off the file descriptor. The specs pass it, because that is the only way to drive the question a destructive command asks.
type Prompter ¶
type Prompter struct {
// contains filtered or unexported fields
}
Prompter reads answers from in and writes questions to out.
func (*Prompter) Confirm ¶
Confirm asks a yes/no question. Anything other than "y" or "yes" is a no — destructive commands should never proceed on an ambiguous answer.
func (*Prompter) Interactive ¶
Interactive reports whether in is a terminal a user could answer on.
func (*Prompter) Line ¶
Line asks for a value, offering def as the default. An empty answer takes the default.
func (*Prompter) Password ¶
Password asks for a secret. On a terminal the input is masked; otherwise it is read as a plain line, which is what lets the specs drive it.
func (*Prompter) Required ¶
Required asks for a value and keeps asking until it gets a non-empty one.