-
Notifications
You must be signed in to change notification settings - Fork 12
/
mod_vendor.go
71 lines (59 loc) · 1.61 KB
/
mod_vendor.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
package gomodvendor
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/fs"
"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 ModVendor struct {
executable Executable
logs scribe.Emitter
clock chronos.Clock
}
func NewModVendor(executable Executable, logs scribe.Emitter, clock chronos.Clock) ModVendor {
return ModVendor{
executable: executable,
logs: logs,
clock: clock,
}
}
func (m ModVendor) ShouldRun(workingDir string) (bool, string, error) {
ok, err := fs.Exists(filepath.Join(workingDir, "vendor"))
if err != nil {
return false, "", err
}
if ok {
return false, "modules are already vendored", nil
}
return true, "", nil
}
func (m ModVendor) Execute(path, workingDir string) error {
args := []string{"mod", "vendor"}
m.logs.Process("Executing build process")
m.logs.Subprocess("Running 'go %s'", strings.Join(args, " "))
duration, err := m.clock.Measure(func() error {
return m.executable.Execute(pexec.Execution{
Args: args,
Env: append(os.Environ(), fmt.Sprintf("GOMODCACHE=%s", path)),
Dir: workingDir,
Stdout: m.logs.ActionWriter,
Stderr: m.logs.ActionWriter,
})
})
if err != nil {
m.logs.Action("Failed after %s", duration.Round(time.Millisecond))
return err
}
m.logs.Action("Completed in %s", duration.Round(time.Millisecond))
m.logs.Break()
return nil
}