gexpect

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2015 License: MIT, Apache-2.0 Imports: 8 Imported by: 0

README

Gexpect

Gexpect is a pure golang expect-like module.

It makes it simple and safe to control other terminal applications.

It provides pexpect-like syntax for golang

child, err := gexpect.Spawn("python")
if err != nil {
	panic(err)
}
child.Expect(">>>")
child.SendLine("print 'Hello World'")
child.Interact()
child.Close()

It's fast, with its 'expect' function working off a variant of Knuth-Morris-Pratt on Standard Output/Error streams

It also provides interface functions that make it much simpler to work with subprocesses

child.Spawn("/bin/sh -c 'echo \"my complicated command\" | tee log | cat > log2'")

child.ReadLine() // ReadLine() (string, error)

child.ReadUntil(' ') // ReadUntil(delim byte) ([]byte, error)

child.SendLine("/bin/sh -c 'echo Hello World | tee foo'") //  SendLine(command string) (error)

child.Wait() // Wait() (error)

sender, reciever := child.AsyncInteractChannels() // AsyncInteractChannels() (chan string, chan string)
sender <- "echo Hello World\n" // Send to stdin

line, open := <- reciever // Recieve a line from stdout/stderr
// When the subprocess stops (e.g. with child.Close()) , receiver is closed
if open {
	fmt.Printf("Received %s", line)]
}

Free, MIT open source licenced, etc etc.

Check gexpect_test.go and the examples folder for full examples

Golang Dependencies

"github.com/kballard/go-shellquote"
"github.com/kr/pty"

Credits

KMP Algorithm: "http://blog.databigbang.com/searching-for-substrings-in-streams-a-slight-modification-of-the-knuth-morris-pratt-algorithm-in-haxe/"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpectSubprocess

type ExpectSubprocess struct {
	Cmd *exec.Cmd
	// contains filtered or unexported fields
}

func Command

func Command(command string) (*ExpectSubprocess, error)

func Spawn

func Spawn(command string) (*ExpectSubprocess, error)

func SpawnAtDirectory

func SpawnAtDirectory(command string, directory string) (*ExpectSubprocess, error)

func (*ExpectSubprocess) AsyncInteractChannels

func (expect *ExpectSubprocess) AsyncInteractChannels() (send chan string, receive chan string)

func (*ExpectSubprocess) Close

func (expect *ExpectSubprocess) Close() error

func (*ExpectSubprocess) Expect

func (expect *ExpectSubprocess) Expect(searchString string) (e error)

func (*ExpectSubprocess) ExpectRegex

func (expect *ExpectSubprocess) ExpectRegex(regexSearchString string) (e error)

This quite possibly won't work as we're operating on an incomplete stream. It might work if all the input is within one Flush, but that can't be relied upon. I need to find a nice, safe way to apply a regex to a stream of partial content, given we don't not knowing how long our input is, and thus can't buffer it. Until that point, please just use Expect, or use the channel to parse the stream yourself.

func (*ExpectSubprocess) ExpectTimeout

func (expect *ExpectSubprocess) ExpectTimeout(searchString string, timeout time.Duration) (e error)

func (*ExpectSubprocess) Interact

func (expect *ExpectSubprocess) Interact()

func (*ExpectSubprocess) ReadLine

func (expect *ExpectSubprocess) ReadLine() (string, error)

func (*ExpectSubprocess) ReadUntil

func (expect *ExpectSubprocess) ReadUntil(delim byte) ([]byte, error)

func (*ExpectSubprocess) Send

func (expect *ExpectSubprocess) Send(command string) error

func (*ExpectSubprocess) SendLine

func (expect *ExpectSubprocess) SendLine(command string) error

func (*ExpectSubprocess) Start

func (expect *ExpectSubprocess) Start() error

func (*ExpectSubprocess) Wait

func (expect *ExpectSubprocess) Wait() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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