forked from twpayne/go-pinentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
55 lines (47 loc) · 1.02 KB
/
process.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
51
52
53
54
55
package pinentry
import (
"bufio"
"io"
"os/exec"
"go.uber.org/multierr"
)
// A Process abstracts the interface to a pinentry Process.
type Process interface {
io.WriteCloser
ReadLine() ([]byte, bool, error)
Start(string, []string) error
}
// A execProcess executes a pinentry process.
type execProcess struct {
cmd *exec.Cmd
stdin io.WriteCloser
stdout *bufio.Reader
}
func (p *execProcess) Close() (err error) {
defer func() {
err = multierr.Append(err, p.cmd.Wait())
}()
err = p.stdin.Close()
return
}
func (p *execProcess) ReadLine() ([]byte, bool, error) {
return p.stdout.ReadLine()
}
func (p *execProcess) Start(name string, args []string) (err error) {
p.cmd = exec.Command(name, args...)
p.stdin, err = p.cmd.StdinPipe()
if err != nil {
return
}
var stdoutPipe io.ReadCloser
stdoutPipe, err = p.cmd.StdoutPipe()
if err != nil {
return
}
p.stdout = bufio.NewReader(stdoutPipe)
err = p.cmd.Start()
return
}
func (p *execProcess) Write(data []byte) (int, error) {
return p.stdin.Write(data)
}