-
Notifications
You must be signed in to change notification settings - Fork 30
/
tempfile.go
94 lines (80 loc) · 1.7 KB
/
tempfile.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
86
87
88
89
90
91
92
93
94
package runner
import (
"bytes"
"context"
"io"
"log"
"os"
"os/exec"
"strconv"
"syscall"
"github.com/pkg/errors"
)
type TempFile struct {
*ExecutableConfig
command *command
Script string
ProgramName string
LanguageID string
}
func (s TempFile) DryRun(ctx context.Context, w io.Writer) {
var b bytes.Buffer
_, err := w.Write(b.Bytes())
if err != nil {
log.Fatalf("failed to write: %s", err)
}
}
func (s *TempFile) Run(ctx context.Context) error {
cmd, err := newCommand(
&commandConfig{
ProgramName: s.ProgramName,
LanguageID: s.LanguageID,
Directory: s.Dir,
Session: s.Session,
Tty: s.Tty,
Stdin: s.Stdin,
Stdout: s.Stdout,
Stderr: s.Stderr,
CommandMode: CommandModeTempFile,
Script: s.Script,
Logger: s.Logger,
},
)
if err != nil {
return err
}
s.command = cmd
defer func() { _ = s.command.Finalize() }()
return s.run(ctx, cmd)
}
func (s TempFile) ExitCode() int {
if s.command == nil || s.command.cmd == nil {
return -1
}
return s.command.cmd.ProcessState.ExitCode()
}
func (s TempFile) run(ctx context.Context, cmd *command) error {
opts := &startOpts{}
if s.Tty {
opts.DisableEcho = true
}
if err := cmd.StartWithOpts(ctx, opts); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
var exiterr *exec.ExitError
// Ignore errors caused by SIGINT.
if errors.As(err, &exiterr) {
var rerr error = ExitErrorFromExec(exiterr)
if exiterr.ProcessState.Sys().(syscall.WaitStatus).Signal() != os.Kill {
msg := "failed to run command"
if len(s.Name) > 0 {
msg += " " + strconv.Quote(s.Name)
}
return errors.Wrap(rerr, msg)
}
return rerr
}
}
return nil
}