forked from sachaos/viddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
59 lines (47 loc) · 936 Bytes
/
run.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
//go:build !windows
package main
import (
"bytes"
"io"
"time"
"github.com/creack/pty"
)
func (s *Snapshot) run(finishedQueue chan<- int64, width int, isPty bool) error {
s.start = time.Now()
defer func() {
s.end = time.Now()
}()
var b, eb bytes.Buffer
commands := []string{s.command}
commands = append(commands, s.args...)
command := s.prepareCommand(commands)
command.Stderr = &eb
if isPty {
pty, err := pty.StartWithSize(command, &pty.Winsize{
Cols: uint16(width),
})
if err != nil {
return err
}
go func() {
_, _ = io.Copy(&b, pty)
}()
} else {
command.Stdout = &b
if err := command.Start(); err != nil {
return err
}
}
go func() {
if err := command.Wait(); err != nil {
s.err = err
}
s.result = b.Bytes()
s.errorResult = eb.Bytes()
s.exitCode = command.ProcessState.ExitCode()
s.completed = true
finishedQueue <- s.id
close(s.finish)
}()
return nil
}