surveyexpect

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 16 Imported by: 0

README

Expects for AlecAivazis/survey

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

surveyexpect is an Expect library for AlecAivazis/survey/v2

Prerequisites

  • Go >= 1.16

Install

go get github.com/nhatthm/surveyexpect

Usage

Supported Types
Type Supported Supported Actions
Confirm
  • Answer yes, no or a custom one
  • Interrupt (^C)
  • Ask for help
Editor There is no plan for support
Input
  • Answer
  • No answer
  • Suggestions with navigation (Arrow Up , Arrow Down , Tab , Esc , Enter )
  • Interrupt (^C)
  • Ask for help
Multiline
  • Answer
  • No answer
  • Interrupt (^C)
Multiselect
  • Type to filter, delete
  • Navigation (Move Up , Move Down , Select None , Select All , Tab , Enter )
  • Interrupt (^C)
  • Ask for help
Password
  • Answer (+ check for *)
  • No answer
  • Interrupt (^C)
  • Ask for help
Select
  • Type to filter, delete
  • Navigation (Move Up , Move Down , Tab , Enter )
  • Interrupt (^C)
  • Ask for help
Expect

There are 2 steps:

Step 1: Create an expectation.

Call surveyexpect.Expect()

s := surveyexpect.Expect(func(s *surveyexpect.Survey) {
    s.ExpectPassword("Enter a password:").
        Answer("secret")
})(t) // t is *testing.T

Step 2: Run it.

Important: Use the stdio arg and inject it into the survey.Prompt otherwise it won't work.

s.Start(func(stdio terminal.Stdio)) {
    // For example
    p := &survey.Password{Message: "Enter a password:"}
    var answer string
    err := survey.AskOne(p, &answer, surveyexpect.WithStdio(stdio))

    // Asserts.
    assert.Equal(t, "123456", answer)
    assert.NoError(t, err)
})

Examples

package mypackage_test

import (
	"testing"

	"github.com/AlecAivazis/survey/v2"
	"github.com/AlecAivazis/survey/v2/terminal"
	"github.com/nhatthm/surveyexpect"
	"github.com/stretchr/testify/assert"
)

func TestMyPackage(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		scenario       string
		expectSurvey   surveyexpect.Expector
		expectedAnswer string
		expectedError  string
	}{
		{
			scenario: "empty answer",
			expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
				s.ExpectPassword("Enter a password:").
					Answer("")
			}),
		},
		{
			scenario: "password without help",
			expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
				s.ExpectPassword("Enter a password:").
					Answer("secret")
			}),
			expectedAnswer: "secret",
		},

		{
			scenario: "input is interrupted",
			expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
				s.ExpectPassword("Enter a password:").
					Interrupt()
			}),
			expectedError: "interrupt",
		},
	}

	for _, tc := range testCases {
		tc := tc
		t.Run(tc.scenario, func(t *testing.T) {
			t.Parallel()

			p := &survey.Password{Message: "Enter a password:"}

			// Start the survey.
			tc.expectSurvey(t).Start(func(stdio terminal.Stdio) {
				// Run your logic here.
				// For example.
				var answer string
				err := survey.AskOne(p, &answer, surveyexpect.WithStdio(stdio))

				assert.Equal(t, tc.expectedAnswer, answer)

				if tc.expectedError == "" {
					assert.NoError(t, err)
				} else {
					assert.EqualError(t, err, tc.expectedError)
				}
			})
		})
	}
}

You can find more examples in the tests of this library:

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package surveyexpect provides expects for github.com/AlecAivazis/survey.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNothingToDo indicates that there is nothing to do.
	ErrNothingToDo = errors.New("nothing to do")
	// ErrNotFinished indicates that the step is not finished.
	ErrNotFinished = errors.New("step is not finished")
	// ErrSequenceClosed indicates that the step is closed and does not take more action.
	ErrSequenceClosed = errors.New("sequence is closed")
)
View Source
var ReactionTime = 10 * time.Millisecond

ReactionTime is to create a small delay to simulate human reaction.

Functions

func IsIgnoredError added in v0.4.0

func IsIgnoredError(err error) bool

IsIgnoredError checks whether the error is ignored.

func IsInterrupted added in v0.4.0

func IsInterrupted(err error) bool

IsInterrupted checks if the error is terminal.InterruptErr or not.

func IsNothingTodo added in v0.4.0

func IsNothingTodo(err error) bool

IsNothingTodo checks if the error is ErrNothingToDo or not.

func WaitForReaction added in v0.5.4

func WaitForReaction() <-chan time.Time

WaitForReaction creates a small delay to simulate human reaction.

Types

type Action added in v0.5.0

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

Action sends an action.

func (*Action) Do added in v0.5.0

func (a *Action) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*Action) String added in v0.5.0

func (a *Action) String() string

String represents the answer as a string.

type Answer

type Answer interface {
	Step
}

Answer is an expectation for answering a question.

type Buffer

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

Buffer is a goroutine safe bytes.Buffer.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the same as Truncate(0).

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string. If the Buffer is a nil pointer, it returns "<nil>".

To build strings more efficiently, see the strings.Builder type.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.

type ConfirmAnswer

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

ConfirmAnswer is an answer for confirm question.

func (*ConfirmAnswer) Do added in v0.4.0

func (a *ConfirmAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*ConfirmAnswer) Interrupted

func (a *ConfirmAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*ConfirmAnswer) String

func (a *ConfirmAnswer) String() string

String represents the answer as a string.

type ConfirmPrompt added in v0.4.0

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

ConfirmPrompt is an expectation of survey.Confirm.

func (*ConfirmPrompt) Answer added in v0.4.0

func (c *ConfirmPrompt) Answer(answer string) *ConfirmAnswer

Answer sets a custom answer to the prompt.

If the answer is not not empty, the survey expects to have a feedback from the prompt:

`Sorry, your reply was invalid: "hello world!" is not a valid answer, please try again.`

Survey.ExpectConfirm("Are you sure to delete this file?").
	Answer("hello world!").

func (*ConfirmPrompt) Do added in v0.4.0

func (c *ConfirmPrompt) Do(console Console) error

Do runs the step.

func (*ConfirmPrompt) Interrupt added in v0.4.0

func (c *ConfirmPrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectConfirm("Are you sure to delete this file?").
	Interrupt().

func (*ConfirmPrompt) No added in v0.4.0

func (c *ConfirmPrompt) No()

No sets "no" as the answer to the prompt.

Survey.ExpectConfirm("Are you sure to delete this file?").
	No().

func (*ConfirmPrompt) ShowHelp added in v0.4.0

func (c *ConfirmPrompt) ShowHelp(help string, options ...string)

ShowHelp sets help for the expectation.

Survey.ExpectConfirm("Are you sure to delete this file?").
	ShowHelp("The file will be permanently deleted").

func (*ConfirmPrompt) String added in v0.4.0

func (c *ConfirmPrompt) String() string

String represents the expectation as a string.

func (*ConfirmPrompt) Yes added in v0.4.0

func (c *ConfirmPrompt) Yes()

Yes sets "yes" as the answer to the prompt.

Survey.ExpectConfirm("Are you sure to delete this file?").
	Yes().

type Console

type Console interface {
	// terminal device.
	Tty() *os.File

	// pty.
	Fd() uintptr

	// Close closes Console's tty. Calling Close will unblock Expect and ExpectEOF.
	Close() error

	// Send writes string s to Console's tty.
	Send(s string) (int, error)

	// SendLine writes string s to Console's tty with a trailing newline.
	SendLine(s string) (int, error)

	// Expectf reads from the Console's tty until the provided formatted string
	// is read or an error occurs, and returns the buffer read by Console.
	Expectf(format string, args ...interface{}) (string, error)

	// ExpectString reads from Console's tty until the provided string is read or
	// an error occurs, and returns the buffer read by Console.
	ExpectString(s string) (string, error)

	// ExpectEOF reads from Console's tty until EOF or an error occurs, and returns
	// the buffer read by Console.  We also treat the PTSClosed error as an EOF.
	ExpectEOF() (string, error)

	// Expect reads from Console's tty until a condition specified from opts is
	// encountered or an error occurs, and returns the buffer read by console.
	// No extra bytes are read once a condition is met, so if a program isn't
	// expecting input yet, it will be blocked. Sends are queued up in tty's
	// internal buffer so that the next Expect will read the remaining bytes (i.e.
	// rest of prompt) as well as its conditions.
	Expect(opts ...expect.ExpectOpt) (string, error)
}

Console is an interface wrapper around *expect.Console.

type ExpectOption added in v0.1.1

type ExpectOption func(s *Survey)

ExpectOption is option for the survey.

type Expector

type Expector func(t TestingT) *Survey

Expector exp survey.

func Expect

func Expect(options ...ExpectOption) Expector

Expect creates an expected survey with expectations and assures that ExpectationsWereMet() is called.

type HelpAction added in v0.5.0

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

HelpAction sends a ? to show the help.

func (*HelpAction) Do added in v0.5.0

func (a *HelpAction) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*HelpAction) String added in v0.5.0

func (a *HelpAction) String() string

String represents the answer as a string.

type HelpAnswer

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

HelpAnswer sends a ? to show the help.

func (*HelpAnswer) Do added in v0.4.0

func (a *HelpAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*HelpAnswer) String

func (a *HelpAnswer) String() string

String represents the answer as a string.

type InlineSteps added in v0.4.0

type InlineSteps struct {
	*Steps
}

InlineSteps is for internal steps and they are part of an expectation.

func (*InlineSteps) String added in v0.4.0

func (s *InlineSteps) String() string

String represents the answer as a string.

type InputAnswer added in v0.4.0

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

InputAnswer is an answer for password question.

func (*InputAnswer) Do added in v0.4.0

func (a *InputAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*InputAnswer) Interrupted added in v0.4.0

func (a *InputAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*InputAnswer) String added in v0.4.0

func (a *InputAnswer) String() string

String represents the answer as a string.

type InputPrompt added in v0.4.0

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

InputPrompt is an expectation of survey.Input.

func (*InputPrompt) Answer added in v0.4.0

func (p *InputPrompt) Answer(answer string) *InputAnswer

Answer sets the answer to the input prompt.

Survey.ExpectInput("Enter your name:").
	Answer("johnny")

func (*InputPrompt) Do added in v0.4.0

func (p *InputPrompt) Do(c Console) error

Do runs the step.

func (*InputPrompt) Interrupt added in v0.4.0

func (p *InputPrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectInput("Enter your name:").
	Interrupt()

func (*InputPrompt) Once added in v0.4.0

func (p *InputPrompt) Once() *InputPrompt

Once indicates that the message should only be asked once.

Survey.ExpectInput("Enter your name:").
	Answer("johnny").
	Once()

func (*InputPrompt) ShowHelp added in v0.4.0

func (p *InputPrompt) ShowHelp(help string, options ...string)

ShowHelp sets help for the expectation.

Survey.ExpectInput("Enter your name:").
	ShowHelp("It's your full name")

func (*InputPrompt) String added in v0.4.0

func (p *InputPrompt) String() string

String represents the expectation as a string.

func (*InputPrompt) Tab added in v0.4.0

func (p *InputPrompt) Tab(times ...int) *InputSuggestionSteps

Tab starts a sequence of steps to interact with suggestion mode. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Tab()

func (*InputPrompt) Times added in v0.4.0

func (p *InputPrompt) Times(i int) *InputPrompt

Times indicates that the message should only be asked the indicated number of times.

Survey.ExpectInput("Enter your name:").
	Answer("johnny").
	Times(5)

func (*InputPrompt) Twice added in v0.4.0

func (p *InputPrompt) Twice() *InputPrompt

Twice indicates that the message should only be asked twice.

Survey.ExpectInput("Enter your name:").
	Answer("johnny").
	Twice()

func (*InputPrompt) Type added in v0.4.0

Type starts a sequence of steps to interact with suggestion mode.

Survey.ExpectInput("Enter your name:").
	Type("johnny")

type InputSuggestionSteps added in v0.4.0

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

InputSuggestionSteps is a sequence of steps when user is in suggestion mode.

func (*InputSuggestionSteps) Delete added in v0.5.0

func (a *InputSuggestionSteps) Delete(times ...int) *InputSuggestionSteps

Delete sends the DELETE key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Type("johnny").
	Delete(5)

func (*InputSuggestionSteps) Do added in v0.4.0

Do runs the step.

func (*InputSuggestionSteps) Enter added in v0.4.0

func (a *InputSuggestionSteps) Enter()

Enter sends the ENTER key and ends the sequence.

Survey.ExpectInput("Enter your name:").
	Type("hello").
	Enter()

func (*InputSuggestionSteps) Esc added in v0.4.0

Esc sends the ESC key.

Survey.ExpectInput("Enter your name:").
	Type("hello").
	Esc()

func (*InputSuggestionSteps) ExpectSuggestions added in v0.4.0

func (a *InputSuggestionSteps) ExpectSuggestions(suggestions ...string) *InputSuggestionSteps

ExpectSuggestions expects a list of suggestions.

func (*InputSuggestionSteps) Interrupt added in v0.5.0

Interrupt sends ^C and closes the suggestions.

Survey.ExpectInput("Enter your name:").
	Type("johnny").
	Interrupt()

func (*InputSuggestionSteps) MoveDown added in v0.4.0

func (a *InputSuggestionSteps) MoveDown(times ...int) *InputSuggestionSteps

MoveDown sends the ARROW DOWN key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Tab().
	MoveDown(5)

func (*InputSuggestionSteps) MoveUp added in v0.4.0

func (a *InputSuggestionSteps) MoveUp(times ...int) *InputSuggestionSteps

MoveUp sends the ARROW UP key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Tab().
	MoveUp(5)

func (*InputSuggestionSteps) String added in v0.4.0

func (a *InputSuggestionSteps) String() string

String represents the answer as a string.

func (*InputSuggestionSteps) Tab added in v0.4.0

func (a *InputSuggestionSteps) Tab(times ...int) *InputSuggestionSteps

Tab sends the TAB key the indicated times. Default is 1 when omitted.

Survey.ExpectInput("Enter your name:").
	Type("hello").
	Tab(5)

func (*InputSuggestionSteps) Type added in v0.4.0

Type sends a string without enter.

Survey.ExpectInput("Enter your name:").
	Type("johnny").
	Tab().
	Type(".c").
	Enter()

type InterruptAnswer

type InterruptAnswer struct{}

InterruptAnswer sends an interrupt sequence to terminate the survey.

func (*InterruptAnswer) Do added in v0.4.0

func (a *InterruptAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*InterruptAnswer) String

func (a *InterruptAnswer) String() string

String represents the answer as a string.

type MultiSelectExpect added in v0.5.4

type MultiSelectExpect []string

MultiSelectExpect expects a multiselect list from console.

func (*MultiSelectExpect) Do added in v0.5.4

func (e *MultiSelectExpect) Do(c Console) error

Do runs the step.

func (*MultiSelectExpect) String added in v0.5.4

func (e *MultiSelectExpect) String() string

String represents the answer as a string.

type MultiSelectPrompt added in v0.5.4

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

MultiSelectPrompt is an expectation of survey.Select.

func (*MultiSelectPrompt) Delete added in v0.5.4

func (p *MultiSelectPrompt) Delete(times ...int) *MultiSelectPrompt

Delete sends the DELETE key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Delete(3)

func (*MultiSelectPrompt) Do added in v0.5.4

func (p *MultiSelectPrompt) Do(c Console) error

Do runs the step.

func (*MultiSelectPrompt) Enter added in v0.5.4

func (p *MultiSelectPrompt) Enter()

Enter sends the ENTER key and ends the sequence.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Enter()

func (*MultiSelectPrompt) ExpectOptions added in v0.5.4

func (p *MultiSelectPrompt) ExpectOptions(options ...string) *MultiSelectPrompt

ExpectOptions expects a list of options.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		ExpectOptions("English")

func (*MultiSelectPrompt) Interrupt added in v0.5.4

func (p *MultiSelectPrompt) Interrupt()

Interrupt sends ^C and ends the sequence.

   Survey.ExpectMultiSelect("Select a language:").
		Interrupt()

func (*MultiSelectPrompt) MoveDown added in v0.5.4

func (p *MultiSelectPrompt) MoveDown(times ...int) *MultiSelectPrompt

MoveDown sends the ARROW DOWN key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		MoveDown()

func (*MultiSelectPrompt) MoveUp added in v0.5.4

func (p *MultiSelectPrompt) MoveUp(times ...int) *MultiSelectPrompt

MoveUp sends the ARROW UP key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		MoveUp()

func (*MultiSelectPrompt) Select added in v0.5.4

func (p *MultiSelectPrompt) Select() *MultiSelectPrompt

Select selects an option. If the option is selected, it will be deselected.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Select()

func (*MultiSelectPrompt) SelectAll added in v0.5.4

func (p *MultiSelectPrompt) SelectAll() *MultiSelectPrompt

SelectAll selects all filtered options.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		SelectAll()

func (*MultiSelectPrompt) SelectNone added in v0.5.4

func (p *MultiSelectPrompt) SelectNone() *MultiSelectPrompt

SelectNone deselects all filtered options.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		SelectNone()

func (*MultiSelectPrompt) ShowHelp added in v0.5.4

func (p *MultiSelectPrompt) ShowHelp(help string, options ...string) *MultiSelectPrompt

ShowHelp asks for help and asserts the help text.

Survey.ExpectMultiSelect("Select a language:").
	ShowHelp("Your preferred language")

func (*MultiSelectPrompt) String added in v0.5.4

func (p *MultiSelectPrompt) String() string

String represents the expectation as a string.

func (*MultiSelectPrompt) Tab added in v0.5.4

func (p *MultiSelectPrompt) Tab(times ...int) *MultiSelectPrompt

Tab sends the TAB key the indicated times. Default is 1 when omitted.

   Survey.ExpectMultiSelect("Select a language:").
   	Type("Eng").
		Tab()

func (*MultiSelectPrompt) Type added in v0.5.4

Type sends some text to filter the options.

Survey.ExpectMultiSelect("Select a language:").
	Type("Eng")

type MultilineAnswer added in v0.5.5

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

MultilineAnswer is an answer for password question.

func (*MultilineAnswer) Do added in v0.5.5

func (a *MultilineAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*MultilineAnswer) Interrupted added in v0.5.5

func (a *MultilineAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*MultilineAnswer) String added in v0.5.5

func (a *MultilineAnswer) String() string

String represents the answer as a string.

type MultilinePrompt added in v0.5.5

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

MultilinePrompt is an expectation of survey.Multiline.

func (*MultilinePrompt) Answer added in v0.5.5

func (p *MultilinePrompt) Answer(answer string) *MultilineAnswer

Answer sets the answer to the input prompt.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world")

func (*MultilinePrompt) Do added in v0.5.5

func (p *MultilinePrompt) Do(c Console) error

Do runs the step.

func (*MultilinePrompt) Interrupt added in v0.5.5

func (p *MultilinePrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectMultiline("Enter your message:").
	Interrupt()

func (*MultilinePrompt) Once added in v0.5.5

func (p *MultilinePrompt) Once() *MultilinePrompt

Once indicates that the message should only be asked once.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world").
	Once()

func (*MultilinePrompt) String added in v0.5.5

func (p *MultilinePrompt) String() string

String represents the expectation as a string.

func (*MultilinePrompt) Times added in v0.5.5

func (p *MultilinePrompt) Times(i int) *MultilinePrompt

Times indicates that the message should only be asked the indicated number of times.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world").
	Times(5)

func (*MultilinePrompt) Twice added in v0.5.5

func (p *MultilinePrompt) Twice() *MultilinePrompt

Twice indicates that the message should only be asked twice.

Survey.ExpectMultiline("Enter your message:").
	Answer("hello world").
	Twice()

type NoAnswer

type NoAnswer struct{}

NoAnswer sends an empty line to answer the question.

func (*NoAnswer) Do added in v0.4.0

func (a *NoAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*NoAnswer) String

func (a *NoAnswer) String() string

String represents the answer as a string.

type PasswordAnswer

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

PasswordAnswer is an answer for password question.

func (*PasswordAnswer) Do added in v0.4.0

func (a *PasswordAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*PasswordAnswer) Interrupted

func (a *PasswordAnswer) Interrupted()

Interrupted expects the answer will be interrupted.

func (*PasswordAnswer) String

func (a *PasswordAnswer) String() string

String represents the answer as a string.

type PasswordPrompt added in v0.4.0

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

PasswordPrompt is an expectation of survey.Password.

func (*PasswordPrompt) Answer added in v0.4.0

func (p *PasswordPrompt) Answer(answer string) *PasswordAnswer

Answer sets the answer to the password prompt.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!")

func (*PasswordPrompt) Do added in v0.4.0

func (p *PasswordPrompt) Do(c Console) error

Do runs the step.

func (*PasswordPrompt) Interrupt added in v0.4.0

func (p *PasswordPrompt) Interrupt()

Interrupt marks the answer is interrupted.

Survey.ExpectPassword("Enter password:").
	Interrupt()

func (*PasswordPrompt) Once added in v0.4.0

func (p *PasswordPrompt) Once() *PasswordPrompt

Once indicates that the message should only be asked once.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!").
	Once()

func (*PasswordPrompt) ShowHelp added in v0.4.0

func (p *PasswordPrompt) ShowHelp(help string, options ...string)

ShowHelp sets help for the expectation.

Survey.ExpectPassword("Enter password:").
	ShowHelp("Your shiny password")

func (*PasswordPrompt) String added in v0.4.0

func (p *PasswordPrompt) String() string

String represents the expectation as a string.

func (*PasswordPrompt) Times added in v0.4.0

func (p *PasswordPrompt) Times(i int) *PasswordPrompt

Times indicates that the message should only be asked the indicated number of times.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!").
	Times(5)

func (*PasswordPrompt) Twice added in v0.4.0

func (p *PasswordPrompt) Twice() *PasswordPrompt

Twice indicates that the message should only be asked twice.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!").
	Twice()

type Prompt added in v0.4.0

type Prompt interface {
	Step
}

Prompt is a prompt expectation for a survey.

type SelectExpect added in v0.4.0

type SelectExpect []string

SelectExpect expects a select list from console.

func (*SelectExpect) Do added in v0.4.0

func (e *SelectExpect) Do(c Console) error

Do runs the step.

func (*SelectExpect) String added in v0.4.0

func (e *SelectExpect) String() string

String represents the answer as a string.

type SelectPrompt added in v0.5.0

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

SelectPrompt is an expectation of survey.Select.

func (*SelectPrompt) Delete added in v0.5.0

func (p *SelectPrompt) Delete(times ...int) *SelectPrompt

Delete sends the DELETE key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		Delete(3)

func (*SelectPrompt) Do added in v0.5.0

func (p *SelectPrompt) Do(c Console) error

Do runs the step.

func (*SelectPrompt) Enter added in v0.5.0

func (p *SelectPrompt) Enter()

Enter sends the ENTER key and ends the sequence.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		Enter()

func (*SelectPrompt) ExpectOptions added in v0.5.0

func (p *SelectPrompt) ExpectOptions(options ...string) *SelectPrompt

ExpectOptions expects a list of options.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		ExpectOptions("English")

func (*SelectPrompt) Interrupt added in v0.5.0

func (p *SelectPrompt) Interrupt()

Interrupt sends ^C and ends the sequence.

   Survey.ExpectSelect("Select a language:").
		Interrupt()

func (*SelectPrompt) MoveDown added in v0.5.0

func (p *SelectPrompt) MoveDown(times ...int) *SelectPrompt

MoveDown sends the ARROW DOWN key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		MoveDown()

func (*SelectPrompt) MoveUp added in v0.5.0

func (p *SelectPrompt) MoveUp(times ...int) *SelectPrompt

MoveUp sends the ARROW UP key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		MoveUp()

func (*SelectPrompt) ShowHelp added in v0.5.0

func (p *SelectPrompt) ShowHelp(help string, options ...string) *SelectPrompt

ShowHelp asks for help and asserts the help text.

Survey.ExpectSelect("Select a language:").
	ShowHelp("Your preferred language")

func (*SelectPrompt) String added in v0.5.0

func (p *SelectPrompt) String() string

String represents the expectation as a string.

func (*SelectPrompt) Tab added in v0.5.0

func (p *SelectPrompt) Tab(times ...int) *SelectPrompt

Tab sends the TAB key the indicated times. Default is 1 when omitted.

   Survey.ExpectSelect("Select a language:").
   	Type("Eng").
		Tab()

func (*SelectPrompt) Type added in v0.5.0

func (p *SelectPrompt) Type(s string) *SelectPrompt

Type sends some text to filter the options.

Survey.ExpectSelect("Select a language:").
	Type("Eng")

type Signal added in v0.4.0

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

Signal is a safe chan to notify the others.

func NewSignal added in v0.4.0

func NewSignal() *Signal

NewSignal creates a new Signal.

func (*Signal) Done added in v0.4.0

func (s *Signal) Done() <-chan struct{}

Done checks whether the notification arrives or not.

func (*Signal) Notify added in v0.4.0

func (s *Signal) Notify()

Notify notifies the listeners.

type Step added in v0.4.0

type Step interface {
	// Do runs the step.
	Do(c Console) error

	// String represents the step as a string.
	String() string
}

Step is an execution step for a survey.

type Steps added in v0.4.0

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

Steps is a chain of Step.

func (*Steps) Append added in v0.4.0

func (s *Steps) Append(more ...Step) *Steps

Append appends an expectation to the sequence. nolint: unparam

func (*Steps) Close added in v0.4.0

func (s *Steps) Close()

Close closes the steps.

func (*Steps) Do added in v0.4.0

func (s *Steps) Do(c Console) error

Do runs all the steps.

func (*Steps) DoFirst added in v0.4.0

func (s *Steps) DoFirst(c Console) error

DoFirst runs the first step.

func (*Steps) ExpectationsWereMet added in v0.4.0

func (s *Steps) ExpectationsWereMet() error

ExpectationsWereMet checks whether all queued expectations were met in order. If any of them was not met - an error is returned.

func (*Steps) HasNothingToDo added in v0.4.0

func (s *Steps) HasNothingToDo() bool

HasNothingToDo checks whether the steps are not empty.

func (*Steps) Len added in v0.4.0

func (s *Steps) Len() int

Len returns the number of steps.

func (*Steps) Reset added in v0.4.0

func (s *Steps) Reset()

Reset removes all the steps.

func (*Steps) String added in v0.4.0

func (s *Steps) String() string

String represents the steps as a string.

type StringExpect added in v0.6.0

type StringExpect string

StringExpect expects a string from console.

func (StringExpect) Do added in v0.6.0

func (e StringExpect) Do(c Console) error

Do runs the step.

func (StringExpect) String added in v0.6.0

func (e StringExpect) String() string

String represents the answer as a string.

type StringWriter

type StringWriter interface {
	io.Writer
	fmt.Stringer
}

StringWriter is a wrapper for bytes.Buffer.

type Survey

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

Survey is a expectations container and responsible for testing the prompts.

func New

func New(t TestingT, options ...ExpectOption) *Survey

New creates a new expected survey.

func (*Survey) Expect

func (s *Survey) Expect(c Console) error

Expect runs an expectation against a given console.

func (*Survey) ExpectConfirm

func (s *Survey) ExpectConfirm(message string) *ConfirmPrompt

ExpectConfirm expects a ConfirmPrompt.

Survey.ExpectConfirm("ConfirmPrompt?").
	Yes()

func (*Survey) ExpectInput added in v0.4.0

func (s *Survey) ExpectInput(message string) *InputPrompt

ExpectInput expects an InputPrompt.

Survey.ExpectInput("Enter password:").
	Answer("hello world!")

func (*Survey) ExpectMultiSelect added in v0.5.4

func (s *Survey) ExpectMultiSelect(message string) *MultiSelectPrompt

ExpectMultiSelect expects a MultiSelectPrompt.

Survey.ExpectMultiSelect("Enter password:").
	Enter()

func (*Survey) ExpectMultiline added in v0.5.5

func (s *Survey) ExpectMultiline(message string) *MultilinePrompt

ExpectMultiline expects a MultilinePrompt.

Survey.ExpectMultiline("Enter password:").
	Answer("hello world")

func (*Survey) ExpectPassword

func (s *Survey) ExpectPassword(message string) *PasswordPrompt

ExpectPassword expects a PasswordPrompt.

Survey.ExpectPassword("Enter password:").
	Answer("hello world!")

func (*Survey) ExpectSelect added in v0.5.0

func (s *Survey) ExpectSelect(message string) *SelectPrompt

ExpectSelect expects a SelectPrompt.

Survey.ExpectSelect("Enter password:").
	Enter()

func (*Survey) ExpectationsWereMet

func (s *Survey) ExpectationsWereMet() error

ExpectationsWereMet checks whether all queued expectations were met in order. If any of them was not met - an error is returned.

func (*Survey) ResetExpectations

func (s *Survey) ResetExpectations()

ResetExpectations resets all the expectations.

func (*Survey) Start

func (s *Survey) Start(fn func(stdio terminal.Stdio))

Start starts the survey with a default timeout.

func (*Survey) WithTimeout

func (s *Survey) WithTimeout(t time.Duration) *Survey

WithTimeout sets the timeout of a survey.

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	FailNow()
	Cleanup(func())
	Log(args ...interface{})
	Logf(format string, args ...interface{})
}

TestingT is an interface wrapper around *testing.T.

type TypeAnswer added in v0.4.0

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

TypeAnswer types an answer.

func (*TypeAnswer) Do added in v0.4.0

func (a *TypeAnswer) Do(c Console) error

Do runs the step. nolint: errcheck,gosec

func (*TypeAnswer) String added in v0.4.0

func (a *TypeAnswer) String() string

String represents the answer as a string.

Directories

Path Synopsis
Package options provide options for configuring survey options.
Package options provide options for configuring survey options.
cobra
Package cobra provides support for stdio with cobra command.
Package cobra provides support for stdio with cobra command.

Jump to

Keyboard shortcuts

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