-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell.go
50 lines (42 loc) · 1.04 KB
/
shell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package shell
import (
"errors"
"io"
"os/exec"
)
type Shell struct {
Proc *exec.Cmd
Stdin io.WriteCloser
Stdout io.ReadCloser
}
func NewShell(command string) (Shell, error) {
var err error
shell := Shell{}
shell.Proc = exec.Command(command)
if shell.Stdin, err = shell.Proc.StdinPipe(); err != nil {
return shell, errors.New("could not get a pipe to stdin")
}
if shell.Stdout, err = shell.Proc.StdoutPipe(); err != nil {
return shell, errors.New("could not get a pipe to stdout")
}
if err = shell.Proc.Start(); err != nil {
return shell, errors.New("could not start process")
}
return shell, nil
}
func (shell Shell) Close() {
shell.Stdin.Close()
shell.Stdout.Close()
}
func (shell Shell) Run(command string) (string, error) {
// TODO handle case when rlen is > 1024
shell.Stdin.Write([]byte(command + "\n"))
var buf []byte
buf = make([]byte, 1024)
rlen, err := shell.Stdout.Read(buf)
if err != nil {
return "", errors.New("Error reading from stdout:\n" + err.Error())
}
out := string(buf[:rlen])
return out, nil
}