Documentation
¶
Overview ¶
Package prompt asks the user for input on stderr and reads the answer from stdin: confirmations, which --force skips, answers, and secrets, which a terminal hides as they are typed. It decides when a command may prompt at all: stdin is a terminal and --non-interactive is not set.
Index ¶
- func AddForceFlag(cmd *cobra.Command)
- func AddNonInteractiveFlag(cmd *cobra.Command)
- func AddStdinFlag(cmd *cobra.Command, name, usage string)
- func CanPrompt(cmd *cobra.Command) bool
- func Confirm(cmd *cobra.Command, message string) error
- func Prompt(cmd *cobra.Command, message string) (string, error)
- func PromptSecret(cmd *cobra.Command, message string) (string, error)
- func ReadStdinLine(cmd *cobra.Command, name, what string) (string, error)
- func TreatStdinAsTerminalForTesting() (restore func())
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddForceFlag ¶
AddForceFlag adds the --force (-f) flag that skips Confirm.
func AddNonInteractiveFlag ¶
AddNonInteractiveFlag adds --non-interactive to a command that prompts for missing required flags.
func AddStdinFlag ¶
AddStdinFlag adds the bool flag name, which makes the command read a value from stdin, such as --password-stdin. A command given it never prompts, since stdin holds that value.
func CanPrompt ¶
CanPrompt reports whether the command may prompt for a missing required flag: stdin is a terminal, --non-interactive is not set, and no flag added by AddStdinFlag reads stdin.
func Confirm ¶
Confirm asks the user to confirm message unless --force is set. The prompt goes to stderr and the answer is read from stdin, so stdout carries only the command's output. Any answer but "y" or "yes" cancels the operation, and so does stdin that holds no answer or cannot be read, with how to skip the confirmation when the command has --force.
func Prompt ¶
Prompt writes message to stderr and returns the next line of stdin, trimmed. A later Prompt or Confirm on the same stdin reads the line after it. Stdin ending before an answer cancels the operation, as promptError says.
func PromptSecret ¶
PromptSecret is Prompt for a secret, such as an API key or a password, which keeps the answer as typed, as trimLineEnding says: when stdin is a terminal, the answer is hidden as it is typed. Ctrl+D, Ctrl+C and the end of input cancel the operation, as promptError says, and leave the terminal as it was; so does a SIGINT or SIGTERM, as exitOnSignal says.
func ReadStdinLine ¶
ReadStdinLine returns the first line of stdin, without its line break, for the flag name added by AddStdinFlag, which reads what, such as "password". Stdin without one, or with an empty first line, is a validation error.
func TreatStdinAsTerminalForTesting ¶
func TreatStdinAsTerminalForTesting() (restore func())
TreatStdinAsTerminalForTesting makes CanPrompt treat stdin as a terminal, so a test can answer prompts from a buffer. It returns the function that restores the check.
Types ¶
This section is empty.