params

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package params defines the typed parameter spec for spin.toml. It maps the user-facing prompt types onto huh.Field at form-build time and onto a uniform Value at render time.

Index

Constants

View Source
const PageSize = 4

PageSize is the number of params grouped per "page" in the huh form. huh renders each Group as a separate page (user navigates with Next/Prev), so this cap is also the visible pagination granularity. 4 keeps the page scannable on a 24-line terminal.

Variables

This section is empty.

Functions

func Form

func Form(ps []Param, values map[string]any) *huh.Form

Form builds a huh.Form from a slice of params. Params are grouped PageSize-at-a-time into huh.Groups; each Group becomes one form page the user can step through with Next/Prev. A single page is rendered when len(ps) <= PageSize.

func FuncMap

func FuncMap() template.FuncMap

FuncMap returns the set of template functions available to both file rendering (template.Render) and prompt/default rendering (renderStr). A template author can therefore use the same helpers (snake_case, kebab, quote, ...) in a param's prompt or default as they would in a .tmpl file. This is the single source of truth for those helpers; template/engine.go delegates to it.

func Run

func Run(ps []Param, values map[string]any) error

Run executes the form on the given params. The form populates each param's value in place. values are the currently known template values, used to render prompts/defaults.

func SetDefaults

func SetDefaults(ps []Param, values map[string]any)

SetDefaults applies each param's default value to its current value. Useful when running non-interactively. values are used to render any templated defaults.

func ShellQuote

func ShellQuote(s string) string

ShellQuote wraps s in single quotes, escaping any embedded single quotes with the standard "'='"'"' trick.

func SnakeCase

func SnakeCase(s string) string

SnakeCase converts a PascalCase/camelCase identifier to snake_case.

func ValidateDefaults

func ValidateDefaults(ps []Param) error

ValidateDefaults checks resolved param values for consistency in the non-interactive path, where huh's per-field Validate does not run. It enforces that a select param's value is one of its options: a templated default (e.g. `default = "{{ .ed }}"`) or a --param value that resolves outside the option list would otherwise pass through silently and produce a broken scaffold.

Types

type BoolParam

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

func NewBool

func NewBool(name, prompt string, def bool) *BoolParam

func (*BoolParam) Apply

func (p *BoolParam) Apply(v Value)

func (*BoolParam) Default

func (p *BoolParam) Default() any

func (*BoolParam) HuhField

func (p *BoolParam) HuhField(values map[string]any) huh.Field

func (*BoolParam) Name

func (p *BoolParam) Name() string

func (*BoolParam) Prompt

func (p *BoolParam) Prompt() string

func (*BoolParam) SetDefault

func (p *BoolParam) SetDefault(values map[string]any)

func (*BoolParam) String

func (p *BoolParam) String() string

func (*BoolParam) Type

func (p *BoolParam) Type() Type

func (*BoolParam) Value

func (p *BoolParam) Value() Value

type ErrUnknownType

type ErrUnknownType struct {
	Name string
	Type Type
}

ErrUnknownType is returned when a param declares a type we don't recognise.

func (ErrUnknownType) Error

func (e ErrUnknownType) Error() string

type MultiSelectParam

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

func NewMultiSelect

func NewMultiSelect(name, prompt string, options, def []string) *MultiSelectParam

func (*MultiSelectParam) Apply

func (p *MultiSelectParam) Apply(v Value)

func (*MultiSelectParam) Default

func (p *MultiSelectParam) Default() any

func (*MultiSelectParam) HuhField

func (p *MultiSelectParam) HuhField(values map[string]any) huh.Field

func (*MultiSelectParam) Name

func (p *MultiSelectParam) Name() string

func (*MultiSelectParam) Prompt

func (p *MultiSelectParam) Prompt() string

func (*MultiSelectParam) SetDefault

func (p *MultiSelectParam) SetDefault(values map[string]any)

func (*MultiSelectParam) String

func (p *MultiSelectParam) String() string

func (*MultiSelectParam) Type

func (p *MultiSelectParam) Type() Type

func (*MultiSelectParam) Value

func (p *MultiSelectParam) Value() Value

type NumberParam

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

NumberParam has no dedicated huh type; we use NewInput + a Validate that parses the input as int and applies Min/Max bounds. The string backing field (valueStr) is what huh writes into via Value(&p.valueStr); the numeric value is derived on demand from Value() and from SetDefaults.

func NewNumber

func NewNumber(name, prompt string, def int, min, max *int) *NumberParam

func (*NumberParam) Apply

func (p *NumberParam) Apply(v Value)

func (*NumberParam) Default

func (p *NumberParam) Default() any

func (*NumberParam) HuhField

func (p *NumberParam) HuhField(values map[string]any) huh.Field

func (*NumberParam) Name

func (p *NumberParam) Name() string

func (*NumberParam) Prompt

func (p *NumberParam) Prompt() string

func (*NumberParam) SetDefault

func (p *NumberParam) SetDefault(values map[string]any)

func (*NumberParam) String

func (p *NumberParam) String() string

func (*NumberParam) Type

func (p *NumberParam) Type() Type

func (*NumberParam) Value

func (p *NumberParam) Value() Value

type Param

type Param interface {
	Name() string
	Type() Type
	Prompt() string
	Default() any
	SetDefault(values map[string]any)
	Apply(v Value)
	Value() Value
	// HuhField builds the huh field for this param. The form runner
	// writes the result back via Apply(). values are the currently
	// known template values, used to render the param's prompt and
	// default (e.g. `prompt = "Name for {{ .name }}"`).
	HuhField(values map[string]any) huh.Field
	// String returns a one-line summary, used by `spin new --print-params`.
	String() string
}

Param is the interface implemented by every typed parameter.

func Parse

func Parse(specs SpecMap) ([]Param, error)

Parse turns a SpecMap into a list of typed Param instances, sorted alphabetically for consistent display order.

func ParseOne

func ParseOne(name string, s Spec) (Param, error)

ParseOne builds a single Param from a name and Spec.

type PathParam

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

PathParam is a huh.NewFilePicker; default is used as the placeholder starting directory (or filename if non-existent).

func NewDir

func NewDir(name, prompt, def string) *PathParam

func NewPath

func NewPath(name, prompt, def string) *PathParam

func (*PathParam) Apply

func (p *PathParam) Apply(v Value)

func (*PathParam) Default

func (p *PathParam) Default() any

func (*PathParam) HuhField

func (p *PathParam) HuhField(values map[string]any) huh.Field

func (*PathParam) Name

func (p *PathParam) Name() string

func (*PathParam) Prompt

func (p *PathParam) Prompt() string

func (*PathParam) SetDefault

func (p *PathParam) SetDefault(values map[string]any)

func (*PathParam) String

func (p *PathParam) String() string

func (*PathParam) Type

func (p *PathParam) Type() Type

func (*PathParam) Value

func (p *PathParam) Value() Value

type SecretParam

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

SecretParam is a huh.Input with EchoModePassword.

func NewSecret

func NewSecret(name, prompt string) *SecretParam

func (*SecretParam) Apply

func (p *SecretParam) Apply(v Value)

func (*SecretParam) Default

func (p *SecretParam) Default() any

func (*SecretParam) HuhField

func (p *SecretParam) HuhField(values map[string]any) huh.Field

func (*SecretParam) Name

func (p *SecretParam) Name() string

func (*SecretParam) Prompt

func (p *SecretParam) Prompt() string

func (*SecretParam) SetDefault

func (p *SecretParam) SetDefault(values map[string]any)

func (*SecretParam) String

func (p *SecretParam) String() string

func (*SecretParam) Type

func (p *SecretParam) Type() Type

func (*SecretParam) Value

func (p *SecretParam) Value() Value

type SelectParam

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

func NewSelect

func NewSelect(name, prompt string, options []string, def string) *SelectParam

func (*SelectParam) Apply

func (p *SelectParam) Apply(v Value)

func (*SelectParam) Default

func (p *SelectParam) Default() any

func (*SelectParam) HuhField

func (p *SelectParam) HuhField(values map[string]any) huh.Field

func (*SelectParam) Name

func (p *SelectParam) Name() string

func (*SelectParam) Prompt

func (p *SelectParam) Prompt() string

func (*SelectParam) SetDefault

func (p *SelectParam) SetDefault(values map[string]any)

func (*SelectParam) String

func (p *SelectParam) String() string

func (*SelectParam) Type

func (p *SelectParam) Type() Type

func (*SelectParam) Value

func (p *SelectParam) Value() Value

type Spec

type Spec struct {
	Type    Type     `toml:"type"`
	Prompt  string   `toml:"prompt"`
	Default any      `toml:"default"`
	Min     *int     `toml:"min"`
	Max     *int     `toml:"max"`
	Options []string `toml:"options"`
}

Spec is the raw shape we accept from a parsed spin.toml block. The parse step turns a Spec into a concrete Param.

type SpecMap

type SpecMap map[string]Spec

SpecMap is a raw spin.toml `params` block: param name → Spec.

type TextParam

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

func NewText

func NewText(name, prompt, def string) *TextParam

func (*TextParam) Apply

func (p *TextParam) Apply(v Value)

func (*TextParam) Default

func (p *TextParam) Default() any

func (*TextParam) HuhField

func (p *TextParam) HuhField(values map[string]any) huh.Field

func (*TextParam) Name

func (p *TextParam) Name() string

func (*TextParam) Prompt

func (p *TextParam) Prompt() string

func (*TextParam) SetDefault

func (p *TextParam) SetDefault(values map[string]any)

func (*TextParam) String

func (p *TextParam) String() string

func (*TextParam) Type

func (p *TextParam) Type() Type

func (*TextParam) Value

func (p *TextParam) Value() Value

type TextareaParam

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

func NewTextarea

func NewTextarea(name, prompt, def string) *TextareaParam

func (*TextareaParam) Apply

func (p *TextareaParam) Apply(v Value)

func (*TextareaParam) Default

func (p *TextareaParam) Default() any

func (*TextareaParam) HuhField

func (p *TextareaParam) HuhField(values map[string]any) huh.Field

func (*TextareaParam) Name

func (p *TextareaParam) Name() string

func (*TextareaParam) Prompt

func (p *TextareaParam) Prompt() string

func (*TextareaParam) SetDefault

func (p *TextareaParam) SetDefault(values map[string]any)

func (*TextareaParam) String

func (p *TextareaParam) String() string

func (*TextareaParam) Type

func (p *TextareaParam) Type() Type

func (*TextareaParam) Value

func (p *TextareaParam) Value() Value

type Type

type Type string

Type is the wire name used in spin.toml (e.g. `type = "text"`).

const (
	TypeText        Type = "text"
	TypeTextarea    Type = "textarea"
	TypeNumber      Type = "number"
	TypeSelect      Type = "select"
	TypeMultiSelect Type = "multiselect"
	TypeBool        Type = "bool"
	TypePath        Type = "path"
	TypeSecret      Type = "secret"
)

type Value

type Value struct {
	Kind   Type
	String string
	Int    int
	Bool   bool
	List   []string
	Path   string
}

Value is the resolved value of a Param after the form has run. Only one of the data fields is populated, depending on Kind.

func FromAny

func FromAny(v any) Value

FromAny converts a raw CLI/default value (string, int, bool, []string, []any) into a params.Value suitable for Param.Apply. The Value's Kind is derived from the Go type; each param's Apply picks the field it cares about, so a string maps cleanly onto text, select, textarea, secret and path params.

Jump to

Keyboard shortcuts

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