-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
143 lines (120 loc) · 3.08 KB
/
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package bootstrap
import (
"context"
"errors"
"fmt"
"github.com/dlsniper/debugger"
"github.com/hephbuild/heph/log/log"
"github.com/hephbuild/heph/sandbox"
"github.com/hephbuild/heph/scheduler"
"github.com/hephbuild/heph/specs"
"github.com/hephbuild/heph/targetrun"
"github.com/hephbuild/heph/worker2/poolwait"
"os"
"os/exec"
)
type ErrorWithExitCode struct {
Err error
ExitCode int
}
func (e ErrorWithExitCode) Error() string {
return e.Err.Error()
}
func (e ErrorWithExitCode) Unwrap() error {
return e.Err
}
func Run(ctx context.Context, e *scheduler.Scheduler, rrs targetrun.Requests, runopts RunOpts, inlineSingle bool) error {
return RunMode(ctx, e, rrs, runopts, inlineSingle, "", sandbox.IOConfig{
Stdout: os.Stdout,
Stderr: os.Stderr,
Stdin: os.Stdin,
})
}
func RunMode(ctx context.Context, e *scheduler.Scheduler, rrs targetrun.Requests, runopts RunOpts, inlineSingle bool, mode string, iocfg sandbox.IOConfig) error {
debugger.SetLabels(func() []string {
return []string{
"where", "RunMode",
}
})
for i := range rrs {
rrs[i].Mode = mode
}
shellCount := rrs.Count(func(rr targetrun.Request) bool {
return rr.Shell
})
if shellCount > 0 {
if shellCount > 1 {
return fmt.Errorf("shell mode is only compatible with running a single target")
}
if !inlineSingle {
return fmt.Errorf("target invocation must be inlined to enable shell")
}
}
var inlineRR *targetrun.Request
if len(rrs) == 1 && inlineSingle && !runopts.NoInline {
inlineRR = &rrs[0]
}
var skip []specs.Specer
if inlineRR != nil {
skip = []specs.Specer{inlineRR.Target}
}
tdepsMap, tracker, err := e.ScheduleTargetRRsWithDeps(ctx, rrs, skip)
if err != nil {
return err
}
tdeps := tdepsMap.All()
tdeps.AddDep(tracker.Group())
err = poolwait.Wait(ctx, "Run", e.Pool, tdeps, runopts.Plain, e.Config.ProgressInterval)
if err != nil {
return err
}
if inlineRR == nil {
if runopts.PrintOutput.Bool {
for _, target := range rrs.Targets().Slice() {
target := e.LocalCache.Metas.Find(target)
err = PrintTargetOutputPaths(target, runopts.PrintOutput.Str)
if err != nil {
return err
}
}
}
if runopts.CatOutput.Bool {
for _, target := range rrs.Targets().Slice() {
target := e.LocalCache.Metas.Find(target)
err = PrintTargetOutputContent(target, runopts.CatOutput.Str)
if err != nil {
return err
}
}
}
return nil
}
if runopts.PrintOutput.Bool || runopts.CatOutput.Bool {
log.Debugf("Redirecting stdout to stderr")
iocfg.Stdout = os.Stderr
}
err = e.RunWithSpan(ctx, *inlineRR, iocfg, nil)
if err != nil {
var eerr *exec.ExitError
if errors.As(err, &eerr) {
return ErrorWithExitCode{
Err: err,
ExitCode: eerr.ExitCode(),
}
}
return err
}
inlineTarget := e.LocalCache.Metas.Find(inlineRR.Target)
if runopts.PrintOutput.Bool {
err = PrintTargetOutputPaths(inlineTarget, runopts.PrintOutput.Str)
if err != nil {
return err
}
} else if runopts.CatOutput.Bool {
err = PrintTargetOutputContent(inlineTarget, runopts.CatOutput.Str)
if err != nil {
return err
}
}
return nil
}