forked from ignite/cli
/
run.go
104 lines (92 loc) · 2.54 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
package xgenny
import (
"context"
"errors"
"os"
"strings"
"github.com/gobuffalo/genny"
"github.com/gobuffalo/logger"
"github.com/gobuffalo/packd"
"github.com/notional-labs/tinyport/tinyport/pkg/placeholder"
"github.com/notional-labs/tinyport/tinyport/pkg/validation"
)
var _ validation.Error = (*dryRunError)(nil)
type dryRunError struct {
error
}
// ValidationInfo returns validation info
func (d *dryRunError) ValidationInfo() string {
return d.Error()
}
// DryRunner is a genny DryRunner with a logger
func DryRunner(ctx context.Context) *genny.Runner {
runner := genny.DryRunner(ctx)
runner.Logger = logger.New(genny.DefaultLogLvl)
return runner
}
// RunWithValidation checks the generators with a dry run and then execute the wet runner to the generators
func RunWithValidation(
tracer *placeholder.Tracer,
gens ...*genny.Generator,
) (sm SourceModification, err error) {
// run executes the provided runner with the provided generator
run := func(runner *genny.Runner, gen *genny.Generator) error {
err := runner.With(gen)
if err != nil {
return err
}
return runner.Run()
}
for _, gen := range gens {
// check with a dry runner the generators
dryRunner := DryRunner(context.Background())
if err := run(dryRunner, gen); err != nil {
if errors.Is(err, os.ErrNotExist) {
return sm, &dryRunError{err}
}
return sm, err
}
if err := tracer.Err(); err != nil {
return sm, err
}
// fetch the source modification
sm = NewSourceModification()
for _, file := range dryRunner.Results().Files {
fileName := file.Name()
_, err := os.Stat(fileName)
// nolint:gocritic
if os.IsNotExist(err) {
// if the file doesn't exist in the source, it means it has been created by the runner
sm.AppendCreatedFiles(fileName)
} else if err != nil {
return sm, err
} else {
// the file has been modified by the runner
sm.AppendModifiedFiles(fileName)
}
}
// execute the modification with a wet runner
if err := run(genny.WetRunner(context.Background()), gen); err != nil {
return sm, err
}
}
return sm, nil
}
// Box will mount each file in the Box and wrap it, already existing files are ignored
func Box(g *genny.Generator, box packd.Walker) error {
return box.Walk(func(path string, bf packd.File) error {
f := genny.NewFile(path, bf)
f, err := g.Transform(f)
if err != nil {
return err
}
filePath := strings.TrimSuffix(f.Name(), ".plush")
_, err = os.Stat(filePath)
if os.IsNotExist(err) {
// path doesn't exist. move on.
g.File(f)
return nil
}
return err
})
}