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
- type Builder
- type Prompter
- type ScriptedPrompter
- func (p *ScriptedPrompter) Confirm(label string) (bool, error)
- func (p *ScriptedPrompter) Input(label string, validate func(string) error) (string, error)
- func (p *ScriptedPrompter) MultiSelect(label string, options []string) ([]int, error)
- func (p *ScriptedPrompter) Select(label string, options []string) (string, error)
- type SurveyPrompter
- func (s *SurveyPrompter) Confirm(label string) (bool, error)
- func (s *SurveyPrompter) Input(label string, validate func(string) error) (string, error)
- func (s *SurveyPrompter) MultiSelect(label string, options []string) ([]int, error)
- func (s *SurveyPrompter) Select(label string, options []string) (string, error)
Constants ¶
const DefaultMaxDepth = 20
DefaultMaxDepth bounds recursion to defeat self-referential unions. When the guard fires the builder degrades to a raw-JSON prompt.
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.
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) MultiSelect ¶
func (p *ScriptedPrompter) MultiSelect(label string, options []string) ([]int, 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) MultiSelect ¶
func (s *SurveyPrompter) MultiSelect(label string, options []string) ([]int, error)