forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_buildpack.go
85 lines (69 loc) · 2.08 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
package pack
import (
"context"
"github.com/pkg/errors"
pubbldpkg "github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/internal/buildpackage"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/style"
)
const (
FormatImage = "image"
FormatFile = "file"
)
type PackageBuildpackOptions struct {
Name string
Format string
Config pubbldpkg.Config
Publish bool
NoPull bool
}
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)
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(bpURI))
}
packageBuilder.SetBuildpack(bp)
for _, dep := range opts.Config.Dependencies {
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))
}
depBP, err := dist.BuildpackFromRootBlob(blob)
if err != nil {
return errors.Wrapf(err, "creating buildpack from %s", style.Symbol(dep.URI))
}
packageBuilder.AddDependency(depBP)
} else if dep.ImageName != "" {
mainBP, depBPs, err := extractPackagedBuildpacks(ctx, dep.ImageName, c.imageFetcher, opts.Publish, opts.NoPull)
if err != nil {
return err
}
for _, depBP := range append([]dist.Buildpack{mainBP}, 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))
}
}