Categorygithub.com/Netflix/go-expect
modulepackage
0.0.0-20220104043353-73e0943537d2
Repository: https://github.com/netflix/go-expect.git
Documentation: pkg.go.dev

# README

go-expect

Go Build Status GoDoc NetflixOSS Lifecycle

Package expect provides an expect-like interface to automate control of applications. It is unlike expect in that it does not spawn or manage process lifecycle. This package only focuses on expecting output and sending input through it's pseudoterminal.

Usage

os.Exec example

package main

import (
	"log"
	"os"
	"os/exec"
	"time"

	expect "github.com/Netflix/go-expect"
)

func main() {
	c, err := expect.NewConsole(expect.WithStdout(os.Stdout))
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()

	cmd := exec.Command("vi")
	cmd.Stdin = c.Tty()
	cmd.Stdout = c.Tty()
	cmd.Stderr = c.Tty()

	go func() {
		c.ExpectEOF()
	}()

	err = cmd.Start()
	if err != nil {
		log.Fatal(err)
	}

	time.Sleep(time.Second)
	c.Send("iHello world\x1b")
	time.Sleep(time.Second)
	c.Send("dd")
	time.Sleep(time.Second)
	c.SendLine(":q!")

	err = cmd.Wait()
	if err != nil {
		log.Fatal(err)
	}
}

golang.org/x/crypto/ssh/terminal example

package main

import (
	"fmt"

	"golang.org/x/crypto/ssh/terminal"

	expect "github.com/Netflix/go-expect"
)

func getPassword(fd int) string {
	bytePassword, _ := terminal.ReadPassword(fd)

	return string(bytePassword)
}

func main() {
	c, _ := expect.NewConsole()

	defer c.Close()

	donec := make(chan struct{})
	go func() {
		defer close(donec)
		c.SendLine("hunter2")
	}()

	echoText := getPassword(int(c.Tty().Fd()))

	<-donec

	fmt.Printf("\nPassword from stdin: %s", echoText)
}

# Functions

All adds an Expect condition to exit if the content read from Console's tty matches all of the provided ExpectOpt, in any order.
EOF adds an Expect condition to exit if io.EOF is returned from reading Console's tty.
Error adds an Expect condition to exit if reading from Console's tty returns one of the provided errors.
NewChanReader returns a io.Reader over a byte chan.
NewConsole returns a new Console with the given options.
NewPassthroughPipe returns a new pipe for a io.Reader that passes through non-timeout errors.
NewReaderLease returns a new ReaderLease that begins reading the given io.Reader.
NewTestConsole returns a new Console that multiplexes the application's stdout to go's testing logger.
NewTestWriter returns an io.Writer where bytes written to the file are logged by go's testing logger.
PTSClosed adds an Expect condition to exit if we get an "read /dev/ptmx: input/output error" error which can occur on Linux while reading from the ptm after the pts is closed.
Regexp adds an Expect condition to exit if the content read from Console's tty matches the given Regexp.
RegexpPattern adds an Expect condition to exit if the content read from Console's tty matches the given Regexp patterns.
String adds an Expect condition to exit if the content read from Console's tty contains any of the given strings.
StripTrailingEmptyLines returns a copy of s stripped of trailing lines that consist of only space characters.
WithCloser adds closers that are closed in order when Console is closed.
WithDefaultTimeout sets a default read timeout during Expect statements.
WithExpectObserver adds an ExpectObserver to allow monitoring Expect operations.
WithLogger adds a logger for Console to log debugging information to.
WithSendObserver adds a SendObserver to allow monitoring Send operations.
WithStdin adds readers that bytes read are written to Console's tty.
WithStdout adds writers that Console duplicates writes to, similar to the Unix tee(1) command.
WithTimeout sets a read timeout for an Expect statement.

# Structs

Console is an interface to automate input and output for interactive applications.
ConsoleOpts provides additional options on creating a Console.
ExpectOpts provides additional options on Expect.
PassthroughPipe is pipes data from a io.Reader and allows setting a read deadline.
ReaderLease provides cancellable io.Readers from an underlying io.Reader.

# Interfaces

CallbackMatcher is a matcher that provides a Callback function.
Matcher provides an interface for finding a match in content read from Console's tty.

# Type aliases

ConsoleCallback is a callback function to execute if a match is found for the chained matcher.
ConsoleOpt allows setting Console options.
ExpectObserver provides an interface for a function callback that will be called after each Expect operation.
ExpectOpt allows settings Expect options.
SendObserver provides an interface for a function callback that will be called after each Send operation.