-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
runner.go
322 lines (286 loc) · 6.83 KB
/
runner.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package genny
import (
"context"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/gobuffalo/genny/v2/internal/safe"
"github.com/sirupsen/logrus"
)
type RunFn func(r *Runner) error
// Runner will run the generators
type Runner struct {
Logger Logger // Logger to use for the run
Context context.Context // context to use for the run
ExecFn func(*exec.Cmd) error // function to use when executing files
FileFn func(File) (File, error) // function to use when writing files
ChdirFn func(string, func() error) error // function to use when changing directories
DeleteFn func(string) error // function used to delete files/folders
RequestFn func(*http.Request, *http.Client) (*http.Response, error) // function used to make http requests
LookPathFn func(string) (string, error) // function used to make exec.LookPath lookups
Root string // the root of the write path
Disk *Disk
steps map[string]*Step
moot *sync.RWMutex
results Results
curGen *Generator
}
func (r *Runner) Results() Results {
r.moot.Lock()
defer r.moot.Unlock()
r.results.Files = r.Disk.Files()
return r.results
}
func (r *Runner) WithRun(fn RunFn) {
g := New()
g.RunFn(fn)
r.With(g)
}
// With adds a Generator to the Runner
func (r *Runner) With(g *Generator) error {
r.moot.Lock()
step, ok := r.steps[g.StepName]
if !ok {
var err error
step, err = NewStep(g, len(r.steps))
if err != nil {
return err
}
}
r.moot.Unlock()
return r.WithStep(g.StepName, step)
}
func (r *Runner) WithGroup(gg *Group) {
for _, g := range gg.Generators {
r.With(g)
}
}
// WithNew takes a Generator and an error.
// Perfect for new-ing up generators
/*
// foo.New(Options) (*genny.Generator, error)
if err := run.WithNew(foo.New(opts)); err != nil {
return err
}
*/
func (r *Runner) WithNew(g *Generator, err error) error {
if err != nil {
return err
}
r.With(g)
return nil
}
func (r *Runner) WithStep(name string, step *Step) error {
r.moot.Lock()
defer r.moot.Unlock()
if len(name) == 0 {
name = stepName()
}
r.steps[name] = step
return nil
}
func (r *Runner) Steps() []*Step {
r.moot.RLock()
var steps []*Step
for _, step := range r.steps {
steps = append(steps, step)
}
sort.Slice(steps, func(a, b int) bool {
return steps[a].index < steps[b].index
})
r.moot.RUnlock()
return steps
}
func (r *Runner) FindStep(name string) (*Step, error) {
r.moot.RLock()
s, ok := r.steps[name]
r.moot.RUnlock()
if !ok {
return nil, fmt.Errorf("could not find step %s", name)
}
return s, nil
}
func (r *Runner) ReplaceStep(name string, s *Step) error {
os, err := r.FindStep(name)
if err != nil {
return err
}
s.index = os.index
return r.WithStep(name, s)
}
func (r *Runner) Run() error {
if f, ok := r.Logger.(io.Closer); ok {
defer f.Close()
}
steps := r.Steps()
// payload := events.Payload{
// "runner": r,
// "steps": steps,
// }
// events.EmitPayload(EvtStarted, payload)
for _, step := range steps {
if err := step.Run(r); err != nil {
// payload = events.Payload{
// "runner": r,
// "step": step,
// }
// events.EmitError(EvtFinishedErr, err, payload)
return err
}
}
// events.EmitPayload(EvtFinished, payload)
return nil
}
// Exec can be used inside of Generators to run commands
func (r *Runner) Exec(cmd *exec.Cmd) error {
r.results.Commands = append(r.results.Commands, cmd)
r.Logger.Debug("Exec: ", strings.Join(cmd.Args, " "))
if r.ExecFn == nil {
return nil
}
return safe.RunE(func() error {
return r.ExecFn(cmd)
})
}
func (r *Runner) LookPath(s string) (string, error) {
r.Logger.Debug("LookPath: ", s)
if r.LookPathFn != nil {
return r.LookPathFn(s)
}
return s, nil
}
// File can be used inside of Generators to write files
func (r *Runner) File(f File) error {
if r.curGen != nil {
var err error
f, err = r.curGen.Transform(f)
if err != nil {
return err
}
}
name := f.Name()
if !filepath.IsAbs(name) {
name = filepath.Join(r.Root, name)
}
_, isDir := f.(Dir)
if isDir {
r.Logger.Debug("Dir: ", name)
} else {
r.Logger.Debug("File: ", name)
}
if r.FileFn != nil {
err := safe.RunE(func() error {
var e error
if f, e = r.FileFn(f); e != nil {
return e
}
if s, ok := f.(io.Seeker); ok {
_, err := s.Seek(0, 0)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
}
f = NewFile(f.Name(), f)
if s, ok := f.(io.Seeker); ok {
_, err := s.Seek(0, 0)
if err != nil {
return err
}
}
r.Disk.Add(f)
return nil
}
func (r *Runner) FindFile(name string) (File, error) {
return r.Disk.Find(name)
}
// Chdir will change to the specified directory
// and revert back to the current directory when
// the runner function has returned.
// If the directory does not exist, it will be
// created for you.
func (r *Runner) Chdir(path string, fn func() error) error {
if len(path) == 0 {
return fn()
}
r.Logger.Debug("Chdir: ", path)
if r.ChdirFn != nil {
return safe.RunE(func() error {
return r.ChdirFn(path, fn)
})
}
if err := safe.RunE(fn); err != nil {
return err
}
return nil
}
func (r *Runner) Delete(path string) error {
r.Logger.Debug("Delete: ", path)
defer r.Disk.Remove(path)
if r.DeleteFn != nil {
return safe.RunE(func() error {
return r.DeleteFn(path)
})
}
return nil
}
func (r *Runner) Request(req *http.Request) (*http.Response, error) {
return r.RequestWithClient(req, http.DefaultClient)
}
func (r *Runner) RequestWithClient(req *http.Request, c *http.Client) (*http.Response, error) {
key := fmt.Sprintf("[%s] %s\n", strings.ToUpper(req.Method), req.URL)
r.Logger.Debug("Request: ", key)
store := func(res *http.Response, err error) (*http.Response, error) {
r.moot.Lock()
r.results.Requests = append(r.results.Requests, RequestResult{
Request: req,
Response: res,
Client: c,
Error: err,
})
r.moot.Unlock()
return res, err
}
if r.RequestFn == nil {
return store(nil, nil)
}
var res *http.Response
err := safe.RunE(func() error {
var e error
res, e = r.RequestFn(req, c)
if e != nil {
return e
}
return nil
})
return store(res, err)
}
// NewRunner will NOT execute commands and write files
// it is NOT destructive it is just the most basic Runner
// you can have.
func NewRunner(ctx context.Context) *Runner {
pwd, _ := os.Getwd()
l := logrus.New()
l.Out = os.Stdout
l.SetLevel(logrus.DebugLevel)
r := &Runner{
Logger: l,
Context: ctx,
Root: pwd,
moot: &sync.RWMutex{},
steps: map[string]*Step{},
}
r.Disk = newDisk(r)
return r
}