form

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: LGPL-3.0 Imports: 9 Imported by: 39

Documentation

Overview

Package form provides ways to create and process forms based on environschema schemas.

The API exposed by this package is not currently subject to the environschema.v1 API compatibility guarantees.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFromEnv

func DefaultFromEnv(attr NamedAttr, checker schema.Checker) (val interface{}, _ string, err error)

DefaultFromEnv returns any default value found in the environment for the given attribute.

The environment variables specified in attr will be checked in order and the first non-empty value found is coerced using the given checker and returned.

Types

type Filler

type Filler interface {
	Fill(f Form) (map[string]interface{}, error)
}

Filler represents an object that can fill out a Form. The the form is described in f. The returned value should be compatible with the schema defined in f.Fields.

type Form

type Form struct {
	// Title holds the title of the form, giving contextual
	// information for the fields.
	Title string

	// Fields holds the fields that make up the body of the form.
	Fields environschema.Fields
}

Form describes a form based on a schema.

type IOFiller

type IOFiller struct {
	// In is used to read responses from the user. If this is nil,
	// then os.Stdin will be used.
	In io.Reader

	// Out is used to write prompts and information to the user. If
	// this is nil, then os.Stdout will be used.
	Out io.Writer

	// MaxTries is the number of times to attempt to get a valid
	// response when prompting. If this is 0 then the default of 3
	// attempts will be used.
	MaxTries int

	// ShowDescriptions holds whether attribute descriptions
	// should be printed as well as the attribute names.
	ShowDescriptions bool

	// GetDefault returns the default value for the given attribute,
	// which must have been coerced using the given checker.
	// If there is no default, it should return (nil, "", nil).
	//
	// The display return value holds the string to use
	// to describe the value of the default. If it's empty,
	// fmt.Sprint(val) will be used.
	//
	// If GetDefault returns an error, it will be printed as a warning.
	//
	// If GetDefault is nil, DefaultFromEnv will be used.
	GetDefault func(attr NamedAttr, checker schema.Checker) (val interface{}, display string, err error)
}

IOFiller is a Filler based around an io.Reader and io.Writer.

func (IOFiller) Fill

func (f IOFiller) Fill(form Form) (map[string]interface{}, error)

Fill implements Filler.Fill by writing the field information to f.Out, then reading input from f.In. If f.In is a terminal and the attribute is secret, echo will be disabled.

Fill processes fields by first sorting them and then prompting for the value of each one in turn.

The fields are sorted by first by group name. Those in the same group are sorted so that secret fields come after non-secret ones, finally the fields are sorted by description.

Each field will be prompted for, then the returned value will be validated against the field's type. If the returned value does not validate correctly it will be prompted again up to MaxTries before giving up.

type NamedAttr

type NamedAttr struct {
	Name string
	environschema.Attr
}

NamedAttr associates a name with an environschema.Field.

func SortedFields

func SortedFields(fields environschema.Fields) []NamedAttr

SortedFields returns the given fields sorted first by group name. Those in the same group are sorted so that secret fields come after non-secret ones, finally the fields are sorted by name.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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