forms

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Overview

Package forms implements interactive terminal forms that collect user input and produce structured data. Forms are defined as YAML documents containing typed properties (string, bool, integer, float, password, object, array) that are presented to the user interactively. Properties support conditionals, validation expressions, enums, defaults, and nested sub-properties.

The collected answers are assembled into a map[string]any result using an internal entry tree (see graph.go) that supports querying partially-built results, enabling conditional properties that reference earlier answers.

Index

Constants

View Source
const (
	ArrayIfEmpty  = "array"  // emit an empty array
	ObjectIfEmpty = "object" // emit an empty object
	AbsentIfEmpty = "absent" // omit the key entirely
)

IfEmpty constants control what value is emitted when a property answer is empty.

View Source
const (
	StringType   = "string"
	BoolType     = "bool"
	IntType      = "integer"
	FloatType    = "float"
	PasswordType = "password"
	ObjectType   = "object"
	ArrayType    = "array"
)

Type constants identify property types in form definitions.

Variables

This section is empty.

Functions

func ProcessBytes

func ProcessBytes(f []byte, env map[string]any, opts ...processOption) (map[string]any, error)

ProcessBytes unmarshals f as a YAML form definition and processes it interactively.

func ProcessFile

func ProcessFile(f string, env map[string]any, opts ...processOption) (map[string]any, error)

ProcessFile reads YAML form data from the file at path f and processes it interactively.

func ProcessForm

func ProcessForm(f Form, env map[string]any, opts ...processOption) (map[string]any, error)

ProcessForm presents the form interactively on a terminal and returns the collected answers as a map. It requires a valid terminal (stdin and stdout). The env map provides template variables for property descriptions and conditional expressions.

func ProcessReader

func ProcessReader(r io.Reader, env map[string]any, opts ...processOption) (map[string]any, error)

ProcessReader reads YAML form data from r and processes it interactively.

Types

type Form

type Form struct {
	Name        string     `json:"name" yaml:"name"`
	Description string     `json:"description" yaml:"description"`
	Properties  []Property `json:"properties" yaml:"properties"`
}

Form defines an interactive form with a name, description, and a list of properties to present to the user. The Description supports Go template syntax with Sprig functions and color markup tags like {red}text{/red}.

type Property

type Property struct {
	Name                  string     `json:"name" yaml:"name"`
	Description           string     `json:"description" yaml:"description"`
	Help                  string     `json:"help" yaml:"help"`
	IfEmpty               string     `json:"empty" yaml:"empty"`
	Type                  string     `json:"type" yaml:"type"`
	ConditionalExpression string     `json:"conditional" yaml:"conditional"`
	ValidationExpression  string     `json:"validation" yaml:"validation"`
	Required              bool       `json:"required" yaml:"required"`
	Default               string     `json:"default" yaml:"default"`
	Enum                  []string   `json:"enum" yaml:"enum"`
	Properties            []Property `json:"properties" yaml:"properties"`
}

Property defines a single form field. Type determines the input method (string, bool, integer, float, password, object, array). Properties with sub-Properties create nested structures. ConditionalExpression is a validation expression evaluated against the current environment and collected input to decide whether to present this property.

func (*Property) RenderedDescription

func (p *Property) RenderedDescription(env map[string]any) (string, error)

RenderedDescription executes the property's Description as a Go template with Sprig functions against env, then applies color markup to the result.

Jump to

Keyboard shortcuts

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