ui

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2019 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Overview

Package ui provides a simple way to interact with the user through the terminal, i.e. the interface of a CLI.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrCannotAsk occurs when prompting for input is impossible.
	ErrCannotAsk = askErr.Code("cannot_ask_for_input").Error("Cannot ask for interactive input.\n\n" +
		"This usually happens when you run something non-interactively that needs to ask interactive questions.")
	ErrPassphrasesDoNotMatch = askErr.Code("passphrase_does_not_match").Error("passphrases do not match")
)

Errors

View Source
var (
	ErrReadInput = errRead.Code("read_input").ErrorPref("could not read input: %s")
)

Errors

Functions

func Ask

func Ask(io IO, question string) (string, error)

Ask prints out the question and reads the first line of input.

func AskAndValidate

func AskAndValidate(io IO, question string, n int, validationFunc func(string) error) (string, error)

AskAndValidate asks the user a question and re-prompts the configured amount of times when the users answer does not validate.

func AskPassphrase

func AskPassphrase(io IO, question string, repeatPhrase string, n int) (string, error)

AskPassphrase asks for a password and then asks to type it again for confirmation. When the user types two different passphrases, he is asked again. When the answers still haven't matched after trying n times, the error ErrPassphrasesDoNotMatch is returned. For the empty answer ("") no confirmation is asked.

func AskSecret

func AskSecret(io IO, question string) (string, error)

AskSecret prints out the question and reads back the input, without echoing it back. Useful for passwords and other sensitive inputs.

func AskYesNo

func AskYesNo(io IO, question string, t ConfirmationType) (bool, error)

AskYesNo asks the user for confirmation. A user must type in "yes" or "no" and then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as confirmations. If no input is given, it will return true with DefaultYes and false with DefaultNo. If the input is not recognized, it will ask again. The function retries 3 times. If it still has no valid response after that, it returns false.

func Choose added in v0.26.0

func Choose(io IO, question string, getOptions func() ([]Option, bool, error), addOwn bool, optionName string) (string, error)

func ConfirmCaseInsensitive

func ConfirmCaseInsensitive(io IO, question string, expected ...string) (bool, error)

ConfirmCaseInsensitive asks the user to confirm by typing one of the expected strings. The comparison is not case-sensitive. If multiple values for expected are given, true is returned if the input equals any of the the expected values.

func Readln

func Readln(r io.Reader) (string, error)

Readln reads 1 line of input from a io.Reader. The newline character is not included in the response.

Types

type ConfirmationType

type ConfirmationType int

ConfirmationType defines what AskYesNo uses as the default answer.

const (
	// DefaultNone assumes no default [y/n]
	DefaultNone ConfirmationType = iota
	// DefaultNo assumes no as the default answer [y/N]
	DefaultNo
	// DefaultYes assumes yes as the default answer [Y/n]
	DefaultYes
)

type FakeIO

type FakeIO struct {
	StdIn     *FakeReader
	StdOut    *FakeWriter
	PromptIn  *FakeReader
	PromptOut *FakeWriter
	PromptErr error
}

FakeIO is a helper type for testing that implements the ui.IO interface

func NewFakeIO

func NewFakeIO() *FakeIO

NewFakeIO creates a new FakeIO with empty buffers.

func (*FakeIO) Prompts

func (f *FakeIO) Prompts() (Reader, Writer, error)

Prompts returns the mocked prompts and error.

func (*FakeIO) Stdin

func (f *FakeIO) Stdin() Reader

Stdin returns the mocked StdIn.

func (*FakeIO) Stdout

func (f *FakeIO) Stdout() Writer

Stdout returns the mocked StdOut.

type FakeReader

type FakeReader struct {
	*bytes.Buffer
	Piped bool

	Reads   []string
	ReadErr error
	// contains filtered or unexported fields
}

FakeReader implements the Reader interface.

func (*FakeReader) IsPiped

func (f *FakeReader) IsPiped() bool

IsPiped returns the mocked Piped.

func (*FakeReader) Read

func (f *FakeReader) Read(p []byte) (n int, err error)

Read returns the mocked ReadErr or reads from the mocked buffer.

func (*FakeReader) ReadPassword

func (f *FakeReader) ReadPassword() ([]byte, error)

ReadPassword reads a line from the mocked buffer.

type FakeWriter

type FakeWriter struct {
	*bytes.Buffer
	Piped bool
}

FakeWriter implements the Writer interface.

func (*FakeWriter) IsPiped

func (f *FakeWriter) IsPiped() bool

IsPiped returns the mocked Piped.

type IO

type IO interface {
	Stdin() Reader
	Stdout() Writer
	Prompts() (Reader, Writer, error)
}

IO is an interface to work with input/output.

type Option added in v0.26.0

type Option struct {
	Value   string
	Display string
}

func (Option) String added in v0.26.0

func (o Option) String() string

type Reader

type Reader interface {
	io.Reader
	// ReadPassword reads a line of input from a terminal without local echo.
	ReadPassword() ([]byte, error)
	IsPiped() bool
}

Reader can read input for a CLI program.

type UserIO

type UserIO struct {
	Input  Reader
	Output Writer
	// contains filtered or unexported fields
}

UserIO is a middleware between input and output to the CLI program. It implements userIO.Prompter and can be passed to libraries.

func NewStdUserIO

func NewStdUserIO() UserIO

NewStdUserIO creates a new UserIO middleware only from os.Stdin and os.Stdout.

func NewUserIO

func NewUserIO() UserIO

NewUserIO creates a new UserIO middleware from os.Stdin and os.Stdout and adds tty if it is available.

func (UserIO) Prompts

func (o UserIO) Prompts() (Reader, Writer, error)

Prompts simply returns Stdin and Stdout, when both input and output are not piped. When either input or output is piped, Prompts attempts to bypass stdin and stdout by connecting to /dev/tty on Unix systems when available. On systems where tty is not available and when either input or output is piped, prompting is not possible so an error is returned.

func (UserIO) Stdin

func (o UserIO) Stdin() Reader

Stdin returns the UserIO's Input.

func (UserIO) Stdout

func (o UserIO) Stdout() Writer

Stdout returns the UserIO's Output.

type Writer

type Writer interface {
	io.Writer
	IsPiped() bool
}

Writer can write output for a CLI program.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳