forked from paketo-buildpacks/go-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_path_manager.go
61 lines (48 loc) · 1.16 KB
/
go_path_manager.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
package gobuild
import (
"fmt"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2/fs"
)
type GoPathManager struct {
tempDir string
}
func NewGoPathManager(tempDir string) GoPathManager {
return GoPathManager{
tempDir: tempDir,
}
}
func (m GoPathManager) Setup(workspace, importPath string) (string, string, error) {
_, err := os.Stat(filepath.Join(workspace, "go.mod"))
if err == nil {
return "", workspace, nil
}
path, err := os.MkdirTemp(m.tempDir, "gopath")
if err != nil {
return "", "", fmt.Errorf("failed to setup GOPATH: %w", err)
}
if importPath == "" {
importPath = "workspace"
}
appPath := filepath.Join(path, "src", importPath)
err = os.MkdirAll(appPath, os.ModePerm)
if err != nil {
return "", "", fmt.Errorf("failed to setup GOPATH: %w", err)
}
err = fs.Copy(workspace, appPath)
if err != nil {
return "", "", fmt.Errorf("failed to copy application source onto GOPATH: %w", err)
}
return path, appPath, nil
}
func (m GoPathManager) Teardown(path string) error {
if path == "" {
return nil
}
err := os.RemoveAll(path)
if err != nil {
return fmt.Errorf("failed to teardown GOPATH: %w", err)
}
return nil
}