This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dep_ensure_process.go
79 lines (65 loc) · 2.07 KB
/
dep_ensure_process.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
package depensure
import (
// "github.com/paketo-buildpacks/packit/v2/chronos"
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"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) (err error)
}
type DepEnsureProcess struct {
executable Executable
logs scribe.Emitter
}
func NewDepEnsureProcess(executable Executable, logs scribe.Emitter) DepEnsureProcess {
return DepEnsureProcess{
executable: executable,
logs: logs,
}
}
func (p DepEnsureProcess) Execute(workspace, gopath, depcachedir string) error {
var err error
tmpAppPath := filepath.Join(gopath, "src", "app")
err = os.MkdirAll(tmpAppPath, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create GOPATH app dir: %w", err)
}
err = fs.Copy(workspace, tmpAppPath)
if err != nil {
return fmt.Errorf("failed to copy application source onto GOPATH: %w", err)
}
args := []string{"ensure"}
buffer := bytes.NewBuffer(nil)
p.logs.Subprocess("Running 'dep %s'", strings.Join(args, " "))
err = p.executable.Execute(pexec.Execution{
Args: args,
Dir: tmpAppPath,
Stdout: buffer,
Stderr: buffer,
Env: append(os.Environ(), fmt.Sprintf("GOPATH=%s", gopath), fmt.Sprintf("DEPCACHEDIR=%s", depcachedir)),
})
if err != nil {
p.logs.Detail(buffer.String())
return fmt.Errorf("'dep ensure' command failed: %w", err)
}
err = os.RemoveAll(filepath.Join(workspace, "vendor"))
if err != nil {
return fmt.Errorf("failed to remove vendor from application source: %w", err)
}
err = fs.Copy(filepath.Join(tmpAppPath, "vendor"), filepath.Join(workspace, "vendor"))
if err != nil {
return fmt.Errorf("failed to copy vendor back to application source: %w", err)
}
err = fs.Copy(filepath.Join(tmpAppPath, "Gopkg.lock"), filepath.Join(workspace, "Gopkg.lock"))
if err != nil {
return fmt.Errorf("failed to copy Gopkg.lock back to application source: %w", err)
}
return err
}