-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskrun.go
More file actions
236 lines (217 loc) · 6.74 KB
/
Copy pathtaskrun.go
File metadata and controls
236 lines (217 loc) · 6.74 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Package taskrun is an embeddable task and script runner.
//
// A Definition is validated and frozen by New, so later mutation of the caller's
// maps cannot change a run. Each Run gets isolated variables, environment and
// working directory; actions run through explicit shells or host handlers, and
// the processes an action owns are terminated when its context is canceled or
// its deadline expires.
package taskrun
import (
"os"
"strings"
"time"
"github.com/gookit/taskrun/internal/data"
)
// Default limits applied by New unless an Option overrides them.
const (
// DefaultMaxCallDepth bounds the static task call chain.
DefaultMaxCallDepth = 64
// DefaultMaxExpansions bounds the number of task calls in one run.
DefaultMaxExpansions = 10000
// DefaultKillGrace is how long a canceled process tree may exit on its own
// before it is killed.
DefaultKillGrace = 200 * time.Millisecond
// DefaultDynamicOutputLimit bounds the captured output of a dynamic
// variable command.
DefaultDynamicOutputLimit = 1 << 20
)
// Option configures a Runner. Options are applied before the definition is
// validated, so an invalid engine, handler name or limit fails New.
type Option func(*runnerConfig) error
type runnerConfig struct {
engine Engine
handlers map[string]Handler
observer Observer
baseEnv map[string]string
baseEnvSet bool
maxCallDepth int
maxExpansions int
killGrace time.Duration
dynamicLimit int64
}
// WithEngine replaces the external action backend. The backend must honor
// cancellation and return a ProcessError or a context cause so failures can be
// classified.
func WithEngine(engine Engine) Option {
return func(c *runnerConfig) error {
if engine == nil {
return errf("WithEngine: engine is nil")
}
c.engine = engine
return nil
}
}
// WithHandler registers a host action. Handlers are immutable after New: a
// definition that names an unregistered handler is rejected.
func WithHandler(name string, handler Handler) Option {
return func(c *runnerConfig) error {
if name == "" {
return errf("WithHandler: name is required")
}
if handler == nil {
return errf("WithHandler: handler for %q is nil", name)
}
if c.handlers == nil {
c.handlers = map[string]Handler{}
}
c.handlers[name] = handler
return nil
}
}
// WithObserver registers an event observer. Observers are optional, cannot
// change scheduling decisions and do not return errors; callbacks of one run are
// delivered in order, while different runs may call the observer concurrently.
func WithObserver(observer Observer) Option {
return func(c *runnerConfig) error {
if observer == nil {
return errf("WithObserver: observer is nil")
}
c.observer = observer
return nil
}
}
// WithBaseEnv sets the environment baseline snapshot. The default is
// os.Environ copied when New runs; a run never re-reads the process
// environment and never modifies it.
func WithBaseEnv(env map[string]string) Option {
return func(c *runnerConfig) error {
for key := range env {
if key == "" {
return errf("WithBaseEnv: empty variable name")
}
}
c.baseEnv = data.CloneStringMap(env)
c.baseEnvSet = true
return nil
}
}
// WithMaxCallDepth bounds the static task call chain. Zero or negative disables
// the static check.
func WithMaxCallDepth(depth int) Option {
return func(c *runnerConfig) error {
c.maxCallDepth = depth
return nil
}
}
// WithMaxExpansions bounds the number of task calls in one run.
func WithMaxExpansions(limit int) Option {
return func(c *runnerConfig) error {
if limit <= 0 {
return errf("WithMaxExpansions: limit must be positive")
}
c.maxExpansions = limit
return nil
}
}
// WithKillGrace sets how long a canceled process tree may exit on its own
// before the engine kills it.
func WithKillGrace(grace time.Duration) Option {
return func(c *runnerConfig) error {
if grace < 0 {
return errf("WithKillGrace: grace must not be negative")
}
c.killGrace = grace
return nil
}
}
// WithDynamicOutputLimit bounds the captured output of a dynamic variable
// command. Output above the bound fails the run instead of rendering a
// truncated value.
func WithDynamicOutputLimit(limit int64) Option {
return func(c *runnerConfig) error {
if limit <= 0 {
return errf("WithDynamicOutputLimit: limit must be positive")
}
c.dynamicLimit = limit
return nil
}
}
// Runner holds a validated definition snapshot and an immutable backend
// configuration. One Runner may serve concurrent Runs.
type Runner struct {
def Definition
cfg runnerConfig
}
// New validates a definition and publishes a private snapshot. Load, New,
// Lookup, List and Inspect never execute commands; only Run produces side
// effects through explicit actions and dynamic variables.
func New(def Definition, opts ...Option) (*Runner, error) {
cfg := runnerConfig{
handlers: map[string]Handler{},
maxCallDepth: DefaultMaxCallDepth,
maxExpansions: DefaultMaxExpansions,
killGrace: DefaultKillGrace,
dynamicLimit: DefaultDynamicOutputLimit,
}
for _, opt := range opts {
if opt == nil {
continue
}
if err := opt(&cfg); err != nil {
return nil, &RunError{Kind: ErrKindInvalidDefinition, Err: err}
}
}
if cfg.engine == nil {
cfg.engine = ProcessEngine{grace: cfg.killGrace}
}
prepared, err := prepareDefinition(def, cfg)
if err != nil {
return nil, err
}
if !cfg.baseEnvSet {
cfg.baseEnv = envFromList(os.Environ())
}
handlers := make(map[string]Handler, len(cfg.handlers))
for name, handler := range cfg.handlers {
handlers[name] = handler
}
cfg.handlers = handlers
return &Runner{def: prepared, cfg: cfg}, nil
}
// Lookup returns an exact-name copy of a task. The returned value shares no
// mutable state with the Runner.
func (r *Runner) Lookup(name string) (Task, error) {
task, ok := r.def.Tasks[name]
if !ok {
// A missing dependency is a definition error; only a root lookup by
// name reports ErrNotFound.
return Task{}, &RunError{Kind: ErrKindNotFound, Task: name, Err: ErrNotFound}
}
return cloneTask(task), nil
}
// List returns every task sorted by name. Each entry is a copy.
func (r *Runner) List() []Task {
names := make([]string, 0, len(r.def.Tasks))
for name := range r.def.Tasks {
names = append(names, name)
}
data.SortStrings(names)
out := make([]Task, 0, len(names))
for _, name := range names {
out = append(out, cloneTask(r.def.Tasks[name]))
}
return out
}
// Source returns the definition provenance recorded by the loader, if any.
func (r *Runner) Source() []Source { return append([]Source(nil), r.def.Sources...) }
func envFromList(list []string) map[string]string {
out := make(map[string]string, len(list))
for _, item := range list {
index := strings.Index(item, "=")
if index <= 0 {
continue
}
out[item[:index]] = item[index+1:]
}
return out
}