-
Notifications
You must be signed in to change notification settings - Fork 5
/
packing_tools.go
86 lines (72 loc) · 1.76 KB
/
packing_tools.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
package freezer
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/paketo-buildpacks/packit/v2/pexec"
)
//go:generate faux --interface Executable --output fakes/executable.go
type Executable interface {
Execute(pexec.Execution) error
}
type PackingTools struct {
jam Executable
pack Executable
tempOutput func(dir string, pattern string) (string, error)
}
func NewPackingTools() PackingTools {
return PackingTools{
jam: pexec.NewExecutable("jam"),
pack: pexec.NewExecutable("pack"),
tempOutput: os.MkdirTemp,
}
}
func (p PackingTools) WithExecutable(executable Executable) PackingTools {
p.jam = executable
return p
}
func (p PackingTools) WithPack(pack Executable) PackingTools {
p.pack = pack
return p
}
func (p PackingTools) WithTempOutput(tempOutput func(string, string) (string, error)) PackingTools {
p.tempOutput = tempOutput
return p
}
func (p PackingTools) Execute(buildpackDir, output, version string, cached bool) error {
jamOutput, err := p.tempOutput("", "")
if err != nil {
return err
}
defer os.RemoveAll(jamOutput)
args := []string{
"pack",
"--buildpack", filepath.Join(buildpackDir, "buildpack.toml"),
"--output", filepath.Join(jamOutput, fmt.Sprintf("%s.tgz", version)),
"--version", version,
}
if cached {
args = append(args, "--offline")
}
err = p.jam.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
return err
}
args = []string{
"buildpack", "package",
output,
"--path", filepath.Join(jamOutput, fmt.Sprintf("%s.tgz", version)),
"--format", "file",
"--target", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
return p.pack.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
}