Watch
1
0
Fork
You've already forked golang-github-twpayne-go-expect
0
No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Daniel Baumann f4c9d9da97
Releasing fastforward version 0.0.1-1~ffwd13+u1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2026-07-08 11:37:44 +02:00
.github/workflows Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
debian Releasing fastforward version 0.0.1-1~ffwd13+u1. 2026-07-08 11:37:44 +02:00
.travis.yml Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
console.go Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
doc.go Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
expect.go Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
expect_opt.go Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
expect_opt_test.go Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
expect_test.go Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
go.mod Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
go.sum Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
LICENSE Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
OSSMETADATA Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
README.md Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00
test_log.go Adding upstream version 0.0.1. 2026-07-08 11:36:58 +02:00

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)
}