-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcliprocess_linux.go
68 lines (55 loc) · 1.14 KB
/
cliprocess_linux.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
//go:build linux
package main
import (
"github.com/iyzyi/aiopty/pty"
// "io"
"path/filepath"
"strings"
)
type LinuxCLIProcess struct {
cmd string
args []string
pty *pty.Pty
}
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 != nil {
return nil, err
}
}
process := &LinuxCLIProcess{
cmd: command,
args: args,
pty: nil,
}
return process, nil
}
func (p *LinuxCLIProcess) Start() error {
pty, err := pty.OpenWithOptions(&pty.Options{
Path: p.cmd,
Args: append([]string{p.cmd}, p.args...),
})
if err != nil {
return err
}
p.pty = pty
return nil
}
func (p *LinuxCLIProcess) Read() ([]byte, error) {
buf := make([]byte, 1024)
n, err := p.pty.Read(buf)
if err != nil {
return nil, err
}
return buf[:n], nil
}
func (p *LinuxCLIProcess) Write(input string) error {
_, err := p.pty.Write([]byte(input + "\n"))
return err
}
func (p *LinuxCLIProcess) Stop() error {
return p.pty.Close()
}