-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.go
60 lines (48 loc) · 1.33 KB
/
generate.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
package gogenerate
import (
"bytes"
"strings"
"time"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface Executable --output fakes/executable.go
type Executable interface {
Execute(pexec.Execution) error
}
type Generate struct {
executable Executable
logs scribe.Logger
clock chronos.Clock
}
func NewGenerate(executable Executable, logs scribe.Logger, clock chronos.Clock) Generate {
return Generate{
executable: executable,
logs: logs,
clock: clock,
}
}
func (g Generate) Execute(workingDir string, config GenerateConfiguration) error {
buffer := bytes.NewBuffer(nil)
args := append([]string{"generate"}, config.Flags...)
args = append(args, config.Args...)
g.logs.Process("Executing build process")
g.logs.Subprocess("Running 'go %s'", strings.Join(args, " "))
duration, err := g.clock.Measure(func() error {
return g.executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Stdout: buffer,
Stderr: buffer,
})
})
if err != nil {
g.logs.Action("Failed after %s", duration.Round(time.Millisecond))
g.logs.Detail(buffer.String())
return err
}
g.logs.Action("Completed in %s", duration.Round(time.Millisecond))
g.logs.Break()
return nil
}