forked from paketo-buildpacks/go-mod-vendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_vendor.go
124 lines (101 loc) · 2.65 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
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
package gomodvendor
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/pexec"
"github.com/paketo-buildpacks/packit/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 := m.hasVendorDirectory(workingDir)
if err != nil {
return false, "", err
}
if ok {
return false, "modules are already vendored", nil
}
ok, err = m.hasModuleGraph(workingDir)
if err != nil {
return false, "", err
}
if !ok {
return false, "module graph is empty", nil
}
return true, "", nil
}
func (m ModVendor) hasVendorDirectory(workingDir string) (bool, error) {
_, err := os.Stat(filepath.Join(workingDir, "vendor"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
return true, nil
}
func (m ModVendor) hasModuleGraph(workingDir string) (bool, error) {
buffer := bytes.NewBuffer(nil)
args := []string{"mod", "graph"}
m.logs.Process("Checking module graph")
m.logs.Subprocess("Running 'go %s'", strings.Join(args, " "))
duration, err := m.clock.Measure(func() error {
return m.executable.Execute(pexec.Execution{
Args: args,
Dir: workingDir,
Stdout: buffer,
Stderr: buffer,
})
})
if err != nil {
m.logs.Action("Failed after %s", duration.Round(time.Millisecond))
m.logs.Detail(buffer.String())
return false, err
}
m.logs.Action("Completed in %s", duration.Round(time.Millisecond))
m.logs.Break()
return buffer.Len() > 0, nil
}
func (m ModVendor) Execute(path, workingDir string) error {
buffer := bytes.NewBuffer(nil)
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("GOPATH=%s", path)),
Dir: workingDir,
Stdout: buffer,
Stderr: buffer,
})
})
if err != nil {
m.logs.Action("Failed after %s", duration.Round(time.Millisecond))
m.logs.Detail(buffer.String())
return err
}
m.logs.Action("Completed in %s", duration.Round(time.Millisecond))
m.logs.Break()
return nil
}