interact

package
v0.0.0-...-9ec3720 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package interact provides helper methods for interacting with the CLI user at command run time.

Index

Constants

View Source
const FormatCertFilename jsonschema.Format = "cert-filename"

Non standard json Format

Variables

This section is empty.

Functions

func FindMatch

func FindMatch(s string, options []string) (match string, found bool)

FindMatch does a case-insensitive search of the given options and returns the matching option. Found reports whether s was found in the options.

func NewErrWriter

func NewErrWriter(w io.Writer) io.Writer

NewErrWriter wraps w in a type that will cause all writes to be written as ansi terminal BrightRed.

func QueryVerify

func QueryVerify(question string, scanner *bufio.Scanner, out, errOut io.Writer, verify VerifyFunc) (answer string, err error)

QueryVerify writes a question to w and waits for an answer to be read from scanner. It will pass the answer into the verify function. Verify, if non-nil, should check the answer for validity, returning an error that will be written out to errOut, or nil if answer is valid.

This function takes a scanner rather than an io.Reader to avoid the case where the scanner reads past the delimiter and thus might lose data. It is expected that this method will be used repeatedly with the same scanner if multiple queries are required.

Types

type List

type List struct {
	Singular string
	Plural   string
	Options  []string
	Default  string
}

List contains the information necessary to ask the user to select one item from a list of options.

type MultiList

type MultiList struct {
	Singular string
	Plural   string
	Options  []string
	Default  []string
}

MultiList contains the information necessary to ask the user to select from a list of options.

type Pollster

type Pollster struct {
	VerifyURLs     VerifyFunc
	VerifyCertFile VerifyFunc
	// contains filtered or unexported fields
}

Pollster is used to ask multiple questions of the user using a standard formatting.

func New

func New(in io.Reader, out, errOut io.Writer) *Pollster

New returns a Pollster that wraps the given reader and writer.

func (*Pollster) Enter

func (p *Pollster) Enter(valueName string) (string, error)

Enter requests that the user enter a value. Any value except an empty string is accepted.

func (*Pollster) EnterDefault

func (p *Pollster) EnterDefault(valueName, defVal string) (string, error)

EnterDefault requests that the user enter a value. Any value is accepted. An empty string is treated as defVal.

func (*Pollster) EnterOptional

func (p *Pollster) EnterOptional(valueName string) (string, error)

EnterOptional requests that the user enter a value. It accepts any value, even an empty string.

func (*Pollster) EnterPassword

func (p *Pollster) EnterPassword(valueName string) (string, error)

EnterPassword works like Enter except that if the pollster's input wraps a terminal, the user's input will be read without local echo.

func (*Pollster) EnterVerify

func (p *Pollster) EnterVerify(valueName string, verify VerifyFunc) (string, error)

EnterVerify requests that the user enter a value. Values failing to verify will be rejected with the error message returned by verify. A nil verify function will accept any value (even an empty string).

func (*Pollster) EnterVerifyDefault

func (p *Pollster) EnterVerifyDefault(valueName string, verify VerifyFunc, defVal string) (string, error)

EnterVerifyDefault requests that the user enter a value. Values failing to verify will be rejected with the error message returned by verify. An empty string will be accepted as the default value even if it would fail verification.

func (*Pollster) MultiSelect

func (p *Pollster) MultiSelect(l MultiList) ([]string, error)

MultiSelect queries the user to select one more answers from the given list of options by entering values delimited by commas (and thus options must not contain commas).

func (*Pollster) QuerySchema

func (p *Pollster) QuerySchema(schema *jsonschema.Schema) (interface{}, error)

QuerySchema takes a jsonschema and queries the user to input value(s) for the schema. It returns an object as defined by the schema (generally a map[string]interface{} for objects, etc).

func (*Pollster) Select

func (p *Pollster) Select(l List) (string, error)

Select queries the user to select from the given list of options.

func (*Pollster) SelectVerify

func (p *Pollster) SelectVerify(l List, verify VerifyFunc) (string, error)

SelectVerify queries the user to select from the given list of options, verifying the choice by passing responses through verify.

func (*Pollster) YN

func (p *Pollster) YN(q string, defVal bool) (bool, error)

YN queries the user with a yes no question q (which should not include a question mark at the end). It uses defVal as the default answer.

type VerifyFunc

type VerifyFunc func(s string) (ok bool, errmsg string, err error)

VerifyFunc is a type that determines whether a value entered by the user is acceptable or not. If it returns an error, the calling func will return an error, and the other return values are ignored. If ok is true, the value is acceptable, and that value will be returned by the calling function. If ok is false, the user will be asked to enter a new value for query. If ok is false. if errmsg is not empty, it will be printed out as an error to te the user.

func MatchOptions

func MatchOptions(options []string, errmsg string) VerifyFunc

MatchOptions returns a function that performs a case insensitive comparison against the given list of options. To make a verification function that accepts an empty default, include an empty string in the list.

func VerifyOptions

func VerifyOptions(singular string, options []string, hasDefault bool) VerifyFunc

VerifyOptions is the verifier used by pollster.Select.

Jump to

Keyboard shortcuts

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