File: process.go

package info (click to toggle)
golang-github-twpayne-go-pinentry 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 132 kB
  • sloc: makefile: 4
file content (55 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download
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)
}