interactive

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package interactive builds request bodies by prompting the user for each field of a Go struct via reflection. Input is gathered through the Prompter interface so the traversal can be unit-tested without a terminal.

Index

Constants

View Source
const DefaultMaxDepth = 20

DefaultMaxDepth bounds recursion to defeat self-referential unions. When the guard fires the builder degrades to a raw-JSON prompt.

View Source
const DefaultParamBagThreshold = 15

DefaultParamBagThreshold is the optional-field count above which a struct is treated as a parameter bag (the user multi-selects which fields to set).

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	Prompter          Prompter
	MaxDepth          int // 0 uses DefaultMaxDepth
	ParamBagThreshold int // 0 uses DefaultParamBagThreshold
}

Builder prompts for every field of a struct via reflection.

func (*Builder) Build

func (b *Builder) Build(v any) error

Build reflectively prompts for every field of the struct pointed to by v. v must be a non-nil pointer to a struct. Fields already non-zero on v are preserved (lets the caller pre-populate identifiers).

type Prompter

type Prompter interface {
	// Input reads a free-text line. validate, when non-nil, is run on the entry;
	// the implementation re-prompts until it passes (SurveyPrompter) or surfaces
	// its error (ScriptedPrompter). Pass nil for no validation. An empty entry
	// means "skip" for optional fields and zero for required scalars.
	Input(label string, validate func(string) error) (string, error)
	// Confirm asks a yes/no question.
	Confirm(label string) (bool, error)
	// Select asks the user to pick exactly one option and returns the chosen
	// label.
	Select(label string, options []string) (string, error)
	// MultiSelect asks the user to pick zero or more options and returns the
	// chosen 0-based indexes.
	MultiSelect(label string, options []string) ([]int, error)
}

Prompter is the input surface used by the reflective Builder. Production code uses SurveyPrompter; tests use a scripted fake.

type ScriptedPrompter

type ScriptedPrompter struct {
	Inputs       map[string]string
	Confirms     map[string]bool
	Selects      map[string]string
	MultiSelects map[string][]int
}

ScriptedPrompter is a deterministic Prompter for tests. Answers are keyed by a substring of the prompt label rather than by call order, so a test does not break when the target struct (for example a generated SDK type) adds or reorders fields. A prompt whose label matches no key returns a safe default: empty input, a false confirm, the first option, or no multi-selection. Unknown or newly added fields are therefore simply skipped.

Matching rule: a key matches a label when the label equals the key or contains it as a substring. An exact match wins over any substring match; among substring matches the longest (most specific) key wins, so the result is deterministic regardless of map iteration order.

func (*ScriptedPrompter) Confirm

func (p *ScriptedPrompter) Confirm(label string) (bool, error)

func (*ScriptedPrompter) Input

func (p *ScriptedPrompter) Input(label string, validate func(string) error) (string, error)

func (*ScriptedPrompter) MultiSelect

func (p *ScriptedPrompter) MultiSelect(label string, options []string) ([]int, error)

func (*ScriptedPrompter) Select

func (p *ScriptedPrompter) Select(label string, options []string) (string, error)

type SurveyPrompter

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

SurveyPrompter implements Prompter using survey/v2 via the repo's pkg/prompt wrappers (which are swappable in tests).

func NewSurveyPrompter

func NewSurveyPrompter(io *iostreams.IOStreams) *SurveyPrompter

NewSurveyPrompter returns a Prompter that reads and writes the given streams.

func (*SurveyPrompter) Confirm

func (s *SurveyPrompter) Confirm(label string) (bool, error)

func (*SurveyPrompter) Input

func (s *SurveyPrompter) Input(label string, validate func(string) error) (string, error)

func (*SurveyPrompter) MultiSelect

func (s *SurveyPrompter) MultiSelect(label string, options []string) ([]int, error)

func (*SurveyPrompter) Select

func (s *SurveyPrompter) Select(label string, options []string) (string, error)

Jump to

Keyboard shortcuts

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