forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_buildpack.go
105 lines (86 loc) · 2.8 KB
/
package_buildpack.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
package pack
import (
"context"
"github.com/pkg/errors"
pubbldpkg "github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/internal/archive"
"github.com/buildpacks/pack/internal/buildpackage"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/style"
)
const (
FormatImage = "image"
FormatFile = "file"
)
// PackageBuildpackOptions are configuration options and metadata you can pass into PackageBuildpack
type PackageBuildpackOptions struct {
Name string
Format string
Config pubbldpkg.Config
Publish bool
NoPull bool
}
// PackageBuildpack packages buildpack(s) into an image or file
func (c *Client) PackageBuildpack(ctx context.Context, opts PackageBuildpackOptions) error {
packageBuilder := buildpackage.NewBuilder(c.imageFactory)
if opts.Format == "" {
opts.Format = FormatImage
}
bpURI := opts.Config.Buildpack.URI
if bpURI == "" {
return errors.New("buildpack URI must be provided")
}
blob, err := c.downloader.Download(ctx, bpURI)
if err != nil {
return errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(bpURI))
}
bp, err := dist.BuildpackFromRootBlob(blob, archive.DefaultTarWriterFactory())
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(bpURI))
}
packageBuilder.SetBuildpack(bp)
for _, dep := range opts.Config.Dependencies {
var depBPs []dist.Buildpack
if dep.URI != "" {
blob, err := c.downloader.Download(ctx, dep.URI)
if err != nil {
return errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(dep.URI))
}
isOCILayout, err := buildpackage.IsOCILayoutBlob(blob)
if err != nil {
return errors.Wrap(err, "inspecting buildpack blob")
}
if isOCILayout {
mainBP, deps, err := buildpackage.BuildpacksFromOCILayoutBlob(blob)
if err != nil {
return errors.Wrapf(err, "extracting buildpacks from %s", style.Symbol(dep.URI))
}
depBPs = append([]dist.Buildpack{mainBP}, deps...)
} else {
depBP, err := dist.BuildpackFromRootBlob(blob, archive.DefaultTarWriterFactory())
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(dep.URI))
}
depBPs = []dist.Buildpack{depBP}
}
} else if dep.ImageName != "" {
mainBP, deps, err := extractPackagedBuildpacks(ctx, dep.ImageName, c.imageFetcher, opts.Publish, opts.NoPull)
if err != nil {
return err
}
depBPs = append([]dist.Buildpack{mainBP}, deps...)
}
for _, depBP := range depBPs {
packageBuilder.AddDependency(depBP)
}
}
switch opts.Format {
case FormatFile:
return packageBuilder.SaveAsFile(opts.Name)
case FormatImage:
_, err = packageBuilder.SaveAsImage(opts.Name, opts.Publish)
return errors.Wrapf(err, "saving image")
default:
return errors.Errorf("unknown format: %s", style.Symbol(opts.Format))
}
}