prompts

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 21 Imported by: 6

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

Constants

This section is empty.

Variables

View Source
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}$"
)
View Source
var (

	// GetCmdExecutor allows monkey-patching the command executor for testing purposes.
	GetCmdExecutor = getEditorExecutor
)

Functions

func EditFile

func EditFile(initialContent []byte) ([]byte, error)

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

func FilterLines(lines []string, validate func(input string) error) ([]string, error)

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

func MultiSelect(prompt string, options []string, minSelections int) ([]string, error)

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 ReadBool

func ReadBool(prompt string, defaultVal bool) (bool, error)

ReadBool prompts the user to enter a boolean value.

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

func ReadCIDRs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)

ReadCIDRs prompts the user to enter a comma-separated string of CIDR blocks, optionally with a maximum number of values.

func ReadDomainOrIPNoPort

func ReadDomainOrIPNoPort(label, defaultVal, errMsg string, optional bool) (string, error)

ReadDomainOrIPNoPort prompts the user to enter an FQDN or an IPv4/IPv6 address without a port.

func ReadDomains

func ReadDomains(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)

ReadDomains prompts the user to enter a comma-separated string of FQDNs, optionally with a maximum number of values.

func ReadDomainsOrIPs

func ReadDomainsOrIPs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)

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

func ReadFilePath(label, defaultVal, errMsg string, optional bool) (string, error)

ReadFilePath prompts the user to enter a fully qualified path for a file on the local file system.

func ReadIPs

func ReadIPs(label, defaultVal, errMsg string, optional bool, maxVals int) (string, error)

ReadIPs prompts the user to enter a comma-separated string of IPv4 or IPv6 addresses, optionally with a maximum number of values.

func ReadInt

func ReadInt(prompt, defaultVal string, minVal, maxVal int) (int, error)

ReadInt prompts the user to enter an integer value with optional min and max values.

func ReadIntSlice

func ReadIntSlice(label, defaultVal string, optional bool) ([]int, error)

ReadIntSlice prompts the user to enter a slice of integers.

func ReadK8sName

func ReadK8sName(label, defaultVal string, optional bool) (string, error)

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

func ReadPassword(label, defaultVal string, optional bool, maxLen int) (string, error)

ReadPassword prompts the user to enter a password with an optional maximum length. User input is masked with the '*' character.

func ReadSemVer

func ReadSemVer(label, defaultVal, errMsg string) (string, error)

ReadSemVer prompts the user to enter a semantic version.

func ReadText

func ReadText(label, defaultVal string, optional bool, maxLen int) (string, error)

ReadText prompts the user to enter a string value, optionally with a maximum length.

func ReadTextRegex

func ReadTextRegex(label, defaultVal, errMsg, regexPattern string) (string, error)

ReadTextRegex prompts the user to enter a string value with regex validation.

func ReadTextSlice

func ReadTextSlice(label, defaultVal, errMsg, regexPattern string, optional bool) ([]string, error)

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 ReadURL

func ReadURL(label, defaultVal, errMsg string, optional bool) (string, error)

ReadURL prompts the user to enter a URL.

func ReadURLRegex

func ReadURLRegex(label, defaultVal, errMsg, regexPattern string) (string, error)

ReadURLRegex prompts the user to enter a URL with additional regex validation.

func ReadURLSlice

func ReadURLSlice(label, defaultVal, errMsg string, optional bool) ([]string, error)

ReadURLSlice prompts the user to enter a slice of URLs.

func Select

func Select(prompt string, options []string) (string, error)

Select prompts the user to select a single option from a list of options.

func SetLogger

func SetLogger(l pterm.Logger)

SetLogger sets the logger to be used by the prompts package.

func ValidateJSON added in v0.1.1

func ValidateJSON(s string) error

ValidateJSON validates that the input string is valid JSON.

func ValidateNoProxy

func ValidateNoProxy(s string) error

ValidateNoProxy validates input for the NO_PROXY environment variable. See: https://pkg.go.dev/golang.org/x/net/http/httpproxy#Config

func ValidateSSHPublicKey

func ValidateSSHPublicKey(s string) error

ValidateSSHPublicKey validates that the input string is an SSH public key.

Types

type ChoiceItem

type ChoiceItem struct {
	ID   string
	Name string
}

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

func (p PtermTUI) GetBool(prompt string, defaultVal bool) (bool, error)

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

func (p PtermTUI) GetSelection(prompt string, options []string) (string, error)

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.

func (PtermTUI) GetTextSlice

func (p PtermTUI) GetTextSlice(prompt, defaultVal string, optional bool, validate func([]string) error) ([]string, error)

GetTextSlice prompts a slice of strings from the user with optional validation.

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.

Directories

Path Synopsis
Package mocks contains mock implementations of the interfaces in the prompts package for testing purposes.
Package mocks contains mock implementations of the interfaces in the prompts package for testing purposes.

Jump to

Keyboard shortcuts

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