Documentation ¶
Overview ¶
Package prompts contains helper functions for prompting users for input via the command line. It is a lightweight wrapper around the pterm library for writing text-based user interfaces.
Index ¶
- Variables
- func EditFile(initialContent []byte) ([]byte, error)
- func EditFileValidatedByFullContent(prompt, content string, fileValidate func(content string) error, minLines int) (string, error)
- func EditFileValidatedByLine(prompt, content, separator string, lineValidate func(input string) error, ...) (string, error)
- func FilterLines(lines []string, validate func(input string) error) ([]string, error)
- func MultiSelect(prompt string, options []string, minSelections int) ([]string, error)
- func ReadBasicCreds(usernamePrompt, passwordPrompt, defaultUsername, defaultPassword string, ...) (string, string, error)
- func ReadBool(prompt string, defaultVal bool) (bool, error)
- func ReadCACert(prompt string, defaultCaCertPath, caCertPathOverride string) (caCertPath string, caCertName string, caCertData []byte, err error)
- func ReadCIDRs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)
- func ReadDomainOrIPNoPort(label, defaultVal, errMsg string, optional bool) (string, error)
- func ReadDomains(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)
- func ReadDomainsOrIPs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)
- func ReadDomainsOrIPsOrURLs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)
- func ReadFilePath(label, defaultVal, errMsg string, optional bool) (string, error)
- func ReadIPs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)
- func ReadInt(prompt, defaultVal string, minVal, maxVal int) (int, error)
- func ReadIntSlice(label, defaultVal string, optional bool) ([]int, error)
- func ReadK8sName(label, defaultVal string, optional bool) (string, error)
- func ReadPassword(label, defaultVal string, optional bool, maxLen int) (string, error)
- func ReadSemVer(label, defaultVal, errMsg string) (string, error)
- func ReadText(label, defaultVal string, optional bool, maxLen int) (string, error)
- func ReadTextRegex(label, defaultVal, errMsg, regexPattern string) (string, error)
- func ReadTextSlice(label, defaultVal, errMsg, regexPattern string, optional bool) ([]string, error)
- func ReadTextSliceCustom(label, defaultVal string, optional bool, validate func(input []string) error) ([]string, error)
- func ReadURL(label, defaultVal, errMsg string, optional bool) (string, error)
- func ReadURLRegex(label, defaultVal, errMsg, regexPattern string) (string, error)
- func ReadURLSlice(label, defaultVal, errMsg string, optional bool) ([]string, error)
- func Select(prompt string, options []string) (string, error)
- func SetLogger(l pterm.Logger)
- func ValidateJSON(s string) error
- func ValidateNoProxy(s string) error
- func ValidateSSHPublicKey(s string) error
- type ChoiceItem
- type PtermTUI
- func (p PtermTUI) GetBool(prompt string, defaultVal bool) (bool, error)
- func (p PtermTUI) GetMultiSelection(prompt string, options []string, minSelections int) ([]string, error)
- func (p PtermTUI) GetSelection(prompt string, options []string) (string, error)
- func (p PtermTUI) GetText(prompt, defaultVal, mask string, optional bool, validate func(string) error) (string, error)
- func (p PtermTUI) GetTextSlice(prompt, defaultVal string, optional bool, validate func([]string) error) ([]string, error)
- type TUI
Constants ¶
This section is empty.
Variables ¶
var ( // Tui is exported to enable monkey-patching. Tui TUI = PtermTUI{} // ErrValidationFailed is returned when input validation fails. ErrValidationFailed = errors.New("validation failed") // ErrInputMandatory is returned when mandatory input is missing. ErrInputMandatory = errors.New("input is mandatory") // KindClusterRegex is a regex pattern for validating a kind cluster name. KindClusterRegex = "^[a-z0-9]{1}[a-z0-9-]{0,30}[a-z0-9]{1}$" // ArtifactRefRegex is a regex pattern for validating an OCI artifact reference. ArtifactRefRegex = "^[a-z0-9_.\\-\\/]+(:.*)?(@sha256:.*)?$" // UUIDRegex is a regex pattern for validating a UUID. UUIDRegex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" )
var (
// GetCmdExecutor allows monkey-patching the command executor for testing purposes.
GetCmdExecutor = getEditorExecutor
)
Functions ¶
func EditFile ¶
EditFile prompts a user to edit a file with a predefined prompt and initial content.
func EditFileValidatedByFullContent ¶
func EditFileValidatedByFullContent(prompt, content string, fileValidate func(content string) error, minLines int) (string, error)
EditFileValidatedByFullContent prompts a user to edit a file with a predefined prompt and initial content. An optional file validation function can be specified to validate the content of the entire file. Additionally, a minimum number of lines can be specified for the file. The final file content is returned to the caller.
func EditFileValidatedByLine ¶
func EditFileValidatedByLine(prompt, content, separator string, lineValidate func(input string) error, minEntries int) (string, error)
EditFileValidatedByLine prompts a user to edit a file with a predefined prompt, initial content, and separator. An optional line function can be specified to validate the content of each line. Entries within the file must be newline-separated. Additionally, a minimum number of entries can be specified. The values on each line are joined by the separator and returned to the caller.
func FilterLines ¶ added in v0.1.2
FilterLines filters a list of lines from a file. Comment lines (starting with '#') and any line matching an optional validation function are removed.
func MultiSelect ¶
MultiSelect prompts the user to select at least n options from a list of options.
func ReadBasicCreds ¶
func ReadBasicCreds(usernamePrompt, passwordPrompt, defaultUsername, defaultPassword string, optional, maskUser bool) (string, string, error)
ReadBasicCreds prompts the user to enter a username and password. User input for the password is always masked and the username can optionally be masked.
func ReadCACert ¶
func ReadCACert(prompt string, defaultCaCertPath, caCertPathOverride string) (caCertPath string, caCertName string, caCertData []byte, err error)
ReadCACert loads and validates a CA certificate from the local file system when caCertPathOverride is provided. Otherwise, the user is prompted to enter a CA certificate path and the certificate at that path is loaded and validated.
func ReadCIDRs ¶
ReadCIDRs prompts the user to enter a comma-separated string of CIDR blocks, optionally with a maximum number of values.
func ReadDomainOrIPNoPort ¶
ReadDomainOrIPNoPort prompts the user to enter an FQDN or an IPv4/IPv6 address without a port.
func ReadDomains ¶
ReadDomains prompts the user to enter a comma-separated string of FQDNs, optionally with a maximum number of values.
func ReadDomainsOrIPs ¶
ReadDomainsOrIPs prompts the user to enter a comma-separated string of FQDNs or IPv4/IPv6 addresses, optionally with a maximum number of values.
func ReadDomainsOrIPsOrURLs ¶ added in v0.1.3
func ReadDomainsOrIPsOrURLs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)
ReadDomainsOrIPsOrURLs prompts the user to enter a comma-separated string of FQDNs or IPv4/IPv6 addresses or URLs, optionally with a maximum number of values.
func ReadFilePath ¶
ReadFilePath prompts the user to enter a fully qualified path for a file on the local file system.
func ReadIPs ¶
ReadIPs prompts the user to enter a comma-separated string of IPv4 or IPv6 addresses, optionally with a maximum number of values.
func ReadIntSlice ¶
ReadIntSlice prompts the user to enter a slice of integers.
func ReadK8sName ¶
ReadK8sName prompts the user to enter a string which is a valid Kubernetes name. Inputs must be both a Kubernetes "qualified name" and compliant with DNS (RFC 1123).
func ReadPassword ¶
ReadPassword prompts the user to enter a password with an optional maximum length. User input is masked with the '*' character.
func ReadSemVer ¶
ReadSemVer prompts the user to enter a semantic version.
func ReadText ¶
ReadText prompts the user to enter a string value, optionally with a maximum length.
func ReadTextRegex ¶
ReadTextRegex prompts the user to enter a string value with regex validation.
func ReadTextSlice ¶
ReadTextSlice prompts the user to enter a slice of strings with optional regex validation.
func ReadTextSliceCustom ¶ added in v0.1.1
func ReadTextSliceCustom(label, defaultVal string, optional bool, validate func(input []string) error) ([]string, error)
ReadTextSliceCustom prompts the user to enter a slice of strings with custom validation.
func ReadURLRegex ¶
ReadURLRegex prompts the user to enter a URL with additional regex validation.
func ReadURLSlice ¶
ReadURLSlice prompts the user to enter a slice of URLs.
func ValidateJSON ¶ added in v0.1.1
ValidateJSON validates that the input string is valid JSON.
func ValidateNoProxy ¶
ValidateNoProxy validates input for the NO_PROXY environment variable. See: https://pkg.go.dev/golang.org/x/net/http/httpproxy#Config
func ValidateSSHPublicKey ¶
ValidateSSHPublicKey validates that the input string is an SSH public key.
Types ¶
type ChoiceItem ¶
ChoiceItem is a struct representing a selectable item.
func SelectID ¶
func SelectID(prompt string, items []ChoiceItem) (*ChoiceItem, error)
SelectID prompts the user to select an item from a list of ChoiceItems. Useful for presenting a pretty name to the user, but obtaining an ID associated with the pretty name.
type PtermTUI ¶
type PtermTUI struct{}
PtermTUI is a text-user-interface implementation using the pterm library.
func (PtermTUI) GetBool ¶
GetBool prompts a bool from the user while automatically appending a ? character to the end of the prompt message.
func (PtermTUI) GetMultiSelection ¶
func (p PtermTUI) GetMultiSelection(prompt string, options []string, minSelections int) ([]string, error)
GetMultiSelection prompts the user to select multiple options from a list of options.
func (PtermTUI) GetSelection ¶
GetSelection prompts the user to select an option from a list of options.
func (PtermTUI) GetText ¶
func (p PtermTUI) GetText(prompt, defaultVal, mask string, optional bool, validate func(string) error) (string, error)
GetText prompts a string from the user with optional validation. If the default value is >60 characters, multiline input with tab completion is used. Otherwise, single line input with enter completion is used.
type TUI ¶
type TUI interface { GetBool(prompt string, defaultVal bool) (bool, error) GetText(prompt, defaultVal, mask string, optional bool, validate func(string) error) (string, error) GetTextSlice(prompt, defaultVal string, optional bool, validate func([]string) error) ([]string, error) GetSelection(prompt string, options []string) (string, error) GetMultiSelection(prompt string, options []string, minSelections int) ([]string, error) }
TUI is a text-user-interface for eliciting user input.