prompt

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func IsTerminal(v any) bool

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.

func ReadAll

func ReadAll(r io.Reader) (string, error)

ReadAll reads a secret piped on stdin, for --password-stdin. It trims a single trailing newline — `echo secret | fft ...` appends one and the user did not mean it to be part of the password — but nothing else, because leading and trailing spaces can be genuine.

Types

type Option

type Option func(*Prompter)

Option configures a Prompter.

func WithInteractive

func WithInteractive(v bool) Option

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 New

func New(in io.Reader, out io.Writer, opts ...Option) *Prompter

New returns a Prompter reading from in and writing prompts to out. out should be stderr.

func (*Prompter) Confirm

func (p *Prompter) Confirm(label string) (bool, error)

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

func (p *Prompter) Interactive() bool

Interactive reports whether in is a terminal a user could answer on.

func (*Prompter) Line

func (p *Prompter) Line(label, def string) (string, error)

Line asks for a value, offering def as the default. An empty answer takes the default.

func (*Prompter) Password

func (p *Prompter) Password(label string) (string, error)

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

func (p *Prompter) Required(label string) (string, error)

Required asks for a value and keeps asking until it gets a non-empty one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL