Skip to content

Instantly share code, notes, and snippets.

@keithpitt
Last active October 13, 2015 02:31
Show Gist options
  • Save keithpitt/22766a0d3c523e5563e7 to your computer and use it in GitHub Desktop.
Save keithpitt/22766a0d3c523e5563e7 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path"
"sync"
"syscall"
"github.com/kr/pty"
)
func main() {
wd, _ := os.Getwd()
c := exec.Command(path.Join(wd, "pty-test.sh"))
c.Dir = wd
c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
f, err := pty.Start(c)
if err != nil {
panic(err)
}
var wg sync.WaitGroup
wg.Add(1)
var buffer bytes.Buffer
go func() {
_, err = io.Copy(&buffer, f)
if e, ok := err.(*os.PathError); ok && e.Err == syscall.EIO {
// We can safely ignore this error, because it's just
// the PTY telling us that it closed successfully. See:
// https://github.com/buildkite/agent/pull/34#issuecomment-46080419
err = nil
}
wg.Done()
}()
c.Wait()
wg.Wait()
fmt.Printf(buffer.String())
}
#!/bin/bash
echo "Oh hai there!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment