prompts

package
v1.22.6 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: BSD-3-Clause Imports: 24 Imported by: 0

Documentation

Overview

Package prompts provides interactive and non-interactive user prompting utilities.

Index

Constants

View Source
const (
	// EnvNonInteractive forces non-interactive mode.
	// Set to "1", "true", "yes", or "on" to enable.
	EnvNonInteractive = "LUX_NON_INTERACTIVE"

	// EnvCI is a common CI environment variable.
	// When truthy, implies non-interactive.
	EnvCI = "CI"
)

Environment variable names for non-interactive mode.

View Source
const (
	Yes = "Yes"
	No  = "No"

	Add        = "Add"
	Del        = "Delete"
	Preview    = "Preview"
	MoreInfo   = "More Info"
	Done       = "Done"
	Cancel     = "Cancel"
	LessThanEq = "Less Than Or Eq"
	MoreThanEq = "More Than Or Eq"
	MoreThan   = "More Than"

	// Address formats
	PChainFormat = "P-Chain"
	CChainFormat = "C-Chain"
)
View Source
const EVMFormat = "evm"

EVMFormat represents the EVM address format

Variables

View Source
var ErrNonInteractive = errors.New("cannot prompt in non-interactive mode")

ErrNonInteractive is returned when a prompt is attempted in non-interactive mode. Commands should catch this error and provide actionable guidance.

Functions

func CaptureKeyAddress

func CaptureKeyAddress(
	prompt Prompter,
	goal string,
	keyDir string,
	getKey func(string) (string, error),
	network models.Network,
	addressFormat string,
) (string, error)

CaptureKeyAddress prompts the user to select a key address

func CaptureListDecision

func CaptureListDecision[T comparable](

	prompter Prompter,

	prompt string,

	capture func(prompt string) (T, error),

	capturePrompt string,

	label string,

	info string,
) ([]T, bool, error)

CaptureListDecision runs a for loop and continuously asks the user for a specific input (currently only `CapturePChainAddress` and `CaptureAddress` is supported) until the user cancels or chooses `Done`. It does also offer an optional `info` to print (if provided) and a preview. Items can also be removed.

func CheckSubnetAuthKeys

func CheckSubnetAuthKeys(subnetAuthKeys []string, controlKeys []string, threshold uint32) error

check subnet authorization criteria: - [subnetAuthKeys] satisfy subnet's [threshold] - [subnetAuthKeys] is a subset of subnet's [controlKeys]

func ConvertToAddress

func ConvertToAddress(addr string) (crypto.Address, error)

ConvertToAddress converts a string to a crypto.Address

func GetKeyOrLedger

func GetKeyOrLedger(prompter Prompter, goal string, keyDir string) (bool, string, error)

GetKeyOrLedger prompts user to choose between key or ledger

func GetSubnetAuthKeys

func GetSubnetAuthKeys(prompt Prompter, controlKeys []string, threshold uint32) ([]string, error)

get subnet authorization keys from the user, as a subset of the subnet's [controlKeys] with a len equal to the subnet's [threshold]

func GetTestnetKeyOrLedger

func GetTestnetKeyOrLedger(prompt Prompter, goal string, keyDir string) (bool, string, error)

func IsInteractive added in v1.22.6

func IsInteractive() bool

IsInteractive returns true if prompting is allowed.

Interactive mode is enabled when ALL of:

  • stdin is a TTY (not piped/redirected)
  • LUX_NON_INTERACTIVE is not truthy
  • CI is not truthy

This follows UNIX conventions:

  • If stdin is not a TTY → never prompt (scripts, pipes)
  • Explicit env override always wins

func IsNonInteractive added in v1.22.6

func IsNonInteractive(flag bool) bool

IsNonInteractive is the inverse of IsInteractive. Deprecated: Use !IsInteractive() or the Validator pattern instead.

func MissingError added in v1.22.6

func MissingError(cmd string, missing []MissingOpt) error

MissingError creates a clear, actionable error listing all missing options. The error message follows UNIX conventions and guides users to the right flags.

func MustInteractive added in v1.22.6

func MustInteractive(operation string)

MustInteractive panics if in non-interactive mode. Use for operations that absolutely require user interaction and cannot be made non-interactive (e.g., ledger signing).

func PromptAddress

func PromptAddress(prompter Prompter, prompt string) (string, error)

PromptAddress prompts the user for an address

func PromptChain

func PromptChain(
	prompt Prompter,
	message string,
	blockchainNames []string,
	pChainEnabled bool,
	xChainEnabled bool,
	cChainEnabled bool,
	blockchainNameToAvoid string,
	blockchainIDEnabled bool,
) (bool, bool, bool, bool, string, string, error)

PromptChain prompts the user to select a chain

func PromptOrFail added in v1.22.6

func PromptOrFail(cmd string, missing []MissingOpt, promptFn func(MissingOpt) (string, error), targets ...*string) error

PromptOrFail handles the common pattern of prompting for missing values. In interactive mode, prompts for each missing option. In non-interactive mode, returns an error listing all missing options.

Usage:

missing := []prompts.MissingOpt{}
if chainID == "" {
    missing = append(missing, prompts.MissingOpt{Flag: "--chain-id", Env: "LUX_CHAIN_ID", Prompt: "EVM chain ID"})
}
if err := prompts.PromptOrFail("lux chain create", missing, func(m MissingOpt) (string, error) {
    return app.Prompt.CaptureString(m.Prompt)
}, &chainID); err != nil {
    return err
}

func PromptPrivateKey

func PromptPrivateKey(prompter Prompter, prompt string) (string, error)

PromptPrivateKey prompts the user for a private key

func RequestURL

func RequestURL(url string) error

RequestURL makes a GET request to validate URL connectivity

func ValidateAddress

func ValidateAddress(addr string) error

ValidateAddress validates an Ethereum address Accepts addresses with or without 0x prefix

func ValidateHexa

func ValidateHexa(s string) error

ValidateHexa validates a hexadecimal string

func ValidateNodeID

func ValidateNodeID(input string) error

ValidateNodeID validates a node ID string (exported for external use)

func ValidatePositiveInt

func ValidatePositiveInt(input string) error

ValidatePositiveInt validates that a string can be parsed as a positive integer

func ValidateRepoBranch

func ValidateRepoBranch(branch string) error

ValidateRepoBranch validates a git branch name

func ValidateRepoFile

func ValidateRepoFile(filepath string) error

ValidateRepoFile validates a repository file path

func ValidateURL

func ValidateURL(input string, checkConnection bool) error

ValidateURL validates URL format and optionally checks connectivity

func ValidateURLFormat

func ValidateURLFormat(input string) error

Types

type Comparator

type Comparator struct {
	Label string // Label that identifies reference value
	Type  string // Less Than Eq or More than Eq
	Value uint64 // Value to Compare To
}

func (*Comparator) Validate

func (comparator *Comparator) Validate(val uint64) error

type MissingOpt added in v1.22.6

type MissingOpt struct {
	Flag    string // e.g., "--chain-id"
	Env     string // e.g., "LUX_CHAIN_ID" (optional)
	Prompt  string // e.g., "EVM chain ID" - used for interactive prompts
	Note    string // optional additional context
	Default string // optional default value hint
}

MissingOpt describes a required option that was not provided.

type NonInteractivePrompter added in v1.22.6

type NonInteractivePrompter struct {
	// FailMessage provides context about what flag/env var to set.
	// If empty, a default message is used.
	FailMessage string
}

NonInteractivePrompter implements Prompter but fails fast on any prompt attempt. Use this in CI/script environments to detect missing flags early.

func NewNonInteractivePrompter added in v1.22.6

func NewNonInteractivePrompter() *NonInteractivePrompter

NewNonInteractivePrompter creates a prompter that fails fast on any interaction.

func NewNonInteractivePrompterWithMessage added in v1.22.6

func NewNonInteractivePrompterWithMessage(msg string) *NonInteractivePrompter

NewNonInteractivePrompterWithMessage creates a prompter with a custom fail message.

func (*NonInteractivePrompter) CaptureAddress added in v1.22.6

func (p *NonInteractivePrompter) CaptureAddress(promptStr string) (common.Address, error)

func (*NonInteractivePrompter) CaptureAddresses added in v1.22.6

func (p *NonInteractivePrompter) CaptureAddresses(promptStr string) ([]common.Address, error)

func (*NonInteractivePrompter) CaptureDate added in v1.22.6

func (p *NonInteractivePrompter) CaptureDate(promptStr string) (time.Time, error)

func (*NonInteractivePrompter) CaptureDuration added in v1.22.6

func (p *NonInteractivePrompter) CaptureDuration(promptStr string) (time.Duration, error)

func (*NonInteractivePrompter) CaptureEmail added in v1.22.6

func (p *NonInteractivePrompter) CaptureEmail(promptStr string) (string, error)

func (*NonInteractivePrompter) CaptureExistingFilepath added in v1.22.6

func (p *NonInteractivePrompter) CaptureExistingFilepath(promptStr string) (string, error)

func (*NonInteractivePrompter) CaptureFloat added in v1.22.6

func (p *NonInteractivePrompter) CaptureFloat(promptStr string, validator func(float64) error) (float64, error)

func (*NonInteractivePrompter) CaptureFujiDuration added in v1.22.6

func (p *NonInteractivePrompter) CaptureFujiDuration(promptStr string) (time.Duration, error)

func (*NonInteractivePrompter) CaptureFutureDate added in v1.22.6

func (p *NonInteractivePrompter) CaptureFutureDate(promptStr string, minDate time.Time) (time.Time, error)

func (*NonInteractivePrompter) CaptureGitURL added in v1.22.6

func (p *NonInteractivePrompter) CaptureGitURL(promptStr string) (*url.URL, error)

func (*NonInteractivePrompter) CaptureID added in v1.22.6

func (p *NonInteractivePrompter) CaptureID(promptStr string) (ids.ID, error)

func (*NonInteractivePrompter) CaptureIndex added in v1.22.6

func (p *NonInteractivePrompter) CaptureIndex(promptStr string, options []any) (int, error)

func (*NonInteractivePrompter) CaptureInt added in v1.22.6

func (p *NonInteractivePrompter) CaptureInt(promptStr string, validator func(int) error) (int, error)

func (*NonInteractivePrompter) CaptureList added in v1.22.6

func (p *NonInteractivePrompter) CaptureList(promptStr string, options []string) (string, error)

func (*NonInteractivePrompter) CaptureListWithSize added in v1.22.6

func (p *NonInteractivePrompter) CaptureListWithSize(prompt string, options []string, size int) ([]string, error)

func (*NonInteractivePrompter) CaptureMainnetDuration added in v1.22.6

func (p *NonInteractivePrompter) CaptureMainnetDuration(promptStr string) (time.Duration, error)

func (*NonInteractivePrompter) CaptureMainnetL1StakingDuration added in v1.22.6

func (p *NonInteractivePrompter) CaptureMainnetL1StakingDuration(promptStr string) (time.Duration, error)

func (*NonInteractivePrompter) CaptureNewFilepath added in v1.22.6

func (p *NonInteractivePrompter) CaptureNewFilepath(promptStr string) (string, error)

func (*NonInteractivePrompter) CaptureNoYes added in v1.22.6

func (p *NonInteractivePrompter) CaptureNoYes(promptStr string) (bool, error)

func (*NonInteractivePrompter) CaptureNodeID added in v1.22.6

func (p *NonInteractivePrompter) CaptureNodeID(promptStr string) (ids.NodeID, error)

func (*NonInteractivePrompter) CapturePChainAddress added in v1.22.6

func (p *NonInteractivePrompter) CapturePChainAddress(promptStr string, network models.Network) (string, error)

func (*NonInteractivePrompter) CapturePositiveBigInt added in v1.22.6

func (p *NonInteractivePrompter) CapturePositiveBigInt(promptStr string) (*big.Int, error)

func (*NonInteractivePrompter) CapturePositiveInt added in v1.22.6

func (p *NonInteractivePrompter) CapturePositiveInt(promptStr string, comparators []Comparator) (int, error)

func (*NonInteractivePrompter) CaptureRepoBranch added in v1.22.6

func (p *NonInteractivePrompter) CaptureRepoBranch(promptStr string, repo string) (string, error)

func (*NonInteractivePrompter) CaptureRepoFile added in v1.22.6

func (p *NonInteractivePrompter) CaptureRepoFile(promptStr string, repo string, branch string) (string, error)

func (*NonInteractivePrompter) CaptureString added in v1.22.6

func (p *NonInteractivePrompter) CaptureString(promptStr string) (string, error)

func (*NonInteractivePrompter) CaptureStringAllowEmpty added in v1.22.6

func (p *NonInteractivePrompter) CaptureStringAllowEmpty(promptStr string) (string, error)

func (*NonInteractivePrompter) CaptureURL added in v1.22.6

func (p *NonInteractivePrompter) CaptureURL(promptStr string, validateConnection bool) (string, error)

func (*NonInteractivePrompter) CaptureUint64 added in v1.22.6

func (p *NonInteractivePrompter) CaptureUint64(promptStr string) (uint64, error)

func (*NonInteractivePrompter) CaptureUint64Compare added in v1.22.6

func (p *NonInteractivePrompter) CaptureUint64Compare(promptStr string, comparators []Comparator) (uint64, error)

func (*NonInteractivePrompter) CaptureUint8 added in v1.22.6

func (p *NonInteractivePrompter) CaptureUint8(promptStr string) (uint8, error)

func (*NonInteractivePrompter) CaptureValidatedString added in v1.22.6

func (p *NonInteractivePrompter) CaptureValidatedString(promptStr string, validator func(string) error) (string, error)

func (*NonInteractivePrompter) CaptureValidatorBalance added in v1.22.6

func (p *NonInteractivePrompter) CaptureValidatorBalance(promptStr string, availableBalance float64, minBalance float64) (float64, error)

func (*NonInteractivePrompter) CaptureVersion added in v1.22.6

func (p *NonInteractivePrompter) CaptureVersion(promptStr string) (string, error)

func (*NonInteractivePrompter) CaptureWeight added in v1.22.6

func (p *NonInteractivePrompter) CaptureWeight(promptStr string, validator func(uint64) error) (uint64, error)

func (*NonInteractivePrompter) CaptureXChainAddress added in v1.22.6

func (p *NonInteractivePrompter) CaptureXChainAddress(promptStr string, network models.Network) (string, error)

func (*NonInteractivePrompter) CaptureYesNo added in v1.22.6

func (p *NonInteractivePrompter) CaptureYesNo(promptStr string) (bool, error)

func (*NonInteractivePrompter) ChooseKeyOrLedger added in v1.22.6

func (p *NonInteractivePrompter) ChooseKeyOrLedger(goal string) (bool, error)

type Prompter

type Prompter interface {
	CapturePositiveBigInt(promptStr string) (*big.Int, error)
	CaptureAddress(promptStr string) (common.Address, error)
	CaptureNewFilepath(promptStr string) (string, error)
	CaptureExistingFilepath(promptStr string) (string, error)
	CaptureYesNo(promptStr string) (bool, error)
	CaptureNoYes(promptStr string) (bool, error)
	CaptureList(promptStr string, options []string) (string, error)
	CaptureString(promptStr string) (string, error)
	CaptureGitURL(promptStr string) (*url.URL, error)
	CaptureURL(promptStr string, validateConnection bool) (string, error)
	CaptureStringAllowEmpty(promptStr string) (string, error)
	CaptureEmail(promptStr string) (string, error)
	CaptureIndex(promptStr string, options []any) (int, error)
	CaptureVersion(promptStr string) (string, error)
	CaptureDuration(promptStr string) (time.Duration, error)
	CaptureDate(promptStr string) (time.Time, error)
	CaptureNodeID(promptStr string) (ids.NodeID, error)
	CaptureID(promptStr string) (ids.ID, error)
	CaptureWeight(promptStr string, validator func(uint64) error) (uint64, error)
	CapturePositiveInt(promptStr string, comparators []Comparator) (int, error)
	CaptureUint64(promptStr string) (uint64, error)
	CaptureUint64Compare(promptStr string, comparators []Comparator) (uint64, error)
	CapturePChainAddress(promptStr string, network models.Network) (string, error)
	CaptureFutureDate(promptStr string, minDate time.Time) (time.Time, error)
	ChooseKeyOrLedger(goal string) (bool, error)
	CaptureValidatorBalance(promptStr string, availableBalance float64, minBalance float64) (float64, error)
	CaptureListWithSize(prompt string, options []string, size int) ([]string, error)
	CaptureFloat(promptStr string, validator func(float64) error) (float64, error)
	CaptureAddresses(promptStr string) ([]common.Address, error)
	CaptureXChainAddress(promptStr string, network models.Network) (string, error)
	CaptureValidatedString(promptStr string, validator func(string) error) (string, error)
	CaptureRepoBranch(promptStr string, repo string) (string, error)
	CaptureRepoFile(promptStr string, repo string, branch string) (string, error)
	CaptureInt(promptStr string, validator func(int) error) (int, error)
	CaptureUint8(promptStr string) (uint8, error)
	CaptureFujiDuration(promptStr string) (time.Duration, error)
	CaptureMainnetDuration(promptStr string) (time.Duration, error)
	CaptureMainnetL1StakingDuration(promptStr string) (time.Duration, error)
}

func NewPrompter

func NewPrompter() Prompter

NewProcessChecker creates a new process checker which can respond if the server is running

func NewPrompterForMode added in v1.22.6

func NewPrompterForMode(nonInteractiveFlag bool) Prompter

NewPrompterForMode returns the appropriate prompter based on mode.

If non-interactive, returns NonInteractivePrompter that fails fast. If interactive (TTY), returns the standard realPrompter that can prompt.

type Validator added in v1.22.6

type Validator struct {
	// contains filtered or unexported fields
}

Validator holds options being collected and tracks missing ones. Use this for clean, declarative option handling.

func NewValidator added in v1.22.6

func NewValidator(cmd string) *Validator

NewValidator creates a validator for a command.

func (*Validator) HasMissing added in v1.22.6

func (v *Validator) HasMissing() bool

HasMissing returns true if any required options are missing.

func (*Validator) Missing added in v1.22.6

func (v *Validator) Missing() []MissingOpt

Missing returns the list of missing options.

func (*Validator) Optional added in v1.22.6

func (v *Validator) Optional(target *string, defaultVal string) *Validator

Optional sets a default if the value is empty (no prompting).

func (*Validator) Require added in v1.22.6

func (v *Validator) Require(target *string, opt MissingOpt) *Validator

Require marks a value as required. If empty, adds to missing list.

func (*Validator) RequireWithDefault added in v1.22.6

func (v *Validator) RequireWithDefault(target *string, opt MissingOpt, defaultVal string) *Validator

RequireWithDefault marks a value as required with a default. Uses the default if empty and non-interactive, otherwise prompts.

func (*Validator) Resolve added in v1.22.6

func (v *Validator) Resolve(promptFn func(MissingOpt) (string, error)) error

Resolve prompts for missing values (interactive) or returns error (non-interactive).

Directories

Path Synopsis
Package comparator provides value comparison utilities for prompts.
Package comparator provides value comparison utilities for prompts.
Package mocks provides mock implementations for prompts.
Package mocks provides mock implementations for prompts.

Jump to

Keyboard shortcuts

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