-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliprocess_win.go
85 lines (69 loc) · 1.47 KB
/
cliprocess_win.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//go:build windows
package main
import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"
)
type WinCLIProcess struct {
command string
args []string
cmd *exec.Cmd
stdoutPipe io.ReadCloser
stdinPipe io.WriteCloser
}
func newCLIProcess(command string, args []string) (CLIProcess, error) {
// filepath.Abs() calls filepath.Clean() which translates the separators to the OS's default separator
if strings.Contains(command, "/") {
var err error
command, err = filepath.Abs(command)
if _, err := os.Stat(command); os.IsNotExist(err) {
// If file does not exist, we add the .exe extension
// This is just for the Windows impl
command += ".exe"
}
if err != nil {
return nil, err
}
}
cmd := exec.Command(command, args...)
process := &WinCLIProcess{
command: command,
args: args,
cmd: cmd,
}
return process, nil
}
func (p *WinCLIProcess) Start() error {
var err error
p.stdoutPipe, err = p.cmd.StdoutPipe()
if err != nil {
return err
}
p.stdinPipe, err = p.cmd.StdinPipe()
if err != nil {
return err
}
err = p.cmd.Start()
if err != nil {
return err
}
return nil
}
func (p *WinCLIProcess) Read() ([]byte, error) {
buf := make([]byte, 1024)
n, err := p.stdoutPipe.Read(buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}
func (p *WinCLIProcess) Write(input string) error {
_, err := io.Copy(p.stdinPipe, strings.NewReader(input+"\r\n"))
return err
}
func (p *WinCLIProcess) Stop() error {
return p.cmd.Wait()
}