Documentation
¶
Overview ¶
Package ui provides a simple way to interact with the user through the terminal, i.e. the interface of a CLI.
Index ¶
- Variables
- func Ask(io IO, question string) (string, error)
- func AskAndValidate(io IO, question string, n int, validationFunc func(string) error) (string, error)
- func AskMultiline(io IO, question string) ([]byte, error)
- func AskPassphrase(io IO, question string, repeatPhrase string, n int) (string, error)
- func AskSecret(io IO, question string) (string, error)
- func AskYesNo(io IO, question string, t ConfirmationType) (bool, error)
- func Choose(io IO, question string, getOptions func() ([]Option, bool, error), addOwn bool, ...) (string, error)
- func ConfirmCaseInsensitive(io IO, question string, expected ...string) (bool, error)
- func EOFKey() string
- func Readln(r io.Reader) (string, error)
- type ConfirmationType
- type FakeIO
- type FakeReader
- type FakeWriter
- type IO
- type Option
- type Reader
- type UserIO
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCannotAsk occurs when prompting for input is impossible. ErrCannotAsk = askErr.Code("cannot_ask_for_input").Error("Cannot ask for interactive input.\n\n" + "This usually happens when you run something non-interactively that needs to ask interactive questions.") ErrPassphrasesDoNotMatch = askErr.Code("passphrase_does_not_match").Error("passphrases do not match") )
Errors
var (
ErrReadInput = errRead.Code("read_input").ErrorPref("could not read input: %s")
)
Errors
Functions ¶
func AskAndValidate ¶
func AskAndValidate(io IO, question string, n int, validationFunc func(string) error) (string, error)
AskAndValidate asks the user a question and re-prompts the configured amount of times when the users answer does not validate.
func AskMultiline ¶ added in v0.31.0
AskMultiline prints out the question and reads back the input until an EOF is reached. The input is displayed to the user.
func AskPassphrase ¶
AskPassphrase asks for a password and then asks to type it again for confirmation. When the user types two different passphrases, he is asked again. When the answers still haven't matched after trying n times, the error ErrPassphrasesDoNotMatch is returned. For the empty answer ("") no confirmation is asked.
func AskSecret ¶
AskSecret prints out the question and reads back the input, without echoing it back. Useful for passwords and other sensitive inputs.
func AskYesNo ¶
func AskYesNo(io IO, question string, t ConfirmationType) (bool, error)
AskYesNo asks the user for confirmation. A user must type in "yes" or "no" and then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as confirmations. If no input is given, it will return true with DefaultYes and false with DefaultNo. If the input is not recognized, it will ask again. The function retries 3 times. If it still has no valid response after that, it returns false.
func ConfirmCaseInsensitive ¶
ConfirmCaseInsensitive asks the user to confirm by typing one of the expected strings. The comparison is not case-sensitive. If multiple values for expected are given, true is returned if the input equals any of the the expected values.
Types ¶
type ConfirmationType ¶
type ConfirmationType int
ConfirmationType defines what AskYesNo uses as the default answer.
const ( // DefaultNone assumes no default [y/n] DefaultNone ConfirmationType = iota // DefaultNo assumes no as the default answer [y/N] DefaultNo // DefaultYes assumes yes as the default answer [Y/n] DefaultYes )
type FakeIO ¶
type FakeIO struct { StdIn *FakeReader StdOut *FakeWriter PromptIn *FakeReader PromptOut *FakeWriter PromptErr error }
FakeIO is a helper type for testing that implements the ui.IO interface
type FakeReader ¶
type FakeReader struct { *bytes.Buffer Piped bool Reads []string ReadErr error // contains filtered or unexported fields }
FakeReader implements the Reader interface.
func (*FakeReader) Read ¶
func (f *FakeReader) Read(p []byte) (n int, err error)
Read returns the mocked ReadErr or reads from the mocked buffer.
func (*FakeReader) ReadPassword ¶
func (f *FakeReader) ReadPassword() ([]byte, error)
ReadPassword reads a line from the mocked buffer.
type FakeWriter ¶
FakeWriter implements the Writer interface.
type Reader ¶
type Reader interface { io.Reader // ReadPassword reads a line of input from a terminal without local echo. ReadPassword() ([]byte, error) IsPiped() bool }
Reader can read input for a CLI program.
type UserIO ¶
UserIO is a middleware between input and output to the CLI program. It implements userIO.Prompter and can be passed to libraries.
func NewStdUserIO ¶
func NewStdUserIO() UserIO
NewStdUserIO creates a new UserIO middleware only from os.Stdin and os.Stdout.
func NewUserIO ¶
func NewUserIO() UserIO
NewUserIO creates a new UserIO middleware from os.Stdin and os.Stdout and adds tty if it is available.
func (UserIO) Prompts ¶
Prompts simply returns Stdin and Stdout, when both input and output are not piped. When either input or output is piped, Prompts attempts to bypass stdin and stdout by connecting to /dev/tty on Unix systems when available. On systems where tty is not available and when either input or output is piped, prompting is not possible so an error is returned.