Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Netflix/go-expect
an expect-like golang library to automate control of terminal or console based programs.
https://github.com/Netflix/go-expect
automation cli expect
Last synced: 3 months ago
JSON representation
an expect-like golang library to automate control of terminal or console based programs.
- Host: GitHub
- URL: https://github.com/Netflix/go-expect
- Owner: Netflix
- License: apache-2.0
- Created: 2018-05-14T17:53:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-15T04:55:13.000Z (5 months ago)
- Last Synced: 2024-06-19T12:42:43.561Z (5 months ago)
- Topics: automation, cli, expect
- Language: Go
- Size: 78.1 KB
- Stars: 427
- Watchers: 309
- Forks: 65
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-repositories - Netflix/go-expect - an expect-like golang library to automate control of terminal or console based programs. (Go)
README
# go-expect
![Go](https://github.com/Netflix/go-expect/workflows/Go/badge.svg)
[![Build Status](https://travis-ci.com/Netflix/go-expect.svg?branch=master)](https://travis-ci.com/Netflix/go-expect)
[![GoDoc](https://godoc.org/github.com/Netflix/go-expect?status.svg)](https://godoc.org/github.com/Netflix/go-expect)
[![NetflixOSS Lifecycle](https://img.shields.io/osslifecycle/Netflix/go-expect.svg)]()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
```go
package mainimport (
"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
```go
package mainimport (
"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)
}
```