gexpect

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2016 License: MIT, Apache-2.0 Imports: 11 Imported by: 0

README

Gexpect

Gexpect is a pure golang expect-like module.

It makes it simpler to create and control other terminal applications.

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

Examples

Spawn handles the argument parsing from a string

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)

ReadLine, ReadUntil and SendLine send strings from/to stdout/stdin respectively

child := gexpect.Spawn("cat")
child.SendLine("echoing process_stdin") //  SendLine(command string) (error)
msg, _ := child.ReadLine() // msg = echoing process_stdin

Wait and Close allow for graceful and ungraceful termination.

child.Wait() // Waits until the child terminates naturally.
child.Close() // Sends a kill command

AsyncInteractChannels spawns two go routines to pipe into and from stdout/stdin, allowing for some usecases to be a little simpler.

child := gexpect.spawn("sh")
sender, reciever := child.AsyncInteractChannels()
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)]
}

ExpectRegex uses golang's internal regex engine to wait until a match from the process with the given regular expression (or an error on process termination with no match).

child := gexpect.Spawn("echo accb")	
match, _ := child.ExpectRegex("a..b")
// (match=true)

ExpectRegexFind allows for groups to be extracted from process stdout. The first element is an array of containing the total matched text, followed by each subexpression group match.

child := gexpect.Spawn("echo 123 456 789")
result, _ := child.ExpectRegexFind("\d+ (\d+) (\d+)")
// result = []string{"123 456 789", "456", "789"}

See gexpect_test.go and the examples folder for full syntax

Credits

github.com/kballard/go-shellquote	
github.com/kr/pty
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) Capture

func (expect *ExpectSubprocess) Capture()

func (*ExpectSubprocess) Close

func (expect *ExpectSubprocess) Close() error

func (*ExpectSubprocess) Collect

func (expect *ExpectSubprocess) Collect() []byte

func (*ExpectSubprocess) Expect

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

func (*ExpectSubprocess) ExpectRegex

func (expect *ExpectSubprocess) ExpectRegex(regex string) (bool, error)

func (*ExpectSubprocess) ExpectRegexFind

func (expect *ExpectSubprocess) ExpectRegexFind(regex string) ([]string, error)

func (*ExpectSubprocess) ExpectRegexFindWithOutput

func (expect *ExpectSubprocess) ExpectRegexFindWithOutput(regex string) ([]string, string, error)

func (*ExpectSubprocess) ExpectTimeout

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

func (*ExpectSubprocess) ExpectTimeoutRegexFind

func (expect *ExpectSubprocess) ExpectTimeoutRegexFind(regex string, timeout time.Duration) ([]string, error)

func (*ExpectSubprocess) ExpectTimeoutRegexFindWithOutput

func (expect *ExpectSubprocess) ExpectTimeoutRegexFindWithOutput(regex string, timeout time.Duration) ([]string, string, 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