Documentation
¶
Overview ¶
Package prompt provides interfaces and implementations for interactive user prompts, designed for testability with mock implementations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CredentialReader ¶
type CredentialReader interface {
// ReadCredential displays a prompt and reads a credential with hidden input.
// The input is not echoed to the terminal for security.
// Returns the credential string or an error if input cannot be read.
ReadCredential(prompt string) (string, error)
}
CredentialReader defines the interface for reading sensitive credentials from the user with hidden input.
type MockCredentialReader ¶
type MockCredentialReader struct {
// Credentials is a queue of credentials to return for successive calls.
Credentials []string
// Errors is a queue of errors to return for successive calls.
// If non-nil, the error is returned instead of the credential.
Errors []error
// Calls records all prompts passed to ReadCredential for verification.
Calls []string
// contains filtered or unexported fields
}
MockCredentialReader implements CredentialReader for testing, returning pre-configured credentials.
func NewMockCredentialReader ¶
func NewMockCredentialReader(credentials ...string) *MockCredentialReader
NewMockCredentialReader creates a MockCredentialReader with the given credentials.
func (*MockCredentialReader) ReadCredential ¶
func (m *MockCredentialReader) ReadCredential(prompt string) (string, error)
ReadCredential returns the next pre-configured credential or error.
type MockPrompter ¶
type MockPrompter struct {
// Responses is a queue of responses to return for successive calls.
// Each response is the zero-based index to return.
Responses []int
// Errors is a queue of errors to return for successive calls.
// If non-nil, the error is returned instead of the response.
Errors []error
// Calls records all calls made to Prompt for verification.
Calls []MockPrompterCall
// contains filtered or unexported fields
}
MockPrompter implements Prompter for testing, returning pre-configured responses.
func NewMockPrompter ¶
func NewMockPrompter(responses ...int) *MockPrompter
NewMockPrompter creates a MockPrompter with the given responses.
type MockPrompterCall ¶
MockPrompterCall records a single call to Prompt.
type MockYesNoCall ¶
MockYesNoCall records a single call to PromptYesNo.
type MockYesNoPrompter ¶
type MockYesNoPrompter struct {
// Responses is a queue of responses to return for successive calls.
Responses []bool
// Errors is a queue of errors to return for successive calls.
Errors []error
// Calls records all calls made to PromptYesNo for verification.
Calls []MockYesNoCall
// contains filtered or unexported fields
}
MockYesNoPrompter implements YesNoPrompter for testing.
func NewMockYesNoPrompter ¶
func NewMockYesNoPrompter(responses ...bool) *MockYesNoPrompter
NewMockYesNoPrompter creates a MockYesNoPrompter with the given responses.
func (*MockYesNoPrompter) PromptYesNo ¶
func (m *MockYesNoPrompter) PromptYesNo(prompt string, defaultYes bool) (bool, error)
PromptYesNo returns the next pre-configured response or error.
type Prompter ¶
type Prompter interface {
// Prompt displays a prompt with numbered options and returns the
// zero-based index of the selected option. If the user presses Enter
// without input, defaultIdx is returned. Returns an error if the
// selection is invalid or input cannot be read.
Prompt(prompt string, options []string, defaultIdx int) (int, error)
}
Prompter defines the interface for presenting options to a user and getting their selection.
type StdinPrompter ¶
StdinPrompter implements Prompter using stdin/stdout for real terminal use.
func NewStdinPrompter ¶
func NewStdinPrompter(r io.Reader, w io.Writer) *StdinPrompter
NewStdinPrompter creates a StdinPrompter that reads from r and writes to w.
type StdinYesNoPrompter ¶
StdinYesNoPrompter implements YesNoPrompter using stdin/stdout.
func NewStdinYesNoPrompter ¶
func NewStdinYesNoPrompter(r io.Reader, w io.Writer) *StdinYesNoPrompter
NewStdinYesNoPrompter creates a StdinYesNoPrompter that reads from r and writes to w.
func (*StdinYesNoPrompter) PromptYesNo ¶
func (p *StdinYesNoPrompter) PromptYesNo(prompt string, defaultYes bool) (bool, error)
PromptYesNo displays the prompt and reads user input. Accepts "y", "Y", "yes", "YES" as true; "n", "N", "no", "NO" as false. Empty input returns defaultYes.
type TerminalCredentialReader ¶
TerminalCredentialReader implements CredentialReader using golang.org/x/term for secure hidden input from a real terminal.
func NewTerminalCredentialReader ¶
func NewTerminalCredentialReader(in *os.File, out io.Writer) *TerminalCredentialReader
NewTerminalCredentialReader creates a TerminalCredentialReader that reads from the given file (typically os.Stdin) and writes prompts to w.
func (*TerminalCredentialReader) ReadCredential ¶
func (r *TerminalCredentialReader) ReadCredential(prompt string) (string, error)
ReadCredential displays the prompt and reads input with echoing disabled.
type YesNoPrompter ¶
type YesNoPrompter interface {
// PromptYesNo displays a yes/no prompt and returns the user's response.
// If the user presses Enter without input, defaultYes determines the result.
// Returns true for yes, false for no.
PromptYesNo(prompt string, defaultYes bool) (bool, error)
}
YesNoPrompter defines the interface for yes/no confirmation prompts.